Hira99 commited on
Commit
de0aa88
1 Parent(s): fa01e2b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
3
+ import json
4
+
5
+ # Load the dataset
6
+ with open('faq_dataset.json') as f:
7
+ faq_data = json.load(f)
8
+
9
+ # Initialize the model and tokenizer
10
+ model_name = "distilbert-base-uncased-distilled-squad"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
13
+ nlp = pipeline("question-answering", model=model, tokenizer=tokenizer)
14
+
15
+ # Create a function to get answers
16
+ def get_answer(question):
17
+ for item in faq_data:
18
+ context = item['answer']
19
+ result = nlp(question=question, context=context)
20
+ if result['score'] > 0.5:
21
+ return result['answer']
22
+ return "Sorry, I don't know the answer to that question."
23
+
24
+ # Create the Gradio interface
25
+ iface = gr.Interface(
26
+ fn=get_answer,
27
+ inputs=gr.inputs.Textbox(label="Ask a Question"),
28
+ outputs=gr.outputs.Textbox(label="Answer"),
29
+ title="FAQ Chatbot",
30
+ description="Ask a question and get an answer from the FAQ dataset."
31
+ )
32
+
33
+ # Launch the interface
34
+ iface.launch()