|
import os |
|
import gradio as gr |
|
import whisper |
|
from gtts import gTTS |
|
import io |
|
import requests |
|
from groq import Groq |
|
import time |
|
|
|
|
|
GROQ_API_KEY = "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" |
|
OPENWEATHER_API_KEY = "aa4db8152e46c2f3fb19fad5d58a0ed8" |
|
OPENWEATHER_URL = "https://api.openweathermap.org/data/2.5/weather" |
|
|
|
if not GROQ_API_KEY: |
|
raise ValueError("GROQ_API_KEY is not set in environment variables.") |
|
if not OPENWEATHER_API_KEY: |
|
raise ValueError("OPENWEATHER_API_KEY is not set in environment variables.") |
|
|
|
|
|
client = Groq(api_key=GROQ_API_KEY) |
|
|
|
|
|
model = whisper.load_model("base") |
|
|
|
def fetch_weather(): |
|
try: |
|
response = requests.get( |
|
OPENWEATHER_URL, |
|
params={ |
|
'q': 'London', |
|
'appid': OPENWEATHER_API_KEY, |
|
'units': 'metric' |
|
} |
|
) |
|
response.raise_for_status() |
|
weather_data = response.json() |
|
temp = weather_data['main']['temp'] |
|
weather_description = weather_data['weather'][0]['description'] |
|
return f"Current temperature is {temp}°C with {weather_description}." |
|
except Exception as e: |
|
return f"Unable to fetch weather data: {e}" |
|
|
|
def process_audio(file_path): |
|
try: |
|
|
|
audio = whisper.load_audio(file_path) |
|
|
|
|
|
result = model.transcribe(audio, language="ur") |
|
text = result["text"] |
|
|
|
|
|
weather_keywords = ['weather', 'temperature', 'climate', 'forecast'] |
|
if any(keyword in text.lower() for keyword in weather_keywords): |
|
weather_info = fetch_weather() |
|
response_message = f"The weather update: {weather_info}" |
|
else: |
|
|
|
chat_completion = client.chat.completions.create( |
|
messages=[{"role": "user", "content": text}], |
|
model="llama-3.1-70b-versatile", |
|
) |
|
|
|
response_message = chat_completion.choices[0].message.content.strip() |
|
|
|
|
|
tts = gTTS(response_message, lang='ur') |
|
response_audio_io = io.BytesIO() |
|
tts.write_to_fp(response_audio_io) |
|
response_audio_io.seek(0) |
|
|
|
|
|
response_audio_path = "response_" + str(int(time.time())) + ".mp3" |
|
|
|
|
|
with open(response_audio_path, "wb") as audio_file: |
|
audio_file.write(response_audio_io.getvalue()) |
|
|
|
|
|
return response_message, response_audio_path |
|
|
|
except Exception as e: |
|
return f"An error occurred: {e}", None |
|
|
|
iface = gr.Interface( |
|
fn=process_audio, |
|
inputs=gr.Audio(type="filepath"), |
|
outputs=[gr.Textbox(label="Response Text (Urdu)"), gr.Audio(label="Response Audio (Urdu)")], |
|
live=True |
|
) |
|
|
|
iface.launch() |
|
|