Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,42 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
4 |
import pytz
|
|
|
5 |
|
6 |
# Pakistan timezone
|
7 |
pakistan_tz = pytz.timezone("Asia/Karachi")
|
8 |
|
9 |
def chatbot(message):
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
now_pk = datetime.now(pakistan_tz)
|
13 |
-
|
|
|
|
|
14 |
else:
|
15 |
-
return "I can only tell you the current time in Pakistan. Try asking: 'What is the time?'"
|
16 |
|
17 |
iface = gr.Interface(
|
18 |
fn=chatbot,
|
19 |
inputs=gr.Textbox(lines=2, placeholder="Ask me the time in Pakistan..."),
|
20 |
outputs="text",
|
21 |
title="Pakistan Time Bot",
|
22 |
-
description="A simple bot that tells you the current time in Pakistan."
|
23 |
)
|
24 |
|
25 |
if __name__ == "__main__":
|
26 |
-
iface.launch()
|
27 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from datetime import datetime
|
3 |
import pytz
|
4 |
+
import json
|
5 |
|
6 |
# Pakistan timezone
|
7 |
pakistan_tz = pytz.timezone("Asia/Karachi")
|
8 |
|
9 |
def chatbot(message):
|
10 |
+
# Handle JSON input from ESP8266
|
11 |
+
try:
|
12 |
+
# Parse the JSON input
|
13 |
+
data = json.loads(message)
|
14 |
+
|
15 |
+
# Extract the actual message from the "data" array
|
16 |
+
if "data" in data and isinstance(data["data"], list) and len(data["data"]) > 0:
|
17 |
+
user_message = data["data"][0].lower()
|
18 |
+
else:
|
19 |
+
return json.dumps({"data": ["I can only tell you the current time in Pakistan. Try asking: 'What is the time?'"]})
|
20 |
+
except:
|
21 |
+
# If it's not JSON, treat it as a regular string (for direct Gradio interface testing)
|
22 |
+
user_message = message.lower()
|
23 |
+
|
24 |
+
# Process the message
|
25 |
+
if "time" in user_message:
|
26 |
now_pk = datetime.now(pakistan_tz)
|
27 |
+
time_str = f"{now_pk.strftime('%I:%M %p')}"
|
28 |
+
# Return in the expected JSON format
|
29 |
+
return json.dumps({"data": [time_str]})
|
30 |
else:
|
31 |
+
return json.dumps({"data": ["I can only tell you the current time in Pakistan. Try asking: 'What is the time?'"]})
|
32 |
|
33 |
iface = gr.Interface(
|
34 |
fn=chatbot,
|
35 |
inputs=gr.Textbox(lines=2, placeholder="Ask me the time in Pakistan..."),
|
36 |
outputs="text",
|
37 |
title="Pakistan Time Bot",
|
38 |
+
description="A simple bot that tells you the current time in Pakistan. It accepts JSON input for ESP8266 compatibility."
|
39 |
)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
+
iface.launch()
|
|