Spaces:
Sleeping
Sleeping
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] | |
def home(): | |
return {"message": "π΅π° Pakistan Time Bot API is running!"} | |
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?'"]} | |