samandar1105's picture
Upload app.py
12b33a7 verified
Raw
History Blame Contribute Delete
5.92 kB
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
# ============================================================
# CONFIGURATION
# ============================================================
MODEL_ID = "samandar1105/Question_Answering"
EXAMPLES = [
[
"The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It was designed by Gustave Eiffel and built between 1887 and 1889 as the entrance arch to the 1889 World's Fair.",
"Who designed the Eiffel Tower?"
],
[
"Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability. Python was created by Guido van Rossum and first released in 1991.",
"When was Python first released?"
],
[
"The Amazon rainforest covers over 5.5 million square kilometers across nine countries, primarily Brazil. It is home to an estimated 10% of all species on Earth and is often called the lungs of the Earth.",
"What percentage of Earth's species live in the Amazon?"
],
[
"Marie Curie was a Polish and naturalized-French physicist and chemist who conducted pioneering research on radioactivity. She was the first woman to win a Nobel Prize, the first person to win the Nobel Prize twice, and the only person to win the Nobel Prize in two different sciences.",
"How many Nobel Prizes did Marie Curie win?"
],
[
"The Great Wall of China is a series of fortifications built across the historical northern borders of ancient Chinese states. The wall stretches approximately 21,196 kilometers in total.",
"How long is the Great Wall of China?"
],
]
print(f"Loading model: {MODEL_ID}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID,
trust_remote_code=False
)
model = AutoModelForQuestionAnswering.from_pretrained(
MODEL_ID,
trust_remote_code=False
)
model.to(device)
model.eval()
print(f"Model loaded on {device}")
def answer_question(context, question):
if not context or context.strip() == "":
return "⚠️ Please provide a text passage (context).", "", ""
if not question or question.strip() == "":
return "⚠️ Please enter a question.", "", ""
if len(context.strip()) < 20:
return "⚠️ Please enter a longer context.", "", ""
if len(question.strip()) < 5:
return "⚠️ Please enter a more complete question.", "", ""
inputs = tokenizer(
question,
context,
return_tensors="pt",
truncation=True,
max_length=384,
stride=128,
return_offsets_mapping=True,
padding=True,
)
inputs.pop("offset_mapping")
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
start_logits = outputs.start_logits[0]
end_logits = outputs.end_logits[0]
sequence_ids = tokenizer(
question,
context,
truncation=True,
max_length=384,
).sequence_ids()
context_start = next(i for i, s in enumerate(sequence_ids) if s == 1)
context_end = len(sequence_ids) - 1 - next(
i for i, s in enumerate(reversed(sequence_ids)) if s == 1
)
mask = torch.full(start_logits.shape, float("-inf"), device=device)
mask[context_start:context_end+1] = 0
start_logits += mask
end_logits += mask
start_idx = start_logits.argmax().item()
end_idx = end_logits.argmax().item()
if end_idx < start_idx:
end_idx = start_idx
answer = tokenizer.decode(
inputs["input_ids"][0][start_idx:end_idx+1],
skip_special_tokens=True
).strip()
if answer == "":
return "❓ Could not find an answer in the provided text.", "", ""
start_conf = torch.softmax(outputs.start_logits[0], dim=-1)[start_idx].item()
end_conf = torch.softmax(outputs.end_logits[0], dim=-1)[end_idx].item()
score = (start_conf + end_conf) / 2
if score > 0.50:
confidence = f"✅ High confidence ({score*100:.1f}%)"
elif score > 0.25:
confidence = f"⚠️ Medium confidence ({score*100:.1f}%)"
else:
confidence = f"❓ Low confidence ({score*100:.1f}%)"
highlighted = context.replace(answer, f"**[{answer}]**", 1)
return answer, confidence, highlighted
with gr.Blocks(
title="Question Answering AI",
theme=gr.themes.Soft(),
) as demo:
gr.Markdown(
"""
# 🔍 Extractive Question Answering
Paste any text passage and ask a question.
The model extracts the answer directly from the provided context.
**Model:** bert-base-cased fine-tuned on SQuAD v1.1
"""
)
with gr.Row():
with gr.Column(scale=3):
context = gr.Textbox(
label="Context",
lines=8,
placeholder="Paste your text here..."
)
question = gr.Textbox(
label="Question",
lines=2,
placeholder="Ask your question..."
)
with gr.Row():
submit = gr.Button("Find Answer", variant="primary")
clear = gr.ClearButton([context, question])
with gr.Column(scale=2):
answer = gr.Textbox(label="Answer")
confidence = gr.Textbox(label="Confidence")
highlighted = gr.Markdown(label="Highlighted Context")
gr.Examples(
examples=EXAMPLES,
inputs=[context, question]
)
submit.click(
answer_question,
[context, question],
[answer, confidence, highlighted]
)
question.submit(
answer_question,
[context, question],
[answer, confidence, highlighted]
)
demo.launch()