import os,time,subprocess,requests from fastapi import FastAPI,Request from fastapi.responses import JSONResponse import uvicorn T=os.getenv("TELEGRAM_BOT_TOKEN","AAGgNISzjysyQzOWzTaE4Y4MKlQ8_Cz5YCI") S=os.getenv("BOT_SYSTEM","You are El Celacho. Reply helpfully and briefly.") U=os.getenv("PUBLIC_URL","https://huggingface.co/spaces/Santiagoismo/CelachoBot") P=int(os.getenv("PORT","7860")) MP=os.getenv("MODEL_PATH","/app/models/Qwen3.5-0.8B-Q8_0.gguf") LPORT=8081 app=FastAPI() ready=False def tg(m,**kw): return requests.post(f"https://api.telegram.org/bot{T}/{m}",json=kw,timeout=60) def ask(msg): r=requests.post( f"http://127.0.0.1:{LPORT}/v1/chat/completions", json={ "messages":[ {"role":"system","content":S}, {"role":"user","content":msg} ], "temperature":0.7, "max_tokens":200 }, timeout=180 ) r.raise_for_status() return r.json()["choices"][0]["message"]["content"].strip() def boot(): global ready if not os.path.exists(MP): raise FileNotFoundError(MP) subprocess.Popen([ "llama-server", "-m",MP, "--host","127.0.0.1", "--port",str(LPORT), "-ngl","0", "-c","2048" ]) for _ in range(120): try: requests.get(f"http://127.0.0.1:{LPORT}/health",timeout=2) ready=True break except Exception: time.sleep(2) if T and U: try: requests.get( f"https://api.telegram.org/bot{T}/setWebhook", params={"url":U.rstrip("/")+"/webhook"}, timeout=30 ) except Exception: pass @app.on_event("startup") def startup(): boot() @app.get("/health") def health(): return {"ok":True,"model_ready":ready} @app.post("/webhook") async def webhook(req:Request): try: u=await req.json() m=u.get("message",{}) c=m.get("chat",{}).get("id") t=m.get("text") if not c: return {"ok":True} if not t: tg("sendMessage",chat_id=c,text="Send text only.") return {"ok":True} if not ready: tg("sendMessage",chat_id=c,text="Model waking up. Retry in a moment.") return {"ok":True} a=ask(t) tg("sendMessage",chat_id=c,text=(a or "...")[:4000]) except Exception: pass return JSONResponse({"ok":True}) if __name__=="__main__": uvicorn.run(app,host="0.0.0.0",port=P)