Abbas0786 commited on
Commit
4f3c0da
·
verified ·
1 Parent(s): 7459d0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -6
app.py CHANGED
@@ -5,11 +5,14 @@ from gtts import gTTS
5
  import io
6
  from groq import Groq
7
  import time
 
8
 
9
- # Ensure GROQ_API_KEY is defined
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 +20,39 @@ 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
@@ -35,8 +71,14 @@ def process_audio(file_path):
35
  # Access the response using dot notation
36
  response_message = chat_completion.choices[0].message.content.strip()
37
 
 
 
 
 
 
 
38
  # Convert the response text to Urdu speech
39
- tts = gTTS(response_message, lang='ur') # Specify language 'ur' for Urdu
40
  response_audio_io = io.BytesIO()
41
  tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
42
  response_audio_io.seek(0)
@@ -49,7 +91,7 @@ def process_audio(file_path):
49
  audio_file.write(response_audio_io.getvalue())
50
 
51
  # Return the response text and the path to the saved audio file
52
- return response_message, response_audio_path
53
 
54
  except Exception as e:
55
  return f"An error occurred: {e}", None
 
5
  import io
6
  from groq import Groq
7
  import time
8
+ import requests
9
 
10
+ # Ensure GROQ_API_KEY and GOOGLE_CLOUD_API_KEY are defined
11
+ GROQ_API_KEY = "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"
12
+ GOOGLE_CLOUD_API_KEY = "AIzaSyBP5XGE4ZtrH2nBJZb7qe3Pjhw61rQvjBM"
13
+
14
+ if not GROQ_API_KEY or not GOOGLE_CLOUD_API_KEY:
15
+ raise ValueError("API keys are not set.")
16
 
17
  # Initialize the Groq client
18
  client = Groq(api_key=GROQ_API_KEY)
 
20
  # Load the Whisper model
21
  model = whisper.load_model("base") # Ensure this model supports Urdu; otherwise, choose a suitable model
22
 
23
+ def get_weather_data(location):
24
+ """Fetch real-time weather data for a given location."""
25
+ try:
26
+ # Example endpoint for Google Maps Geocoding API
27
+ endpoint = f"https://maps.googleapis.com/maps/api/geocode/json"
28
+ params = {
29
+ "address": location,
30
+ "key": GOOGLE_CLOUD_API_KEY
31
+ }
32
+ response = requests.get(endpoint, params=params)
33
+ data = response.json()
34
+
35
+ if data["status"] == "OK":
36
+ # Extract latitude and longitude
37
+ lat = data["results"][0]["geometry"]["location"]["lat"]
38
+ lng = data["results"][0]["geometry"]["location"]["lng"]
39
+
40
+ # Fetch weather data (example using another weather API)
41
+ weather_endpoint = f"http://api.weatherapi.com/v1/current.json"
42
+ weather_params = {
43
+ "key": "aa4db8152e46c2f3fb19fad5d58a0ed8", # Replace with your actual weather API key
44
+ "q": f"{lat},{lng}"
45
+ }
46
+ weather_response = requests.get(weather_endpoint, params=weather_params)
47
+ weather_data = weather_response.json()
48
+
49
+ return f"Temperature: {weather_data['current']['temp_c']}°C, Condition: {weather_data['current']['condition']['text']}"
50
+ else:
51
+ return "Weather data not available for the specified location."
52
+
53
+ except Exception as e:
54
+ return f"An error occurred while fetching weather data: {e}"
55
+
56
  def process_audio(file_path):
57
  try:
58
  # Load the audio file
 
71
  # Access the response using dot notation
72
  response_message = chat_completion.choices[0].message.content.strip()
73
 
74
+ # Get weather data based on the text (assuming it includes location information)
75
+ weather_data = get_weather_data(text) # You may need to refine how location is extracted
76
+
77
+ # Append weather data to the response
78
+ full_response_message = f"{response_message}\n\n{weather_data}"
79
+
80
  # Convert the response text to Urdu speech
81
+ tts = gTTS(full_response_message, lang='ur') # Specify language 'ur' for Urdu
82
  response_audio_io = io.BytesIO()
83
  tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
84
  response_audio_io.seek(0)
 
91
  audio_file.write(response_audio_io.getvalue())
92
 
93
  # Return the response text and the path to the saved audio file
94
+ return full_response_message, response_audio_path
95
 
96
  except Exception as e:
97
  return f"An error occurred: {e}", None