savan360 commited on
Commit
2c95b1b
·
verified ·
1 Parent(s): 6282da4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -11
app.py CHANGED
@@ -1,17 +1,46 @@
1
  import gradio as gr
 
2
 
3
- # Define the Gradio interface
4
- iface = gr.Interface(
5
- fn=generate_response,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  inputs=[
7
- gr.inputs.Textbox(lines=2, label="Input Prompt"),
8
- gr.inputs.Slider(minimum=50, maximum=200, step=10, default=100, label="Max Length"),
9
- gr.inputs.Slider(minimum=0.1, maximum=1.0, step=0.1, default=0.7, label="Temperature")
10
  ],
11
- outputs=gr.outputs.Textbox(label="Generated Response"),
12
- title="Llama 2 Chatbot",
13
- description="Interact with the Llama 2 model using Gradio."
 
14
  )
15
 
16
- # Launch the interface
17
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # **1. Load a better QA model** – using RoBERTa-large for higher accuracy.
5
+ # (You can switch to 'deepset/roberta-base-squad2-distilled' for speed or others as needed.)
6
+ MODEL_NAME = "deepset/roberta-large-squad2"
7
+ qa_pipeline = pipeline(
8
+ "question-answering",
9
+ model=MODEL_NAME,
10
+ tokenizer=MODEL_NAME
11
+ # You can add device=0 here if using a GPU for faster inference
12
+ )
13
+
14
+ # Define the QA function for Gradio
15
+ def answer_question(question, context):
16
+ # **2. Use the pipeline with improved parameters**
17
+ result = qa_pipeline(
18
+ question=question,
19
+ context=context,
20
+ handle_impossible_answer=True, # allow "no answer" if applicable
21
+ top_k=1, # we only want the best answer
22
+ max_answer_len=30 # increase if expecting longer answers
23
+ )
24
+ answer = result.get("answer", "").strip()
25
+ score = result.get("score", 0.0)
26
+ # **3. Handle cases where no answer is found or confidence is low**
27
+ if answer == "" or score < 0.1:
28
+ # If the model found no answer or is very unsure, return a fallback message
29
+ return "🤔 I’m not sure – the model couldn’t find a clear answer in the text."
30
+ return answer
31
+
32
+ # **4. Set up Gradio interface** with appropriate input/output components
33
+ interface = gr.Interface(
34
+ fn=answer_question,
35
  inputs=[
36
+ gr.components.Textbox(lines=2, label="Question"),
37
+ gr.components.Textbox(lines=10, label="Context")
 
38
  ],
39
+ outputs=gr.components.Textbox(label="Answer"),
40
+ title="Question Answering Demo",
41
+ description="Ask a question and get an answer from the provided context. " \
42
+ "Supports unanswerable questions."
43
  )
44
 
45
+ if __name__ == "__main__":
46
+ interface.launch()