Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from pydub import AudioSegment
|
4 |
+
from pydub.playback import play
|
5 |
+
from io import BytesIO
|
6 |
+
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 = "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"
|
13 |
+
client = Groq(api_key=groq_api_key)
|
14 |
+
|
15 |
+
def process_text(text):
|
16 |
+
# Use Groq to generate a response
|
17 |
+
try:
|
18 |
+
chat_completion = client.chat.completions.create(
|
19 |
+
messages=[
|
20 |
+
{"role": "user", "content": text}
|
21 |
+
],
|
22 |
+
model="llama3-8b-8192",
|
23 |
+
)
|
24 |
+
return chat_completion.choices[0].message.content
|
25 |
+
except Exception as e:
|
26 |
+
return f"Error fetching Groq data: {e}"
|
27 |
+
|
28 |
+
def text_to_speech(text, lang='ur'):
|
29 |
+
# Convert text to speech
|
30 |
+
tts = gTTS(text=text, lang=lang)
|
31 |
+
audio_file = BytesIO()
|
32 |
+
tts.write_to_fp(audio_file)
|
33 |
+
audio_file.seek(0)
|
34 |
+
return audio_file
|
35 |
+
|
36 |
+
def audio_to_text(audio_file):
|
37 |
+
# Convert audio to text
|
38 |
+
recognizer = sr.Recognizer()
|
39 |
+
audio = sr.AudioFile(audio_file)
|
40 |
+
with audio as source:
|
41 |
+
audio_data = recognizer.record(source)
|
42 |
+
try:
|
43 |
+
text = recognizer.recognize_google(audio_data, language='ur')
|
44 |
+
return text
|
45 |
+
except sr.UnknownValueError:
|
46 |
+
return "Could not understand audio"
|
47 |
+
except sr.RequestError as e:
|
48 |
+
return f"Could not request results; {e}"
|
49 |
+
|
50 |
+
# Streamlit UI
|
51 |
+
st.title("Urdu Voice Assistant")
|
52 |
+
|
53 |
+
mode = st.radio("Choose input method", ("Real-time Voice", "Upload Voice File"))
|
54 |
+
|
55 |
+
if mode == "Real-time Voice":
|
56 |
+
st.write("Click the button and start speaking.")
|
57 |
+
if st.button("Start Recording"):
|
58 |
+
st.write("Recording... Please wait.")
|
59 |
+
recognizer = sr.Recognizer()
|
60 |
+
with sr.Microphone() as source:
|
61 |
+
audio_data = recognizer.listen(source)
|
62 |
+
st.write("Processing...")
|
63 |
+
try:
|
64 |
+
text = recognizer.recognize_google(audio_data, language='ur')
|
65 |
+
st.write(f"You said: {text}")
|
66 |
+
|
67 |
+
# Get response from Groq
|
68 |
+
response_text = process_text(text)
|
69 |
+
st.write(f"Response: {response_text}")
|
70 |
+
|
71 |
+
# Convert response to audio
|
72 |
+
audio_file = text_to_speech(response_text)
|
73 |
+
st.audio(audio_file, format='audio/mp3')
|
74 |
+
except sr.UnknownValueError:
|
75 |
+
st.write("Sorry, could not understand the audio.")
|
76 |
+
except sr.RequestError as e:
|
77 |
+
st.write(f"Sorry, there was an error with the request: {e}")
|
78 |
+
|
79 |
+
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 the uploaded file to text
|
84 |
+
text = audio_to_text(uploaded_file)
|
85 |
+
st.write(f"Transcribed Text: {text}")
|
86 |
+
|
87 |
+
# Get response from Groq
|
88 |
+
response_text = process_text(text)
|
89 |
+
st.write(f"Response: {response_text}")
|
90 |
+
|
91 |
+
# Convert response to audio
|
92 |
+
audio_file = text_to_speech(response_text)
|
93 |
+
st.audio(audio_file, format='audio/mp3')
|
94 |
+
|