rabiyulfahim commited on
Commit
079a804
·
verified ·
1 Parent(s): 54f0ee3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -116
app.py CHANGED
@@ -1,118 +1,53 @@
1
- from fastapi import FastAPI,Query
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.staticfiles import StaticFiles
9
-
10
-
11
- # ✅ Force Hugging Face cache to /tmp (writable in Spaces)
12
- os.environ["HF_HOME"] = "/tmp"
13
- os.environ["TRANSFORMERS_CACHE"] = "/tmp"
14
-
15
-
16
- # Base + Adapter
17
- from peft import PeftModel
18
-
19
- base_model_id = "microsoft/DialoGPT-small"
20
- adapter_id = "rabiyulfahim/Python_coding_model"
21
-
22
- tokenizer = AutoTokenizer.from_pretrained(base_model_id, cache_dir="/tmp")
23
- base_model = AutoModelForCausalLM.from_pretrained(base_model_id, cache_dir="/tmp")
24
- model = PeftModel.from_pretrained(base_model, adapter_id, cache_dir="/tmp")
25
-
26
-
27
-
28
-
29
- app = FastAPI(title="QA GPT2 API UI", description="Serving HuggingFace model with FastAPI")
30
-
31
- app.add_middleware(
32
- CORSMiddleware,
33
- allow_origins=["*"],
34
- allow_credentials=True,
35
- allow_methods=["*"],
36
- allow_headers=["*"],
37
- )
38
- # Request schema
39
- class QueryRequest(BaseModel):
40
- question: str
41
- max_new_tokens: int = 50
42
- temperature: float = 0.7
43
- top_p: float = 0.9
44
-
45
-
46
- @app.get("/")
47
- def home():
48
- return {"message": "Welcome to QA GPT2 API 🚀"}
49
-
50
- @app.get("/ask")
51
- def ask(question: str, max_new_tokens: int = 50):
52
- inputs = tokenizer(question, return_tensors="pt")
53
- outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
54
- answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
55
- return {"question": question, "answer": answer}
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
  )