Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
|
| 8 |
+
# =========================
|
| 9 |
+
# Config
|
| 10 |
+
# =========================
|
| 11 |
+
|
| 12 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 13 |
+
DEFAULT_MODEL = "gemma-3-4b-it"
|
| 14 |
+
|
| 15 |
+
if not GEMINI_API_KEY:
|
| 16 |
+
raise RuntimeError("GEMINI_API_KEY is not set in environment variables.")
|
| 17 |
+
|
| 18 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 19 |
+
|
| 20 |
+
app = FastAPI(title="Gemma FastAPI", version="1.0.0")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# =========================
|
| 24 |
+
# Request / Response Models
|
| 25 |
+
# =========================
|
| 26 |
+
|
| 27 |
+
class ChatRequest(BaseModel):
|
| 28 |
+
message: str
|
| 29 |
+
system_prompt: Optional[str] = "أنت مساعد عربي مفيد. أجب بشكل واضح ومباشر."
|
| 30 |
+
model: Optional[str] = DEFAULT_MODEL
|
| 31 |
+
temperature: Optional[float] = 0.3
|
| 32 |
+
max_output_tokens: Optional[int] = 300
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ChatResponse(BaseModel):
|
| 36 |
+
success: bool
|
| 37 |
+
model: str
|
| 38 |
+
input_message: str
|
| 39 |
+
reply: str
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# =========================
|
| 43 |
+
# Helper
|
| 44 |
+
# =========================
|
| 45 |
+
|
| 46 |
+
def generate_reply(
|
| 47 |
+
user_message: str,
|
| 48 |
+
system_prompt: str,
|
| 49 |
+
model_name: str,
|
| 50 |
+
temperature: float,
|
| 51 |
+
max_output_tokens: int
|
| 52 |
+
) -> str:
|
| 53 |
+
prompt = f"{system_prompt}\n\nUser: {user_message}\nAssistant:"
|
| 54 |
+
|
| 55 |
+
generation_config = genai.types.GenerationConfig(
|
| 56 |
+
temperature=temperature,
|
| 57 |
+
max_output_tokens=max_output_tokens,
|
| 58 |
+
top_p=0.95,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
model = genai.GenerativeModel(model_name)
|
| 62 |
+
|
| 63 |
+
response = model.generate_content(
|
| 64 |
+
prompt,
|
| 65 |
+
generation_config=generation_config
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
return response.text.strip()
|
| 70 |
+
except Exception:
|
| 71 |
+
return "Model returned an empty response."
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# =========================
|
| 75 |
+
# Routes
|
| 76 |
+
# =========================
|
| 77 |
+
|
| 78 |
+
@app.get("/")
|
| 79 |
+
def home():
|
| 80 |
+
return {"status": "ok", "message": "Gemma API is running"}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 84 |
+
def chat(req: ChatRequest):
|
| 85 |
+
if not req.message or not req.message.strip():
|
| 86 |
+
raise HTTPException(status_code=400, detail="message is required")
|
| 87 |
+
|
| 88 |
+
print("\n========== NEW REQUEST ==========")
|
| 89 |
+
print("Incoming message:")
|
| 90 |
+
print(req.message)
|
| 91 |
+
print("Model:", req.model)
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
reply = generate_reply(
|
| 95 |
+
user_message=req.message,
|
| 96 |
+
system_prompt=req.system_prompt or "أنت مساعد مفيد.",
|
| 97 |
+
model_name=req.model or DEFAULT_MODEL,
|
| 98 |
+
temperature=req.temperature if req.temperature is not None else 0.3,
|
| 99 |
+
max_output_tokens=req.max_output_tokens if req.max_output_tokens is not None else 300
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
print("\nModel reply:")
|
| 103 |
+
print(reply)
|
| 104 |
+
print("=================================\n")
|
| 105 |
+
|
| 106 |
+
return ChatResponse(
|
| 107 |
+
success=True,
|
| 108 |
+
model=req.model or DEFAULT_MODEL,
|
| 109 |
+
input_message=req.message,
|
| 110 |
+
reply=reply
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print("\nERROR:")
|
| 115 |
+
print(str(e))
|
| 116 |
+
print("=================================\n")
|
| 117 |
+
raise HTTPException(status_code=500, detail=str(e))
|