Valtry commited on
Commit
939711f
·
verified ·
1 Parent(s): 6e208f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import re
4
+ from datetime import datetime
5
+ import pytz
6
+
7
+ # -------- METAL PRICE --------
8
+ METALS = {
9
+ "gold": "XAU",
10
+ "silver": "XAG",
11
+ "palladium": "XPD",
12
+ "copper": "HG",
13
+ "platinum": "XPT"
14
+ }
15
+
16
+ OUNCE_TO_GRAM = 31.1035
17
+
18
+ def get_metal_price(query):
19
+ query = query.lower()
20
+
21
+ metal = next((m for m in METALS if m in query), None)
22
+ if not metal:
23
+ return None
24
+
25
+ match = re.search(r"(\d+)\s*gram", query)
26
+ grams = int(match.group(1)) if match else 1
27
+
28
+ symbol = METALS[metal]
29
+
30
+ try:
31
+ price_data = requests.get(f"https://api.gold-api.com/price/{symbol}").json()
32
+ usd_to_inr = requests.get("https://api.frankfurter.app/latest?from=USD&to=INR").json()["rates"]["INR"]
33
+
34
+ per_gram_usd = price_data["price"] / OUNCE_TO_GRAM
35
+ per_gram_inr = per_gram_usd * usd_to_inr
36
+ total = per_gram_inr * grams
37
+
38
+ return f"{metal.capitalize()} 💰\n₹{per_gram_inr:.2f}/g | {grams}g = ₹{total:.2f}"
39
+ except:
40
+ return "Error fetching metal price"
41
+
42
+
43
+ # -------- WEATHER --------
44
+ def get_weather(query):
45
+ match = re.search(r"in ([a-zA-Z ]+)", query)
46
+ if not match:
47
+ return None
48
+
49
+ city = match.group(1)
50
+
51
+ try:
52
+ geo = requests.get(f"https://geocoding-api.open-meteo.com/v1/search?name={city}").json()
53
+ if "results" not in geo:
54
+ return "City not found"
55
+
56
+ lat = geo["results"][0]["latitude"]
57
+ lon = geo["results"][0]["longitude"]
58
+
59
+ weather = requests.get(
60
+ f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true"
61
+ ).json()["current_weather"]
62
+
63
+ return f"{city} 🌦️\nTemp: {weather['temperature']}°C\nWind: {weather['windspeed']} km/h"
64
+ except:
65
+ return "Error fetching weather"
66
+
67
+
68
+ # -------- TIME --------
69
+ def get_time(query):
70
+ match = re.search(r"in ([a-zA-Z ]+)", query)
71
+ if not match:
72
+ return None
73
+
74
+ city = match.group(1)
75
+
76
+ try:
77
+ geo = requests.get(f"https://geocoding-api.open-meteo.com/v1/search?name={city}").json()
78
+ tz = geo["results"][0]["timezone"]
79
+
80
+ now = datetime.now(pytz.timezone(tz))
81
+ return f"{city} 🕒\n{now.strftime('%Y-%m-%d %H:%M:%S')}"
82
+ except:
83
+ return "Error fetching time"
84
+
85
+
86
+ # -------- CRYPTO --------
87
+ def get_crypto(query):
88
+ name = query.split()[-1].lower()
89
+
90
+ try:
91
+ data = requests.get(
92
+ f"https://api.coingecko.com/api/v3/simple/price?ids={name}&vs_currencies=inr"
93
+ ).json()
94
+
95
+ if name not in data:
96
+ return None
97
+
98
+ return f"{name.capitalize()} 🪙 ₹{data[name]['inr']}"
99
+ except:
100
+ return "Error fetching crypto"
101
+
102
+
103
+ # -------- JOKE --------
104
+ def get_joke(query):
105
+ if "joke" not in query.lower():
106
+ return None
107
+
108
+ try:
109
+ data = requests.get("https://official-joke-api.appspot.com/random_joke").json()
110
+ return f"{data['setup']} 😂 {data['punchline']}"
111
+ except:
112
+ return "Error fetching joke"
113
+
114
+
115
+ # -------- MAIN ROUTER --------
116
+ def chatbot(query):
117
+ query = query.lower()
118
+
119
+ # Multi-step support
120
+ responses = []
121
+
122
+ for func in [get_metal_price, get_weather, get_time, get_crypto, get_joke]:
123
+ result = func(query)
124
+ if result:
125
+ responses.append(result)
126
+
127
+ if not responses:
128
+ return "Sorry, I couldn't understand. Try asking about gold, weather, time, crypto, or jokes."
129
+
130
+ return "\n\n".join(responses)
131
+
132
+
133
+ # -------- UI --------
134
+ gr.Interface(
135
+ fn=chatbot,
136
+ inputs="text",
137
+ outputs="text",
138
+ title="🔥 Multi-API AI Bot",
139
+ description="Ask about gold price, weather, time, crypto, or jokes!"
140
+ ).launch()