Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,66 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
-
from fastapi.templating import Jinja2Templates
|
| 4 |
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
MODEL_PATH = "MedhaCodes/qna_finetuned_model"
|
|
|
|
|
|
|
| 10 |
qa_pipeline = pipeline(
|
| 11 |
"question-answering",
|
| 12 |
model=AutoModelForQuestionAnswering.from_pretrained(MODEL_PATH),
|
| 13 |
tokenizer=AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
@app.get("/", response_class=HTMLResponse)
|
| 19 |
-
async def home(request: Request):
|
| 20 |
-
return templates.TemplateResponse("index.html", {"request": request})
|
| 21 |
-
|
| 22 |
-
@app.post("/predict")
|
| 23 |
-
async def predict(request: Request):
|
| 24 |
-
data = await request.json()
|
| 25 |
-
context = data.get("context")
|
| 26 |
-
questions_text = data.get("question")
|
| 27 |
-
|
| 28 |
if not context or not questions_text:
|
| 29 |
-
return
|
| 30 |
|
| 31 |
-
#
|
| 32 |
questions = [q.strip() for q in questions_text.strip().split("\n") if q.strip()]
|
| 33 |
answers = []
|
| 34 |
|
| 35 |
for i, q in enumerate(questions, start=1):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
| 3 |
|
| 4 |
+
# β
Load your fine-tuned model
|
| 5 |
+
# Use your Hugging Face model repo name (e.g., "MedhaCodes/qna_finetuned_model")
|
| 6 |
+
# or use "saved_model" if running locally
|
| 7 |
MODEL_PATH = "MedhaCodes/qna_finetuned_model"
|
| 8 |
+
|
| 9 |
+
# Load the model and tokenizer
|
| 10 |
qa_pipeline = pipeline(
|
| 11 |
"question-answering",
|
| 12 |
model=AutoModelForQuestionAnswering.from_pretrained(MODEL_PATH),
|
| 13 |
tokenizer=AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Function to process multiple questions
|
| 17 |
+
def answer_multiple_questions(context, questions_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if not context or not questions_text:
|
| 19 |
+
return "β οΈ Please provide both context and questions."
|
| 20 |
|
| 21 |
+
# Split questions by line
|
| 22 |
questions = [q.strip() for q in questions_text.strip().split("\n") if q.strip()]
|
| 23 |
answers = []
|
| 24 |
|
| 25 |
for i, q in enumerate(questions, start=1):
|
| 26 |
+
result = qa_pipeline(question=q, context=context)
|
| 27 |
+
answer = result["answer"]
|
| 28 |
+
score = result["score"]
|
| 29 |
+
answers.append(f"Q{i}: {q}\nAnswer: {answer}\nConfidence: {score:.4f}\n{'-'*60}")
|
| 30 |
+
|
| 31 |
+
return "\n".join(answers)
|
| 32 |
+
|
| 33 |
+
# Gradio Interface
|
| 34 |
+
interface = gr.Interface(
|
| 35 |
+
fn=answer_multiple_questions,
|
| 36 |
+
inputs=[
|
| 37 |
+
gr.Textbox(
|
| 38 |
+
label="π Context (Paragraph/Text)",
|
| 39 |
+
lines=20,
|
| 40 |
+
placeholder="Enter or paste a long text context here..."
|
| 41 |
+
),
|
| 42 |
+
gr.Textbox(
|
| 43 |
+
label="β Questions (one per line)",
|
| 44 |
+
lines=10,
|
| 45 |
+
placeholder="Type each question on a new line..."
|
| 46 |
+
),
|
| 47 |
+
],
|
| 48 |
+
outputs=gr.Textbox(
|
| 49 |
+
label="π§© Answers",
|
| 50 |
+
lines=25,
|
| 51 |
+
placeholder="Model responses will appear here..."
|
| 52 |
+
),
|
| 53 |
+
title="π€ Machine Learning QA System",
|
| 54 |
+
description=(
|
| 55 |
+
"This Gradio app uses a fine-tuned RoBERTa model to answer questions from a given context.\n\n"
|
| 56 |
+
"π Enter a long paragraph in the context box.\n"
|
| 57 |
+
"π Write multiple questions (one per line).\n"
|
| 58 |
+
"π Get all answers organized line by line!"
|
| 59 |
+
),
|
| 60 |
+
theme="soft", # optional theme
|
| 61 |
+
allow_flagging="never"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Launch app
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
interface.launch()
|