Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,10 @@ import os
|
|
7 |
from gtts import gTTS
|
8 |
import speech_recognition as sr
|
9 |
from groq import Groq
|
|
|
10 |
|
11 |
# Set up Groq API
|
12 |
-
groq_api_key = "
|
13 |
client = Groq(api_key=groq_api_key)
|
14 |
|
15 |
def process_text(text):
|
@@ -36,16 +37,18 @@ def text_to_speech(text, lang='ur'):
|
|
36 |
def audio_to_text(audio_file):
|
37 |
# Convert audio to text
|
38 |
recognizer = sr.Recognizer()
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
# Streamlit UI
|
51 |
st.title("Urdu Voice Assistant")
|
@@ -80,8 +83,15 @@ elif mode == "Upload Voice File":
|
|
80 |
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
|
81 |
if uploaded_file:
|
82 |
st.write("Processing...")
|
83 |
-
# Convert
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
st.write(f"Transcribed Text: {text}")
|
86 |
|
87 |
# Get response from Groq
|
@@ -91,4 +101,3 @@ elif mode == "Upload Voice File":
|
|
91 |
# Convert response to audio
|
92 |
audio_file = text_to_speech(response_text)
|
93 |
st.audio(audio_file, format='audio/mp3')
|
94 |
-
|
|
|
7 |
from gtts import gTTS
|
8 |
import speech_recognition as sr
|
9 |
from groq import Groq
|
10 |
+
import tempfile
|
11 |
|
12 |
# Set up Groq API
|
13 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
14 |
client = Groq(api_key=groq_api_key)
|
15 |
|
16 |
def process_text(text):
|
|
|
37 |
def audio_to_text(audio_file):
|
38 |
# Convert audio to text
|
39 |
recognizer = sr.Recognizer()
|
40 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
41 |
+
temp_file.write(audio_file.read())
|
42 |
+
temp_file.seek(0)
|
43 |
+
with sr.AudioFile(temp_file.name) as source:
|
44 |
+
audio_data = recognizer.record(source)
|
45 |
+
try:
|
46 |
+
text = recognizer.recognize_google(audio_data, language='ur')
|
47 |
+
return text
|
48 |
+
except sr.UnknownValueError:
|
49 |
+
return "Could not understand audio"
|
50 |
+
except sr.RequestError as e:
|
51 |
+
return f"Could not request results; {e}"
|
52 |
|
53 |
# Streamlit UI
|
54 |
st.title("Urdu Voice Assistant")
|
|
|
83 |
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
|
84 |
if uploaded_file:
|
85 |
st.write("Processing...")
|
86 |
+
# Convert uploaded file to WAV format if needed
|
87 |
+
if uploaded_file.type == "audio/mpeg":
|
88 |
+
audio = AudioSegment.from_mp3(uploaded_file)
|
89 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
90 |
+
audio.export(temp_file.name, format="wav")
|
91 |
+
with open(temp_file.name, "rb") as temp_file_content:
|
92 |
+
text = audio_to_text(temp_file_content)
|
93 |
+
else:
|
94 |
+
text = audio_to_text(uploaded_file)
|
95 |
st.write(f"Transcribed Text: {text}")
|
96 |
|
97 |
# Get response from Groq
|
|
|
101 |
# Convert response to audio
|
102 |
audio_file = text_to_speech(response_text)
|
103 |
st.audio(audio_file, format='audio/mp3')
|
|