Spaces:
Sleeping
Sleeping
handling GPU warning and refactor
Browse files
app.py
CHANGED
|
@@ -1,29 +1,31 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 3 |
from contextretriever import WikipediaContextRetriever
|
| 4 |
from processor import MCQInferenceProcessor
|
| 5 |
|
| 6 |
-
import sys
|
| 7 |
-
import warnings
|
| 8 |
|
| 9 |
-
# Prevents Python from printing ignored garbage collection exceptions
|
| 10 |
sys.unraisablehook = lambda unraisable: None
|
| 11 |
-
|
| 12 |
-
# Hides background socket resource warnings
|
| 13 |
warnings.filterwarnings("ignore", category=ResourceWarning)
|
| 14 |
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def get_random_question_from_local():
|
| 21 |
"""Pulls a random question row from the CSV and returns values for the UI inputs."""
|
| 22 |
if df_samples.empty:
|
| 23 |
-
return "", "", "", "", "", ""
|
| 24 |
|
| 25 |
random_row = df_samples.sample(n=1).iloc[0]
|
| 26 |
-
|
| 27 |
return (
|
| 28 |
str(random_row.get("prompt", "")),
|
| 29 |
str(random_row.get("A", "")),
|
|
@@ -33,22 +35,37 @@ def get_random_question_from_local():
|
|
| 33 |
str(random_row.get("E", ""))
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Initialization outside the Gradio interface
|
| 37 |
processor = MCQInferenceProcessor(model_repo_id="balamaniansp/bigru-map3loss-lr5e-5-layers3-drop02-vocab10k-seq512-emb256-hidden256-fold3")
|
| 38 |
retriever = WikipediaContextRetriever(sample_size=1500)
|
| 39 |
|
|
|
|
| 40 |
def run_mcq_pipeline(question, opt_a, opt_b, opt_c, opt_d, opt_e):
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
retrieved_blocks = retriever._get_context(question, top_n=1)
|
| 44 |
-
context_str = retrieved_blocks[0]["text"]
|
| 45 |
-
context_source = retrieved_blocks[0]["title"]
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
probabilities = processor.predict(context=context_str, question=question, options=options_dict)
|
| 48 |
|
| 49 |
context_feedback = f"📚 Source: {context_source}\n\nFetched Context:\n{context_str}"
|
| 50 |
return probabilities, context_feedback
|
| 51 |
|
|
|
|
| 52 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 53 |
gr.Markdown("# 🧠 Academic Presentation: Open-Domain MCQ Solver (RAG)")
|
| 54 |
gr.Markdown("Type a question manually, or click **🎲 Load Random Sample Question** to test an existing dataset question.")
|
|
@@ -70,6 +87,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 70 |
output_labels = gr.Label(label="Model Probability Confidence Distribution")
|
| 71 |
output_context = gr.Textbox(label="Retrieved Reference Framework Data", interactive=False, lines=10)
|
| 72 |
|
|
|
|
| 73 |
submit_btn.click(
|
| 74 |
fn=run_mcq_pipeline,
|
| 75 |
inputs=[input_q, input_a, input_b, input_c, input_d, input_e],
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import warnings
|
| 3 |
import pandas as pd
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
from contextretriever import WikipediaContextRetriever
|
| 8 |
from processor import MCQInferenceProcessor
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
sys.unraisablehook = lambda unraisable: None
|
|
|
|
|
|
|
| 12 |
warnings.filterwarnings("ignore", category=ResourceWarning)
|
| 13 |
|
| 14 |
|
| 15 |
+
try:
|
| 16 |
+
train_df = pd.read_csv("data/train.csv")
|
| 17 |
+
test_df = pd.read_csv("data/test.csv")
|
| 18 |
+
df_samples = pd.concat([train_df, test_df], ignore_index=True)
|
| 19 |
+
except FileNotFoundError:
|
| 20 |
+
print("⚠️ Warning: Sample CSV data files not found in 'data/' directory. Random button will be empty.")
|
| 21 |
+
df_samples = pd.DataFrame()
|
| 22 |
|
| 23 |
def get_random_question_from_local():
|
| 24 |
"""Pulls a random question row from the CSV and returns values for the UI inputs."""
|
| 25 |
if df_samples.empty:
|
| 26 |
+
return "No samples available", "", "", "", "", ""
|
| 27 |
|
| 28 |
random_row = df_samples.sample(n=1).iloc[0]
|
|
|
|
| 29 |
return (
|
| 30 |
str(random_row.get("prompt", "")),
|
| 31 |
str(random_row.get("A", "")),
|
|
|
|
| 35 |
str(random_row.get("E", ""))
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
processor = MCQInferenceProcessor(model_repo_id="balamaniansp/bigru-map3loss-lr5e-5-layers3-drop02-vocab10k-seq512-emb256-hidden256-fold3")
|
| 39 |
retriever = WikipediaContextRetriever(sample_size=1500)
|
| 40 |
|
| 41 |
+
@spaces.GPU
|
| 42 |
def run_mcq_pipeline(question, opt_a, opt_b, opt_c, opt_d, opt_e):
|
| 43 |
+
# Fallback to avoid empty string matrix processing errors
|
| 44 |
+
options_dict = {
|
| 45 |
+
"A": opt_a if opt_a.strip() else "[Empty Option A]",
|
| 46 |
+
"B": opt_b if opt_b.strip() else "[Empty Option B]",
|
| 47 |
+
"C": opt_c if opt_c.strip() else "[Empty Option C]",
|
| 48 |
+
"D": opt_d if opt_d.strip() else "[Empty Option D]",
|
| 49 |
+
"E": opt_e if opt_e.strip() else "[Empty Option E]"
|
| 50 |
+
}
|
| 51 |
|
| 52 |
+
# Context extraction through RAG framework
|
| 53 |
retrieved_blocks = retriever._get_context(question, top_n=1)
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
if retrieved_blocks and len(retrieved_blocks) > 0:
|
| 56 |
+
context_str = retrieved_blocks[0].get("text", "No context found.")
|
| 57 |
+
context_source = retrieved_blocks[0].get("title", "Unknown Source")
|
| 58 |
+
else:
|
| 59 |
+
context_str = "No applicable Wikipedia context retrieved."
|
| 60 |
+
context_source = "Fallback System"
|
| 61 |
+
|
| 62 |
+
# Model inference call execution
|
| 63 |
probabilities = processor.predict(context=context_str, question=question, options=options_dict)
|
| 64 |
|
| 65 |
context_feedback = f"📚 Source: {context_source}\n\nFetched Context:\n{context_str}"
|
| 66 |
return probabilities, context_feedback
|
| 67 |
|
| 68 |
+
# Layout definition using standard Gradio Blocks
|
| 69 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
gr.Markdown("# 🧠 Academic Presentation: Open-Domain MCQ Solver (RAG)")
|
| 71 |
gr.Markdown("Type a question manually, or click **🎲 Load Random Sample Question** to test an existing dataset question.")
|
|
|
|
| 87 |
output_labels = gr.Label(label="Model Probability Confidence Distribution")
|
| 88 |
output_context = gr.Textbox(label="Retrieved Reference Framework Data", interactive=False, lines=10)
|
| 89 |
|
| 90 |
+
# Wire up action dependencies
|
| 91 |
submit_btn.click(
|
| 92 |
fn=run_mcq_pipeline,
|
| 93 |
inputs=[input_q, input_a, input_b, input_c, input_d, input_e],
|