Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,15 @@ from groq import Groq
|
|
| 5 |
# Predefined password
|
| 6 |
CORRECT_PASSWORD = "password123"
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
with open(audio_file.name, "rb") as file:
|
| 16 |
transcription = client.audio.transcriptions.create(
|
| 17 |
file=(audio_file.name, file.read()),
|
|
@@ -19,63 +21,33 @@ def transcribe_audio(api_key, audio_file=None):
|
|
| 19 |
temperature=1,
|
| 20 |
response_format="verbose_json",
|
| 21 |
)
|
| 22 |
-
return transcription.text
|
| 23 |
-
|
| 24 |
-
return "
|
| 25 |
-
|
| 26 |
-
# Create the
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
audio_file: gr.File.update(visible=True),
|
| 48 |
-
transcribe_button: gr.Button.update(visible=True),
|
| 49 |
-
output: gr.Textbox.update(visible=True)
|
| 50 |
-
}
|
| 51 |
-
else:
|
| 52 |
-
return {
|
| 53 |
-
password_message: gr.Markdown.update(visible=True, value="Incorrect password. Please try again.")
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
# Connect components
|
| 57 |
-
submit_button.click(
|
| 58 |
-
check_password_and_update,
|
| 59 |
-
inputs=[password_input],
|
| 60 |
-
outputs=[password_input, submit_button, password_message, api_key, audio_file, transcribe_button, output]
|
| 61 |
-
)
|
| 62 |
-
transcribe_button.click(
|
| 63 |
-
transcribe_audio,
|
| 64 |
-
inputs=[api_key, audio_file],
|
| 65 |
-
outputs=output
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
# Instructions
|
| 69 |
-
gr.Markdown(
|
| 70 |
-
"""
|
| 71 |
-
## How to use this app:
|
| 72 |
-
1. Enter the correct password to access the app.
|
| 73 |
-
2. Enter your [Groq API Key](https://console.groq.com/keys) in the provided field.
|
| 74 |
-
3. Click on the upload section and provide a supported audio file. Supported audio files include mp3, mp4, mpeg, mpga, m4a, wav, and webm file types.
|
| 75 |
-
4. Click the "Transcribe" button to transcribe your speech.
|
| 76 |
-
5. The transcription will appear in the text box below.
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
-
|
|
|
|
| 5 |
# Predefined password
|
| 6 |
CORRECT_PASSWORD = "password123"
|
| 7 |
|
| 8 |
+
def process_input(password, api_key, audio_file):
|
| 9 |
+
if password != CORRECT_PASSWORD:
|
| 10 |
+
return "Incorrect password. Please try again.", "", ""
|
| 11 |
+
|
| 12 |
+
if not api_key or not audio_file:
|
| 13 |
+
return "Password correct. Please enter your API key and upload an audio file.", "", ""
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
client = Groq(api_key=api_key)
|
| 17 |
with open(audio_file.name, "rb") as file:
|
| 18 |
transcription = client.audio.transcriptions.create(
|
| 19 |
file=(audio_file.name, file.read()),
|
|
|
|
| 21 |
temperature=1,
|
| 22 |
response_format="verbose_json",
|
| 23 |
)
|
| 24 |
+
return "Transcription successful", transcription.text, ""
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"An error occurred: {str(e)}", "", ""
|
| 27 |
+
|
| 28 |
+
# Create the interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=process_input,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Textbox(type="password", label="Password"),
|
| 33 |
+
gr.Textbox(label="Groq API Key", type="password"),
|
| 34 |
+
gr.File(label="Upload Audio File")
|
| 35 |
+
],
|
| 36 |
+
outputs=[
|
| 37 |
+
gr.Textbox(label="Status"),
|
| 38 |
+
gr.Textbox(label="Transcribed Text"),
|
| 39 |
+
gr.Audio(label="Audio File") # This will display the uploaded audio file
|
| 40 |
+
],
|
| 41 |
+
title="Audio Transcription App",
|
| 42 |
+
description="""
|
| 43 |
+
How to use this app:
|
| 44 |
+
1. Enter the correct password.
|
| 45 |
+
2. Enter your Groq API Key.
|
| 46 |
+
3. Upload a supported audio file (mp3, mp4, mpeg, mpga, m4a, wav, or webm).
|
| 47 |
+
4. Click "Submit" to transcribe your speech.
|
| 48 |
+
5. The transcription will appear in the "Transcribed Text" box.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
"""
|
| 50 |
+
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
iface.launch(share=True)
|