Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1 +1,33 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import whisper
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
# Whisper model (STT)
|
| 6 |
+
whisper_model = whisper.load_model("base")
|
| 7 |
+
|
| 8 |
+
def speech_to_text(audio):
|
| 9 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 10 |
+
tmp.write(audio)
|
| 11 |
+
result = whisper_model.transcribe(tmp.name)
|
| 12 |
+
return result["text"]
|
| 13 |
+
|
| 14 |
+
# Live Currency API
|
| 15 |
+
def get_currency_info(country):
|
| 16 |
+
try:
|
| 17 |
+
response = requests.get(f"https://restcountries.com/v3.1/name/{country}")
|
| 18 |
+
data = response.json()[0]
|
| 19 |
+
currency_code = list(data['currencies'].keys())[0]
|
| 20 |
+
return f"{currency_code} - {data['currencies'][currency_code]['name']}"
|
| 21 |
+
except:
|
| 22 |
+
return "Currency info unavailable."
|
| 23 |
+
|
| 24 |
+
# Weather via OpenWeatherMap (You must set your API key)
|
| 25 |
+
def get_weather(city, api_key="YOUR_OPENWEATHERMAP_API_KEY"):
|
| 26 |
+
try:
|
| 27 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 28 |
+
res = requests.get(url).json()
|
| 29 |
+
temp = res['main']['temp']
|
| 30 |
+
condition = res['weather'][0]['description']
|
| 31 |
+
return f"Weather in {city}: {temp}°C, {condition}"
|
| 32 |
+
except:
|
| 33 |
+
return "Weather info unavailable."
|