Update run.py
Browse files
run.py
CHANGED
|
@@ -1,90 +1,221 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
run.py - FastAPI backend + frontend server for GAKR AI
|
| 4 |
+
|
| 5 |
+
Features:
|
| 6 |
+
- Serves templates/chat.html at "/"
|
| 7 |
+
- /api/analyze:
|
| 8 |
+
- prompt: required (must not be empty)
|
| 9 |
+
- files: optional list[UploadFile] (0, 1, many; any type)
|
| 10 |
+
- api_key: required
|
| 11 |
+
- If files are present β file-analysis mode (different system prompt)
|
| 12 |
+
- If no files β general assistant mode
|
| 13 |
+
- Detailed exceptions and logs for easier debugging
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from typing import List, Optional
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
import traceback
|
| 20 |
+
|
| 21 |
+
from fastapi import (
|
| 22 |
+
FastAPI,
|
| 23 |
+
HTTPException,
|
| 24 |
+
Form,
|
| 25 |
+
UploadFile,
|
| 26 |
+
File,
|
| 27 |
+
Request,
|
| 28 |
+
)
|
| 29 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 30 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 31 |
+
from fastapi.templating import Jinja2Templates
|
| 32 |
+
import uvicorn
|
| 33 |
+
|
| 34 |
+
from load_model import init_model
|
| 35 |
+
from generate import generate_response
|
| 36 |
+
from file_pipeline import process_files
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ============================================================
|
| 40 |
+
# APP SETUP
|
| 41 |
+
# ============================================================
|
| 42 |
+
|
| 43 |
+
app = FastAPI(title="GAKR AI")
|
| 44 |
+
|
| 45 |
+
# Templates (chat.html lives in ./templates)
|
| 46 |
+
templates = Jinja2Templates(directory="templates")
|
| 47 |
+
|
| 48 |
+
# CORS (open for dev; restrict origins in production)
|
| 49 |
+
app.add_middleware(
|
| 50 |
+
CORSMiddleware,
|
| 51 |
+
allow_origins=["*"], # change to specific origin in production
|
| 52 |
+
allow_credentials=True,
|
| 53 |
+
allow_methods=["*"],
|
| 54 |
+
allow_headers=["*"],
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# API Key
|
| 58 |
+
API_KEY = "gakr-ai-2025-secret"
|
| 59 |
+
|
| 60 |
+
# Load model ONCE at startup
|
| 61 |
+
print("π Starting GAKR AI Backend...")
|
| 62 |
+
try:
|
| 63 |
+
model, tokenizer = init_model(".")
|
| 64 |
+
print("β
Model initialized successfully")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print("β Failed to load model at startup:")
|
| 67 |
+
traceback.print_exc()
|
| 68 |
+
raise e
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# ============================================================
|
| 72 |
+
# ROUTES
|
| 73 |
+
# ============================================================
|
| 74 |
+
|
| 75 |
+
@app.get("/", response_class=HTMLResponse)
|
| 76 |
+
async def home(request: Request):
|
| 77 |
+
"""
|
| 78 |
+
Serve chat.html as homepage.
|
| 79 |
+
"""
|
| 80 |
+
try:
|
| 81 |
+
return templates.TemplateResponse("chat.html", {"request": request})
|
| 82 |
+
except Exception as e:
|
| 83 |
+
# If template not found or other template error
|
| 84 |
+
traceback.print_exc()
|
| 85 |
+
raise HTTPException(
|
| 86 |
+
status_code=500,
|
| 87 |
+
detail=f"Failed to render chat.html: {str(e)}",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
@app.post("/api/analyze")
|
| 92 |
+
async def analyze_endpoint(
|
| 93 |
+
prompt: str = Form(...), # required
|
| 94 |
+
api_key: str = Form(...),
|
| 95 |
+
files: Optional[List[UploadFile]] = File(None), # optional
|
| 96 |
+
):
|
| 97 |
+
"""
|
| 98 |
+
Main analysis endpoint.
|
| 99 |
+
|
| 100 |
+
Cases:
|
| 101 |
+
- prompt only (no files) β general assistant mode
|
| 102 |
+
- prompt + one/many files β file-analysis mode (uses file context)
|
| 103 |
+
"""
|
| 104 |
+
try:
|
| 105 |
+
# ---------- Basic validation ----------
|
| 106 |
+
if api_key != API_KEY:
|
| 107 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 108 |
+
|
| 109 |
+
if prompt is None:
|
| 110 |
+
raise HTTPException(status_code=400, detail="Prompt is missing")
|
| 111 |
+
if not prompt.strip():
|
| 112 |
+
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
| 113 |
+
|
| 114 |
+
files = files or []
|
| 115 |
+
|
| 116 |
+
# ---------- Branch by presence of files ----------
|
| 117 |
+
if files:
|
| 118 |
+
# ----- CASE 1: prompt + files -----
|
| 119 |
+
try:
|
| 120 |
+
context = await process_files(files)
|
| 121 |
+
except Exception as extract_err:
|
| 122 |
+
traceback.print_exc()
|
| 123 |
+
raise HTTPException(
|
| 124 |
+
status_code=500,
|
| 125 |
+
detail=f"Error while processing uploaded files: {str(extract_err)}",
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
context_text = json.dumps(context, indent=2, ensure_ascii=False)
|
| 129 |
+
combined_user_prompt = f"""
|
| 130 |
+
User question:
|
| 131 |
+
{prompt}
|
| 132 |
+
|
| 133 |
+
Below is structured information extracted from the user's uploaded files.
|
| 134 |
+
The extraction was done by automated tools.
|
| 135 |
+
|
| 136 |
+
Your task:
|
| 137 |
+
1. Answer the user's question as accurately as possible.
|
| 138 |
+
2. Use the context when it is relevant.
|
| 139 |
+
3. Highlight important patterns, risks, or opportunities.
|
| 140 |
+
4. If some information is missing or uncertain, say so honestly.
|
| 141 |
+
|
| 142 |
+
Context:
|
| 143 |
+
{context_text}
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
system_prompt = (
|
| 147 |
+
"You are GAKR AI, a careful and honest analysis assistant that works with "
|
| 148 |
+
"structured summaries of files (tables, PDFs, documents, images, audio, video, etc.). "
|
| 149 |
+
"You never assume file contents beyond what the provided context states."
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
else:
|
| 153 |
+
# ----- CASE 2: prompt only -----
|
| 154 |
+
context = {"files": []} # keep structure consistent
|
| 155 |
+
combined_user_prompt = prompt
|
| 156 |
+
|
| 157 |
+
system_prompt = (
|
| 158 |
+
"You are GAKR AI, a helpful and honest general-purpose assistant. "
|
| 159 |
+
"You answer questions, explain concepts, help with reasoning and coding, "
|
| 160 |
+
"using your knowledge up to 2024. Be clear, concise, and direct."
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# ---------- Generate with Phiβ3 ----------
|
| 164 |
+
try:
|
| 165 |
+
response_text = generate_response(
|
| 166 |
+
user_prompt=combined_user_prompt,
|
| 167 |
+
system_prompt=system_prompt,
|
| 168 |
+
max_tokens=512,
|
| 169 |
+
temperature=0.2,
|
| 170 |
+
)
|
| 171 |
+
except Exception as gen_err:
|
| 172 |
+
traceback.print_exc()
|
| 173 |
+
raise HTTPException(
|
| 174 |
+
status_code=500,
|
| 175 |
+
detail=f"Error during model generation: {str(gen_err)}",
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
return JSONResponse(
|
| 179 |
+
{
|
| 180 |
+
"response": response_text,
|
| 181 |
+
"context": context,
|
| 182 |
+
"status": "success",
|
| 183 |
+
}
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
except HTTPException:
|
| 187 |
+
# Let FastAPI handle HTTPException as-is
|
| 188 |
+
raise
|
| 189 |
+
except Exception as e:
|
| 190 |
+
# Unexpected error: log full traceback and return 500
|
| 191 |
+
traceback.print_exc()
|
| 192 |
+
raise HTTPException(
|
| 193 |
+
status_code=500,
|
| 194 |
+
detail=f"Unexpected backend error: {str(e)}",
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
@app.get("/health")
|
| 199 |
+
async def health_check():
|
| 200 |
+
"""
|
| 201 |
+
Simple health check.
|
| 202 |
+
"""
|
| 203 |
+
return {"status": "healthy", "model_loaded": True}
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# ============================================================
|
| 207 |
+
# ENTRY POINT
|
| 208 |
+
# ============================================================
|
| 209 |
+
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
+
print("\n" + "=" * 60)
|
| 212 |
+
print("π SERVER & CHAT LOCATION")
|
| 213 |
+
print("=" * 60)
|
| 214 |
+
print("π CHAT INTERFACE: http://localhost:8080")
|
| 215 |
+
print("π± ALTERNATIVE URL: http://127.0.0.1:8080")
|
| 216 |
+
print("π§ API DOCUMENTATION: http://localhost:8080/docs")
|
| 217 |
+
print("β
CHAT.HTML SERVED: templates/chat.html")
|
| 218 |
+
print("π TEMPLATES FOLDER: ./templates/")
|
| 219 |
+
print("=" * 60 + "\n")
|
| 220 |
+
|
| 221 |
+
uvicorn.run(app, host="0.0.0.0", port=8080)
|