pk-time / main.py
abdullahalioo's picture
Create main.py
8c62e46 verified
from fastapi import FastAPI
from pydantic import BaseModel
from datetime import datetime
import pytz
# Pakistan timezone
pakistan_tz = pytz.timezone("Asia/Karachi")
# βœ… Hugging Face expects this object at global scope
app = FastAPI(title="Pakistan Time Bot API")
# Schema for ESP8266 request
class RequestData(BaseModel):
data: list[str]
@app.get("/")
def home():
return {"message": "πŸ‡΅πŸ‡° Pakistan Time Bot API is running!"}
@app.post("/run/predict")
async def get_time(request: RequestData):
# Validate and extract message
if request.data and isinstance(request.data, list):
user_message = request.data[0].lower()
else:
return {"data": ["Invalid input. Use: {\"data\": [\"time\"]}"]}
# Handle time request
if "time" in user_message:
now_pk = datetime.now(pakistan_tz)
time_str = now_pk.strftime("%I:%M %p")
return {"data": [time_str]}
else:
return {"data": ["I can only tell you the current time in Pakistan. Try asking: 'What is the time?'"]}