File size: 1,037 Bytes
8c62e46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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?'"]}