Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,71 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
APP = FastAPI()
|
| 5 |
|
|
|
|
| 6 |
class ChatRequest(BaseModel):
|
| 7 |
message: str
|
| 8 |
-
username: str
|
|
|
|
| 9 |
|
| 10 |
@APP.get("/health")
|
| 11 |
async def health():
|
| 12 |
-
return {"ok": True
|
|
|
|
| 13 |
|
| 14 |
@APP.post("/chat/send")
|
| 15 |
-
async def chat_send(
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
reply = f"Socrates says to {username}: {message[::-1]}"
|
| 20 |
-
return {"reply": reply, "user_id": "dev-user"}
|
| 21 |
|
| 22 |
|
|
|
|
| 1 |
+
# app.py (for Hugging Face Docker space)
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI, Header, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from typing import Optional
|
| 6 |
+
|
| 7 |
+
from socrates_core import socrates_reply
|
| 8 |
+
from supabase_ie import get_user_profile_by_username # you may need to implement this
|
| 9 |
|
| 10 |
APP = FastAPI()
|
| 11 |
|
| 12 |
+
|
| 13 |
class ChatRequest(BaseModel):
|
| 14 |
message: str
|
| 15 |
+
username: str # e.g. "user_lollo632"
|
| 16 |
+
|
| 17 |
|
| 18 |
@APP.get("/health")
|
| 19 |
async def health():
|
| 20 |
+
return {"ok": True}
|
| 21 |
+
|
| 22 |
|
| 23 |
@APP.post("/chat/send")
|
| 24 |
+
async def chat_send(
|
| 25 |
+
body: ChatRequest,
|
| 26 |
+
authorization: Optional[str] = Header(default=None),
|
| 27 |
+
):
|
| 28 |
+
"""
|
| 29 |
+
Main endpoint used by the mobile app.
|
| 30 |
+
For now:
|
| 31 |
+
- Use username to find user_id + profile in Supabase.
|
| 32 |
+
- Ignore JWT verification (authorization header) or just log it.
|
| 33 |
+
Later:
|
| 34 |
+
- Verify JWT, extract user_id directly from token.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
message = body.message.strip()
|
| 38 |
+
username = body.username.strip()
|
| 39 |
+
|
| 40 |
+
if not message:
|
| 41 |
+
raise HTTPException(status_code=400, detail="Empty message")
|
| 42 |
+
|
| 43 |
+
# TODO (later): parse/verify JWT if you want:
|
| 44 |
+
# if authorization and authorization.startswith("Bearer "):
|
| 45 |
+
# token = authorization.split(" ", 1)[1]
|
| 46 |
+
# user_id = verify_and_get_user_id_from_token(token)
|
| 47 |
+
# else:
|
| 48 |
+
# ...
|
| 49 |
+
|
| 50 |
+
# For now, derive user_id from username via Supabase (simple helper)
|
| 51 |
+
try:
|
| 52 |
+
# You can write this helper to look up user_profiles where username == body.username
|
| 53 |
+
profile, user_id = get_user_profile_by_username(username)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
# fallback: use username as id if lookup fails (dev only)
|
| 56 |
+
user_id = username
|
| 57 |
+
profile = {}
|
| 58 |
+
|
| 59 |
+
# Call the core Socrates pipeline
|
| 60 |
+
reply_text = socrates_reply(
|
| 61 |
+
user_id=user_id,
|
| 62 |
+
username=username,
|
| 63 |
+
user_msg=message,
|
| 64 |
+
profile=profile,
|
| 65 |
+
ui_lang="en", # or from profile / user_ui_language table later
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return {"reply": reply_text}
|
| 69 |
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|