Spaces:
Sleeping
Sleeping
| """ | |
| Usman's Agent — portfolio chat endpoint powered by Gemini API. | |
| Deploy on a Hugging Face Space (SDK: Docker). | |
| Set GEMINI_API_KEY as a Space secret. | |
| The portfolio frontend POSTs to /chat with { messages: [...], system: "..." } | |
| and expects { reply: "..." }. | |
| """ | |
| import os | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from google import genai | |
| from google.genai import types | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "") | |
| MODEL_ID = os.environ.get("MODEL_ID", "gemini-2.5-flash") | |
| MAX_NEW_TOKENS = int(os.environ.get("MAX_NEW_TOKENS", "1024")) | |
| HISTORY_TURNS = int(os.environ.get("HISTORY_TURNS", "8")) | |
| client = genai.Client(api_key=GEMINI_API_KEY) | |
| app = FastAPI(title="Usman's Agent") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["POST", "GET", "OPTIONS"], | |
| allow_headers=["*"], | |
| ) | |
| class ChatRequest(BaseModel): | |
| messages: list | |
| system: str | None = None | |
| def health(): | |
| return {"status": "ok", "model": MODEL_ID} | |
| def chat(req: ChatRequest): | |
| system = req.system or "" | |
| turns = [m for m in req.messages if m.get("role") in ("user", "assistant")][-HISTORY_TURNS:] | |
| history = [] | |
| for m in turns[:-1]: | |
| role = "model" if m["role"] == "assistant" else "user" | |
| history.append(types.Content(role=role, parts=[types.Part(text=m["content"])])) | |
| last = turns[-1]["content"] if turns else "" | |
| chat_session = client.chats.create( | |
| model=MODEL_ID, | |
| config=types.GenerateContentConfig( | |
| system_instruction=system, | |
| max_output_tokens=MAX_NEW_TOKENS, | |
| temperature=0.7, | |
| ), | |
| history=history, | |
| ) | |
| response = chat_session.send_message(last) | |
| reply = response.text.strip() | |
| return {"reply": reply} | |