MedhaCodes commited on
Commit
525e35d
·
verified ·
1 Parent(s): 94547e8

Upload api_app.py

Browse files
Files changed (1) hide show
  1. api_app.py +56 -0
api_app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.templating import Jinja2Templates
5
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
6
+ import os
7
+
8
+ app = FastAPI(title="QA Dashboard Pro")
9
+
10
+ # Load your fine-tuned model
11
+ MODEL_PATH = "MedhaCodes/qna_finetuned_model"
12
+ qa_pipeline = pipeline(
13
+ "question-answering",
14
+ model=AutoModelForQuestionAnswering.from_pretrained(MODEL_PATH),
15
+ tokenizer=AutoTokenizer.from_pretrained(MODEL_PATH)
16
+ )
17
+
18
+ # Mount static files (CSS, JS)
19
+ app.mount(
20
+ "/static",
21
+ StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")),
22
+ name="static"
23
+ )
24
+
25
+ # Load templates
26
+ templates = Jinja2Templates(directory="templates")
27
+
28
+ @app.get("/", response_class=HTMLResponse)
29
+ async def home(request: Request):
30
+ return templates.TemplateResponse("index.html", {"request": request})
31
+
32
+ @app.post("/predict")
33
+ async def predict(request: Request):
34
+ data = await request.json()
35
+ context = data.get("context")
36
+ questions_text = data.get("question")
37
+
38
+ if not context or not questions_text:
39
+ return JSONResponse({"error": "Please provide both context and question"}, status_code=400)
40
+
41
+ # Handle multiple questions line-by-line
42
+ questions = [q.strip() for q in questions_text.strip().split("\n") if q.strip()]
43
+ answers = []
44
+
45
+ for i, q in enumerate(questions, start=1):
46
+ try:
47
+ result = qa_pipeline(question=q, context=context)
48
+ answers.append({
49
+ "question": q,
50
+ "answer": result["answer"],
51
+ "score": round(result["score"], 4)
52
+ })
53
+ except Exception as e:
54
+ answers.append({"question": q, "answer": f"Error: {e}", "score": 0})
55
+
56
+ return {"results": answers}