tosin2013 commited on
Commit
55e40d9
·
verified ·
1 Parent(s): 8fae716

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -18
app.py CHANGED
@@ -1,29 +1,78 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Load the Question Answering pipeline
5
- qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
 
 
 
 
 
 
6
 
7
- def answer_question(context, question):
8
- result = qa_model(question=question, context=context)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  answer = result['answer']
10
  score = result['score']
11
- return answer, f"{score:.2f}"
 
 
 
 
12
 
13
  # Define the Gradio interface
14
- interface = gr.Interface(
15
- fn=answer_question,
16
- inputs=[
17
- gr.Textbox(lines=10, placeholder="Enter the context here...", label="Context"),
18
- gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
19
- ],
20
- outputs=[
21
- gr.Textbox(label="Answer"),
22
- gr.Textbox(label="Confidence Score")
23
- ],
24
- title="Question Answering System",
25
- description="Upload a document and ask questions to get answers based on the context."
26
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  if __name__ == "__main__":
29
  interface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from transformers.pipelines import PipelineException
4
 
5
+ # Preload models
6
+ models = {
7
+ "distilbert-base-uncased-distilled-squad": "distilbert-base-uncased-distilled-squad",
8
+ "roberta-base-squad2": "deepset/roberta-base-squad2",
9
+ "bert-large-uncased-whole-word-masking-finetuned-squad": "bert-large-uncased-whole-word-masking-finetuned-squad",
10
+ "albert-base-v2": "twmkn9/albert-base-v2-squad2",
11
+ "xlm-roberta-large-squad2": "deepset/xlm-roberta-large-squad2"
12
+ }
13
 
14
+ loaded_models = {}
15
+
16
+ def load_model(model_name):
17
+ if model_name not in loaded_models:
18
+ try:
19
+ loaded_models[model_name] = pipeline("question-answering", model=models[model_name])
20
+ except PipelineException as e:
21
+ return str(e)
22
+ return loaded_models[model_name]
23
+
24
+ def answer_question(model_name, file, question):
25
+ model = load_model(model_name)
26
+ if isinstance(model, str):
27
+ return model, "", ""
28
+
29
+ context = file.read() if file else ""
30
+ result = model(question=question, context=context)
31
  answer = result['answer']
32
  score = result['score']
33
+
34
+ # Explain score
35
+ score_explanation = f"The confidence score ranges from 0 to 1, where a higher score indicates higher confidence in the answer's correctness. In this case, the score is {score:.2f}. A score closer to 1 implies the model is very confident about the answer."
36
+
37
+ return answer, f"{score:.2f}", score_explanation
38
 
39
  # Define the Gradio interface
40
+ with gr.Blocks() as interface:
41
+ gr.Markdown(
42
+ """
43
+ # Question Answering System
44
+ Upload a document and ask questions to get answers based on the context.
45
+ """)
46
+
47
+ with gr.Row():
48
+ model_dropdown = gr.Dropdown(
49
+ choices=list(models.keys()),
50
+ label="Select Model",
51
+ value="distilbert-base-uncased-distilled-squad"
52
+ )
53
+
54
+ with gr.Row():
55
+ file_input = gr.File(label="Upload Document")
56
+ question_input = gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
57
+
58
+ with gr.Row():
59
+ answer_output = gr.Textbox(label="Answer")
60
+ score_output = gr.Textbox(label="Confidence Score")
61
+ explanation_output = gr.Textbox(label="Score Explanation")
62
+
63
+ with gr.Row():
64
+ submit_button = gr.Button("Submit")
65
+
66
+ gr.Markdown("### Progress")
67
+ with gr.Row():
68
+ progress_bar = gr.Progress(label="Loading Model...")
69
+
70
+ submit_button.click(
71
+ answer_question,
72
+ inputs=[model_dropdown, file_input, question_input],
73
+ outputs=[answer_output, score_output, explanation_output],
74
+ show_progress=progress_bar,
75
+ )
76
 
77
  if __name__ == "__main__":
78
  interface.launch()