Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import pytz
|
| 5 |
+
|
| 6 |
+
# Pakistan timezone
|
| 7 |
+
pakistan_tz = pytz.timezone("Asia/Karachi")
|
| 8 |
+
|
| 9 |
+
# ✅ Hugging Face expects this object at global scope
|
| 10 |
+
app = FastAPI(title="Pakistan Time Bot API")
|
| 11 |
+
|
| 12 |
+
# Schema for ESP8266 request
|
| 13 |
+
class RequestData(BaseModel):
|
| 14 |
+
data: list[str]
|
| 15 |
+
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def home():
|
| 18 |
+
return {"message": "🇵🇰 Pakistan Time Bot API is running!"}
|
| 19 |
+
|
| 20 |
+
@app.post("/run/predict")
|
| 21 |
+
async def get_time(request: RequestData):
|
| 22 |
+
# Validate and extract message
|
| 23 |
+
if request.data and isinstance(request.data, list):
|
| 24 |
+
user_message = request.data[0].lower()
|
| 25 |
+
else:
|
| 26 |
+
return {"data": ["Invalid input. Use: {\"data\": [\"time\"]}"]}
|
| 27 |
+
|
| 28 |
+
# Handle time request
|
| 29 |
+
if "time" in user_message:
|
| 30 |
+
now_pk = datetime.now(pakistan_tz)
|
| 31 |
+
time_str = now_pk.strftime("%I:%M %p")
|
| 32 |
+
return {"data": [time_str]}
|
| 33 |
+
else:
|
| 34 |
+
return {"data": ["I can only tell you the current time in Pakistan. Try asking: 'What is the time?'"]}
|