Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,19 @@ import gradio as gr
|
|
3 |
import whisper
|
4 |
from gtts import gTTS
|
5 |
import io
|
|
|
6 |
from groq import Groq
|
7 |
import time
|
8 |
|
9 |
-
# Ensure GROQ_API_KEY
|
10 |
-
GROQ_API_KEY ="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"
|
|
|
|
|
|
|
11 |
if not GROQ_API_KEY:
|
12 |
raise ValueError("GROQ_API_KEY is not set in environment variables.")
|
|
|
|
|
13 |
|
14 |
# Initialize the Groq client
|
15 |
client = Groq(api_key=GROQ_API_KEY)
|
@@ -17,6 +23,24 @@ client = Groq(api_key=GROQ_API_KEY)
|
|
17 |
# Load the Whisper model
|
18 |
model = whisper.load_model("base") # Ensure this model supports Urdu; otherwise, choose a suitable model
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def process_audio(file_path):
|
21 |
try:
|
22 |
# Load the audio file
|
@@ -26,14 +50,19 @@ def process_audio(file_path):
|
|
26 |
result = model.transcribe(audio, language="ur") # Specify 'ur' for Urdu
|
27 |
text = result["text"]
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Convert the response text to Urdu speech
|
39 |
tts = gTTS(response_message, lang='ur') # Specify language 'ur' for Urdu
|
@@ -61,4 +90,4 @@ iface = gr.Interface(
|
|
61 |
live=True # Set to False if you do not need real-time updates
|
62 |
)
|
63 |
|
64 |
-
iface.launch()
|
|
|
3 |
import whisper
|
4 |
from gtts import gTTS
|
5 |
import io
|
6 |
+
import requests
|
7 |
from groq import Groq
|
8 |
import time
|
9 |
|
10 |
+
# Ensure GROQ_API_KEY and OpenWeather API key are defined
|
11 |
+
GROQ_API_KEY = "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"
|
12 |
+
OPENWEATHER_API_KEY = "aa4db8152e46c2f3fb19fad5d58a0ed8"
|
13 |
+
OPENWEATHER_URL = "https://api.openweathermap.org/data/2.5/weather"
|
14 |
+
|
15 |
if not GROQ_API_KEY:
|
16 |
raise ValueError("GROQ_API_KEY is not set in environment variables.")
|
17 |
+
if not OPENWEATHER_API_KEY:
|
18 |
+
raise ValueError("OPENWEATHER_API_KEY is not set in environment variables.")
|
19 |
|
20 |
# Initialize the Groq client
|
21 |
client = Groq(api_key=GROQ_API_KEY)
|
|
|
23 |
# Load the Whisper model
|
24 |
model = whisper.load_model("base") # Ensure this model supports Urdu; otherwise, choose a suitable model
|
25 |
|
26 |
+
def fetch_weather():
|
27 |
+
try:
|
28 |
+
response = requests.get(
|
29 |
+
OPENWEATHER_URL,
|
30 |
+
params={
|
31 |
+
'q': 'London', # Change to dynamic city name as needed
|
32 |
+
'appid': OPENWEATHER_API_KEY,
|
33 |
+
'units': 'metric'
|
34 |
+
}
|
35 |
+
)
|
36 |
+
response.raise_for_status()
|
37 |
+
weather_data = response.json()
|
38 |
+
temp = weather_data['main']['temp']
|
39 |
+
weather_description = weather_data['weather'][0]['description']
|
40 |
+
return f"Current temperature is {temp}°C with {weather_description}."
|
41 |
+
except Exception as e:
|
42 |
+
return f"Unable to fetch weather data: {e}"
|
43 |
+
|
44 |
def process_audio(file_path):
|
45 |
try:
|
46 |
# Load the audio file
|
|
|
50 |
result = model.transcribe(audio, language="ur") # Specify 'ur' for Urdu
|
51 |
text = result["text"]
|
52 |
|
53 |
+
# Check if the text contains any weather-related keywords
|
54 |
+
weather_keywords = ['weather', 'temperature', 'climate', 'forecast']
|
55 |
+
if any(keyword in text.lower() for keyword in weather_keywords):
|
56 |
+
weather_info = fetch_weather()
|
57 |
+
response_message = f"The weather update: {weather_info}"
|
58 |
+
else:
|
59 |
+
# Generate a response in Urdu using Groq
|
60 |
+
chat_completion = client.chat.completions.create(
|
61 |
+
messages=[{"role": "user", "content": text}],
|
62 |
+
model="gemma2-9b-it", # Ensure this model can handle Urdu
|
63 |
+
)
|
64 |
+
# Access the response using dot notation
|
65 |
+
response_message = chat_completion.choices[0].message.content.strip()
|
66 |
|
67 |
# Convert the response text to Urdu speech
|
68 |
tts = gTTS(response_message, lang='ur') # Specify language 'ur' for Urdu
|
|
|
90 |
live=True # Set to False if you do not need real-time updates
|
91 |
)
|
92 |
|
93 |
+
iface.launch()
|