Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,8 +55,6 @@
|
|
55 |
|
56 |
# iface.launch()
|
57 |
|
58 |
-
|
59 |
-
|
60 |
import os
|
61 |
import gradio as gr
|
62 |
import whisper
|
@@ -66,51 +64,53 @@ from groq import Groq
|
|
66 |
|
67 |
# Initialize the Groq client
|
68 |
groq_api_key = os.getenv('GROQ_API_KEY')
|
69 |
-
|
|
|
|
|
70 |
|
71 |
# Load the Whisper model
|
72 |
model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
|
73 |
|
74 |
def process_audio(file_path):
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
|
109 |
iface = gr.Interface(
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
)
|
115 |
|
116 |
iface.launch()
|
|
|
55 |
|
56 |
# iface.launch()
|
57 |
|
|
|
|
|
58 |
import os
|
59 |
import gradio as gr
|
60 |
import whisper
|
|
|
64 |
|
65 |
# Initialize the Groq client
|
66 |
groq_api_key = os.getenv('GROQ_API_KEY')
|
67 |
+
if not groq_api_key:
|
68 |
+
raise ValueError("GROQ_API_KEY environment variable is not set.")
|
69 |
+
client = Groq(api_key=groq_api_key)
|
70 |
|
71 |
# Load the Whisper model
|
72 |
model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
|
73 |
|
74 |
def process_audio(file_path):
|
75 |
+
try:
|
76 |
+
# Load the audio file
|
77 |
+
audio = whisper.load_audio(file_path)
|
78 |
+
|
79 |
+
# Transcribe the audio using Whisper
|
80 |
+
result = model.transcribe(audio)
|
81 |
+
text = result["text"]
|
82 |
+
|
83 |
+
# Generate a response using Groq
|
84 |
+
chat_completion = client.chat.completions.create(
|
85 |
+
messages=[{"role": "user", "content": text}],
|
86 |
+
model="llama3-8b-8192", # Replace with the correct model if necessary
|
87 |
+
)
|
88 |
+
|
89 |
+
# Access the response using dot notation
|
90 |
+
response_message = chat_completion.choices[0].message.content.strip()
|
91 |
+
|
92 |
+
# Convert the response text to speech
|
93 |
+
tts = gTTS(response_message)
|
94 |
+
response_audio_io = io.BytesIO()
|
95 |
+
tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
|
96 |
+
response_audio_io.seek(0)
|
97 |
+
|
98 |
+
# Save audio to a file to ensure it's generated correctly
|
99 |
+
response_audio_path = "response.mp3"
|
100 |
+
with open(response_audio_path, "wb") as audio_file:
|
101 |
+
audio_file.write(response_audio_io.getvalue())
|
102 |
+
|
103 |
+
# Return the response text and the path to the saved audio file
|
104 |
+
return response_message, response_audio_path
|
105 |
+
|
106 |
+
except Exception as e:
|
107 |
+
return f"An error occurred: {e}", None
|
108 |
|
109 |
iface = gr.Interface(
|
110 |
+
fn=process_audio,
|
111 |
+
inputs=gr.Audio(type="filepath"), # Use type="filepath"
|
112 |
+
outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
|
113 |
+
live=True
|
114 |
)
|
115 |
|
116 |
iface.launch()
|