thetripathi26 commited on
Commit
3f3c39f
1 Parent(s): 917d753

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
2
+ import gradio as gr
3
+ import time
4
+
5
+ # Author information
6
+ author = "Chris Choodai"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased-distilled-squad")
9
+ model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-cased-distilled-squad")
10
+ qa_pipe = pipeline("question-answering", model=model, tokenizer=tokenizer)
11
+
12
+ def response(context, question):
13
+ result = qa_pipe(context=context, question=question)
14
+ return result['answer']
15
+
16
+ input_context = gr.Textbox(lines=10, label='Input Context', placeholder='Enter context here...')
17
+ input_question = gr.Textbox(label='Input Question', placeholder='Ask your question here...')
18
+ output_text = gr.Textbox(label="Response", placeholder='Response will display here..')
19
+
20
+ interface = gr.Interface(response, inputs=[input_context, input_question], outputs=output_text,
21
+ title="<div style='color: #336699; font-size: 24px; font-weight: bold; border: 2px solid #336699; padding: 10px; border-radius: 10px;'>Bert Context Based Question Answering</div>",
22
+ description=f"""<div style='color: #666666; font-family: Arial, sans-serif;'>
23
+ <p style='margin-top: 10px;'>Enter context and question to get the response.</p>
24
+ <p>Developed by <span style='color: #336699; font-weight: bold;'>{author}</span>.</p>
25
+ </div>""",
26
+ theme="default" # Change theme to default
27
+ )
28
+
29
+ # Define example contexts, questions, and expected responses
30
+ examples = [
31
+ ["The capital of France is Paris.", "What is the capital of France?", "Paris"],
32
+ ["Water boils at 100 degrees Celsius or 212 degrees Fahrenheit.", "At what temperature does water boil?", "100 degrees Celsius"],
33
+ ["The Mona Lisa was painted by Leonardo da Vinci.", "Who painted the Mona Lisa?", "Leonardo da Vinci"],
34
+ ]
35
+
36
+ def simulate_interaction():
37
+ for example in examples:
38
+ context, question, expected_response = example
39
+ input_context.value = context
40
+ input_question.value = question
41
+ time.sleep(2) # Simulating user typing delay
42
+ response_text = response(context, question)
43
+ output_text.value = response_text
44
+ time.sleep(2) # Simulating response delay
45
+
46
+ # Simulate user interaction
47
+ simulate_interaction()
48
+ # Deploy the interface
49
+ interface.launch(share=True, debug=True)