ishans24 commited on
Commit
cdddaf1
·
verified ·
1 Parent(s): 0602bb3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ from fastapi.responses import PlainTextResponse
3
+ import os
4
+ import requests
5
+
6
+ # Default model used internally; the client only sends plain text
7
+ DEFAULT_MODEL = "kwaipilot/kat-coder-pro:free"
8
+
9
+ app = FastAPI()
10
+
11
+ @app.get("/")
12
+ def health():
13
+ return {"status": "ok"}
14
+
15
+ @app.post("/chat")
16
+ async def chat(request: Request):
17
+ # Accept plain text body; fallback to JSON {"text": "..."}
18
+ body = await request.body()
19
+ text = (body or b"").decode().strip()
20
+ if not text:
21
+ try:
22
+ data = await request.json()
23
+ text = str(data.get("text", "")).strip()
24
+ except Exception:
25
+ pass
26
+ if not text:
27
+ raise HTTPException(status_code=400, detail="Send plain text body or JSON {'text': '...'}")
28
+
29
+ api_key = os.getenv("OPENROUTER_API_KEY")
30
+ if not api_key:
31
+ raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")
32
+
33
+ try:
34
+ r = requests.post(
35
+ "https://openrouter.ai/api/v1/chat/completions", # OpenRouter chat completions [web:21]
36
+ headers={
37
+ "Authorization": f"Bearer {api_key}",
38
+ "Content-Type": "application/json",
39
+ },
40
+ json={
41
+ "model": DEFAULT_MODEL,
42
+ "messages": [{"role": "user", "content": text}],
43
+ },
44
+ timeout=60,
45
+ )
46
+ except requests.RequestException as e:
47
+ raise HTTPException(status_code=502, detail=str(e))
48
+
49
+ # Try to extract assistant text; if not possible, return the raw body from OpenRouter
50
+ content = None
51
+ try:
52
+ data = r.json()
53
+ if r.status_code >= 400:
54
+ return PlainTextResponse(str(data), status_code=r.status_code)
55
+ content = data["choices"][0]["message"]["content"]
56
+ except Exception:
57
+ # Fall back to raw text (may be JSON or error text)
58
+ return PlainTextResponse(r.text, status_code=r.status_code)
59
+
60
+ return PlainTextResponse(content)