| import gradio as gr |
| import requests |
| import re |
| from datetime import datetime |
| import pytz |
|
|
| |
| METALS = { |
| "gold": "XAU", |
| "silver": "XAG", |
| "palladium": "XPD", |
| "copper": "HG", |
| "platinum": "XPT" |
| } |
|
|
| OUNCE_TO_GRAM = 31.1035 |
|
|
| def get_metal_price(query): |
| query = query.lower() |
|
|
| metal = next((m for m in METALS if m in query), None) |
| if not metal: |
| return None |
|
|
| match = re.search(r"(\d+)\s*gram", query) |
| grams = int(match.group(1)) if match else 1 |
|
|
| symbol = METALS[metal] |
|
|
| try: |
| price_data = requests.get(f"https://api.gold-api.com/price/{symbol}").json() |
| usd_to_inr = requests.get("https://api.frankfurter.app/latest?from=USD&to=INR").json()["rates"]["INR"] |
|
|
| per_gram_usd = price_data["price"] / OUNCE_TO_GRAM |
| per_gram_inr = per_gram_usd * usd_to_inr |
| total = per_gram_inr * grams |
|
|
| return f"{metal.capitalize()} 💰\n₹{per_gram_inr:.2f}/g | {grams}g = ₹{total:.2f}" |
| except: |
| return "Error fetching metal price" |
|
|
|
|
| |
| def get_weather(query): |
| match = re.search(r"in ([a-zA-Z ]+)", query) |
| if not match: |
| return None |
|
|
| city = match.group(1) |
|
|
| try: |
| geo = requests.get(f"https://geocoding-api.open-meteo.com/v1/search?name={city}").json() |
| if "results" not in geo: |
| return "City not found" |
|
|
| lat = geo["results"][0]["latitude"] |
| lon = geo["results"][0]["longitude"] |
|
|
| weather = requests.get( |
| f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true" |
| ).json()["current_weather"] |
|
|
| return f"{city} 🌦️\nTemp: {weather['temperature']}°C\nWind: {weather['windspeed']} km/h" |
| except: |
| return "Error fetching weather" |
|
|
|
|
| |
| def get_time(query): |
| match = re.search(r"in ([a-zA-Z ]+)", query) |
| if not match: |
| return None |
|
|
| city = match.group(1) |
|
|
| try: |
| geo = requests.get(f"https://geocoding-api.open-meteo.com/v1/search?name={city}").json() |
| tz = geo["results"][0]["timezone"] |
|
|
| now = datetime.now(pytz.timezone(tz)) |
| return f"{city} 🕒\n{now.strftime('%Y-%m-%d %H:%M:%S')}" |
| except: |
| return "Error fetching time" |
|
|
|
|
| |
| def get_crypto(query): |
| name = query.split()[-1].lower() |
|
|
| try: |
| data = requests.get( |
| f"https://api.coingecko.com/api/v3/simple/price?ids={name}&vs_currencies=inr" |
| ).json() |
|
|
| if name not in data: |
| return None |
|
|
| return f"{name.capitalize()} 🪙 ₹{data[name]['inr']}" |
| except: |
| return "Error fetching crypto" |
|
|
|
|
| |
| def get_joke(query): |
| if "joke" not in query.lower(): |
| return None |
|
|
| try: |
| data = requests.get("https://official-joke-api.appspot.com/random_joke").json() |
| return f"{data['setup']} 😂 {data['punchline']}" |
| except: |
| return "Error fetching joke" |
|
|
|
|
| |
| def chatbot(query): |
| query = query.lower() |
|
|
| |
| responses = [] |
|
|
| for func in [get_metal_price, get_weather, get_time, get_crypto, get_joke]: |
| result = func(query) |
| if result: |
| responses.append(result) |
|
|
| if not responses: |
| return "Sorry, I couldn't understand. Try asking about gold, weather, time, crypto, or jokes." |
|
|
| return "\n\n".join(responses) |
|
|
|
|
| |
| gr.Interface( |
| fn=chatbot, |
| inputs="text", |
| outputs="text", |
| title="🔥 Multi-API AI Bot", |
| description="Ask about gold price, weather, time, crypto, or jokes!" |
| ).launch() |