File size: 3,526 Bytes
78ad284
 
 
 
 
e444d5a
 
78ad284
e444d5a
78ad284
 
 
 
e444d5a
78ad284
 
 
 
e444d5a
78ad284
 
e444d5a
78ad284
 
e444d5a
78ad284
e444d5a
78ad284
 
 
 
 
 
 
 
 
e444d5a
78ad284
 
 
 
 
 
 
 
 
 
e444d5a
78ad284
 
 
 
 
 
 
 
 
e444d5a
78ad284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e444d5a
78ad284
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import gradio as gr
import whisper
from gtts import gTTS
import io
import requests
from groq import Groq
import time

# Ensure GROQ_API_KEY and OpenWeather API key are defined
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.")

# Initialize the Groq client
client = Groq(api_key=GROQ_API_KEY)

# Load the Whisper model
model = whisper.load_model("base")  # Ensure this model supports Urdu; otherwise, choose a suitable model

def fetch_weather():
    try:
        response = requests.get(
            OPENWEATHER_URL,
            params={
                'q': 'London',  # Change to dynamic city name as needed
                '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:
        # Load the audio file
        audio = whisper.load_audio(file_path)

        # Transcribe the audio using Whisper (specify language if needed)
        result = model.transcribe(audio, language="ur")  # Specify 'ur' for Urdu
        text = result["text"]

        # Check if the text contains any weather-related keywords
        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:
            # Generate a response in Urdu using Groq
            chat_completion = client.chat.completions.create(
                messages=[{"role": "user", "content": text}],
                model="gemma2-9b-it",  # Ensure this model can handle Urdu
            )
            # Access the response using dot notation
            response_message = chat_completion.choices[0].message.content.strip()

        # Convert the response text to Urdu speech
        tts = gTTS(response_message, lang='ur')  # Specify language 'ur' for Urdu
        response_audio_io = io.BytesIO()
        tts.write_to_fp(response_audio_io)  # Save the audio to the BytesIO object
        response_audio_io.seek(0)

        # Generate a unique filename
        response_audio_path = "response_" + str(int(time.time())) + ".mp3"
        
        # Save audio to a file
        with open(response_audio_path, "wb") as audio_file:
            audio_file.write(response_audio_io.getvalue())

        # Return the response text and the path to the saved audio file
        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"),  # Use type="filepath"
    outputs=[gr.Textbox(label="Response Text (Urdu)"), gr.Audio(label="Response Audio (Urdu)")],
    live=True  # Set to False if you do not need real-time updates
)

iface.launch()