Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,20 @@ import shutil
|
|
| 9 |
client = Groq(api_key=os.environ["keko"])
|
| 10 |
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
if audio_file is None:
|
| 14 |
-
return "No audio file provided."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Create a transcription of the audio file
|
| 17 |
with open(audio_file, "rb") as file:
|
|
@@ -23,35 +34,48 @@ def transcribe_and_save_audio(audio_file):
|
|
| 23 |
temperature=0.0
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Launch the interface
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
-
# def
|
| 52 |
# if audio_file is None:
|
| 53 |
-
# return "No audio file provided."
|
| 54 |
-
|
| 55 |
# # Create a transcription of the audio file
|
| 56 |
# with open(audio_file, "rb") as file:
|
| 57 |
# transcription = client.audio.transcriptions.create(
|
|
@@ -62,15 +86,27 @@ iface.launch()
|
|
| 62 |
# temperature=0.0
|
| 63 |
# )
|
| 64 |
|
| 65 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# # Create the Gradio interface
|
| 68 |
# iface = gr.Interface(
|
| 69 |
-
# fn=
|
| 70 |
# inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 71 |
-
# outputs=
|
|
|
|
|
|
|
|
|
|
| 72 |
# title="Audio Transcription with Groq",
|
| 73 |
-
# description="Record audio or upload a file - speech-to-text."
|
| 74 |
# )
|
|
|
|
| 75 |
# # Launch the interface
|
| 76 |
-
# iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
client = Groq(api_key=os.environ["keko"])
|
| 10 |
|
| 11 |
|
| 12 |
+
|
| 13 |
+
def save_audio(audio_file):
|
| 14 |
if audio_file is None:
|
| 15 |
+
return None, "No audio file provided."
|
| 16 |
+
|
| 17 |
+
# Save the audio file as MP3
|
| 18 |
+
mp3_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 19 |
+
shutil.copy2(audio_file, mp3_file.name)
|
| 20 |
+
|
| 21 |
+
return mp3_file.name, "Audio saved successfully."
|
| 22 |
+
|
| 23 |
+
def transcribe_audio(audio_file):
|
| 24 |
+
if audio_file is None:
|
| 25 |
+
return "No audio file provided."
|
| 26 |
|
| 27 |
# Create a transcription of the audio file
|
| 28 |
with open(audio_file, "rb") as file:
|
|
|
|
| 34 |
temperature=0.0
|
| 35 |
)
|
| 36 |
|
| 37 |
+
return transcription.text
|
| 38 |
+
|
| 39 |
+
# Create the Gradio interface using Blocks
|
| 40 |
+
with gr.Blocks(title="Audio Recorder and Transcriber") as demo:
|
| 41 |
+
gr.Markdown("# Audio Recorder and Transcriber")
|
| 42 |
+
gr.Markdown("Record audio or upload a file. You can download the audio or transcribe it.")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio")
|
| 46 |
|
| 47 |
+
with gr.Row():
|
| 48 |
+
save_btn = gr.Button("Save Audio")
|
| 49 |
+
transcribe_btn = gr.Button("Transcribe Audio")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
audio_output = gr.Audio(label="Saved Audio (MP3)", format="mp3")
|
| 53 |
+
save_msg = gr.Textbox(label="Save Status")
|
| 54 |
+
|
| 55 |
+
transcription_output = gr.Textbox(label="Transcription", show_copy_button=True)
|
| 56 |
+
|
| 57 |
+
save_btn.click(
|
| 58 |
+
save_audio,
|
| 59 |
+
inputs=[audio_input],
|
| 60 |
+
outputs=[audio_output, save_msg]
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
transcribe_btn.click(
|
| 64 |
+
transcribe_audio,
|
| 65 |
+
inputs=[audio_input],
|
| 66 |
+
outputs=[transcription_output]
|
| 67 |
+
)
|
| 68 |
|
| 69 |
# Launch the interface
|
| 70 |
+
demo.launch()
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
+
# def transcribe_and_save_audio(audio_file):
|
| 76 |
# if audio_file is None:
|
| 77 |
+
# return "No audio file provided.", None
|
| 78 |
+
|
| 79 |
# # Create a transcription of the audio file
|
| 80 |
# with open(audio_file, "rb") as file:
|
| 81 |
# transcription = client.audio.transcriptions.create(
|
|
|
|
| 86 |
# temperature=0.0
|
| 87 |
# )
|
| 88 |
|
| 89 |
+
# # Save the audio file as MP3
|
| 90 |
+
# mp3_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 91 |
+
# shutil.copy2(audio_file, mp3_file.name)
|
| 92 |
+
|
| 93 |
+
# return transcription.text, mp3_file.name
|
| 94 |
|
| 95 |
# # Create the Gradio interface
|
| 96 |
# iface = gr.Interface(
|
| 97 |
+
# fn=transcribe_and_save_audio,
|
| 98 |
# inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 99 |
+
# outputs=[
|
| 100 |
+
# gr.Textbox(label="Transcription", show_copy_button=True),
|
| 101 |
+
# gr.Audio(label="Recorded Audio (MP3)", format="mp3")
|
| 102 |
+
# ],
|
| 103 |
# title="Audio Transcription with Groq",
|
| 104 |
+
# description="Record audio or upload a file - speech-to-text. You can also download the recorded audio as MP3."
|
| 105 |
# )
|
| 106 |
+
|
| 107 |
# # Launch the interface
|
| 108 |
+
# iface.launch()
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|