AI_Tour / utils.py
Sefat33's picture
Update utils.py
44a1d60 verified
import requests
import whisper
import tempfile
# Whisper model (STT)
whisper_model = whisper.load_model("base")
def speech_to_text(audio):
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
tmp.write(audio)
result = whisper_model.transcribe(tmp.name)
return result["text"]
# Live Currency API
def get_currency_info(country):
try:
response = requests.get(f"https://restcountries.com/v3.1/name/{country}")
data = response.json()[0]
currency_code = list(data['currencies'].keys())[0]
return f"{currency_code} - {data['currencies'][currency_code]['name']}"
except:
return "Currency info unavailable."
# Weather via OpenWeatherMap (You must set your API key)
def get_weather(city, api_key="YOUR_OPENWEATHERMAP_API_KEY"):
try:
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
res = requests.get(url).json()
temp = res['main']['temp']
condition = res['weather'][0]['description']
return f"Weather in {city}: {temp}°C, {condition}"
except:
return "Weather info unavailable."