Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,53 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import torch
|
| 4 |
-
import os
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.responses import HTMLResponse
|
| 8 |
-
from fastapi.
|
| 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 |
-
# Mount static folder
|
| 59 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 60 |
-
|
| 61 |
-
@app.get("/ui", response_class=HTMLResponse)
|
| 62 |
-
def serve_ui():
|
| 63 |
-
html_path = os.path.join("static", "index.html")
|
| 64 |
-
with open(html_path, "r", encoding="utf-8") as f:
|
| 65 |
-
return HTMLResponse(f.read())
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Health check endpoint
|
| 69 |
-
@app.get("/health")
|
| 70 |
-
def health():
|
| 71 |
-
return {"status": "ok"}
|
| 72 |
-
|
| 73 |
-
# Inference endpoint
|
| 74 |
-
@app.post("/predict")
|
| 75 |
-
def predict(request: QueryRequest):
|
| 76 |
-
inputs = tokenizer(request.question, return_tensors="pt")
|
| 77 |
-
outputs = model.generate(
|
| 78 |
-
**inputs,
|
| 79 |
-
max_new_tokens=request.max_new_tokens,
|
| 80 |
-
do_sample=True,
|
| 81 |
-
temperature=0.7,
|
| 82 |
-
top_p=0.9,
|
| 83 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 84 |
-
return_dict_in_generate=True
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
answer = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
|
| 88 |
-
return {
|
| 89 |
-
"question": request.question,
|
| 90 |
-
"answer": answer
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
@app.get("/answers")
|
| 97 |
-
def predict(question: str = Query(..., description="The question to ask"), max_new_tokens: int = Query(50, description="Max new tokens to generate")):
|
| 98 |
-
# Tokenize the input question
|
| 99 |
-
inputs = tokenizer(question, return_tensors="pt")
|
| 100 |
-
|
| 101 |
-
# Generate output from model
|
| 102 |
-
outputs = model.generate(
|
| 103 |
-
**inputs,
|
| 104 |
-
max_new_tokens=max_new_tokens,
|
| 105 |
-
do_sample=True,
|
| 106 |
-
temperature=0.7,
|
| 107 |
-
top_p=0.9,
|
| 108 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 109 |
-
return_dict_in_generate=True
|
| 110 |
)
|
| 111 |
-
|
| 112 |
-
# Decode output
|
| 113 |
-
answer = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
|
| 114 |
-
|
| 115 |
-
return {
|
| 116 |
-
"question": question,
|
| 117 |
-
"answer": answer
|
| 118 |
-
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
import sys
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
templates = Jinja2Templates(directory="templates")
|
| 9 |
+
|
| 10 |
+
# --- Core tracer logic ---
|
| 11 |
+
def run_code_with_trace(code: str):
|
| 12 |
+
trace_data = []
|
| 13 |
+
stdout_capture = io.StringIO()
|
| 14 |
+
|
| 15 |
+
def tracer(frame, event, arg):
|
| 16 |
+
if event == "line":
|
| 17 |
+
trace_data.append({
|
| 18 |
+
"line": frame.f_lineno,
|
| 19 |
+
"locals": frame.f_locals.copy()
|
| 20 |
+
})
|
| 21 |
+
return tracer
|
| 22 |
+
|
| 23 |
+
sys.settrace(tracer)
|
| 24 |
+
try:
|
| 25 |
+
exec(code, {}, {})
|
| 26 |
+
except Exception as e:
|
| 27 |
+
trace_data.append({"error": str(e)})
|
| 28 |
+
finally:
|
| 29 |
+
sys.settrace(None)
|
| 30 |
+
|
| 31 |
+
output = stdout_capture.getvalue()
|
| 32 |
+
stdout_capture.close()
|
| 33 |
+
|
| 34 |
+
return trace_data, output
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.get("/", response_class=HTMLResponse)
|
| 38 |
+
async def index(request: Request):
|
| 39 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@app.post("/run", response_class=HTMLResponse)
|
| 43 |
+
async def run_code(request: Request, code: str = Form(...)):
|
| 44 |
+
trace, output = run_code_with_trace(code)
|
| 45 |
+
return templates.TemplateResponse(
|
| 46 |
+
"index.html",
|
| 47 |
+
{
|
| 48 |
+
"request": request,
|
| 49 |
+
"trace": trace,
|
| 50 |
+
"output": output,
|
| 51 |
+
"code": code
|
| 52 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|