Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Initialize the Groq client
|
| 6 |
client = Groq(api_key=os.environ["keko"])
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
if audio_file is None:
|
| 10 |
-
return "No audio file provided."
|
| 11 |
-
|
| 12 |
# Create a transcription of the audio file
|
| 13 |
with open(audio_file, "rb") as file:
|
| 14 |
transcription = client.audio.transcriptions.create(
|
|
@@ -19,15 +23,54 @@ def transcribe_audio(audio_file):
|
|
| 19 |
temperature=0.0
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Create the Gradio interface
|
| 25 |
iface = gr.Interface(
|
| 26 |
-
fn=
|
| 27 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 28 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 29 |
title="Audio Transcription with Groq",
|
| 30 |
-
description="Record audio or upload a file - speech-to-text."
|
| 31 |
)
|
|
|
|
| 32 |
# Launch the interface
|
| 33 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from groq import Groq
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
|
| 8 |
# Initialize the Groq client
|
| 9 |
client = Groq(api_key=os.environ["keko"])
|
| 10 |
|
| 11 |
+
|
| 12 |
+
def transcribe_and_save_audio(audio_file):
|
| 13 |
if audio_file is None:
|
| 14 |
+
return "No audio file provided.", None
|
| 15 |
+
|
| 16 |
# Create a transcription of the audio file
|
| 17 |
with open(audio_file, "rb") as file:
|
| 18 |
transcription = client.audio.transcriptions.create(
|
|
|
|
| 23 |
temperature=0.0
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Save the audio file as MP3
|
| 27 |
+
mp3_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 28 |
+
shutil.copy2(audio_file, mp3_file.name)
|
| 29 |
+
|
| 30 |
+
return transcription.text, mp3_file.name
|
| 31 |
|
| 32 |
# Create the Gradio interface
|
| 33 |
iface = gr.Interface(
|
| 34 |
+
fn=transcribe_and_save_audio,
|
| 35 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 36 |
+
outputs=[
|
| 37 |
+
gr.Textbox(label="Transcription", show_copy_button=True),
|
| 38 |
+
gr.Audio(label="Recorded Audio (MP3)", format="mp3")
|
| 39 |
+
],
|
| 40 |
title="Audio Transcription with Groq",
|
| 41 |
+
description="Record audio or upload a file - speech-to-text. You can also download the recorded audio as MP3."
|
| 42 |
)
|
| 43 |
+
|
| 44 |
# Launch the interface
|
| 45 |
+
iface.launch()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# def transcribe_audio(audio_file):
|
| 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(
|
| 58 |
+
# file=(audio_file, file.read()),
|
| 59 |
+
# model="whisper-large-v3",
|
| 60 |
+
# prompt="text may contain medical gastroenterology terms",
|
| 61 |
+
# response_format="json",
|
| 62 |
+
# temperature=0.0
|
| 63 |
+
# )
|
| 64 |
+
|
| 65 |
+
# return transcription.text
|
| 66 |
+
|
| 67 |
+
# # Create the Gradio interface
|
| 68 |
+
# iface = gr.Interface(
|
| 69 |
+
# fn=transcribe_audio,
|
| 70 |
+
# inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 71 |
+
# outputs=gr.Textbox(label="Transcription", show_copy_button=True),
|
| 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()
|