Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the Hugging Face model
|
| 5 |
+
model = "deepset/bert-base-cased-squad2"
|
| 6 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=model)
|
| 7 |
+
|
| 8 |
+
# Define the function to answer questions
|
| 9 |
+
def get_answer(text):
|
| 10 |
+
result = qa_pipeline({
|
| 11 |
+
"question": text,
|
| 12 |
+
"context": knowledge_base_text
|
| 13 |
+
})
|
| 14 |
+
return result["answer"]
|
| 15 |
+
|
| 16 |
+
# Define the Gradio interface
|
| 17 |
+
def chatbot_interface(text):
|
| 18 |
+
answer = get_answer(text)
|
| 19 |
+
return answer
|
| 20 |
+
|
| 21 |
+
knowledge_base_file = "knowledge_base.txt" # Path to the knowledge base text file
|
| 22 |
+
|
| 23 |
+
# Load the knowledge base from a text file
|
| 24 |
+
with open(knowledge_base_file, "r") as f:
|
| 25 |
+
knowledge_base_text = f.read()
|
| 26 |
+
|
| 27 |
+
# Create the Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=chatbot_interface,
|
| 30 |
+
inputs="Ask any questions about BATB",
|
| 31 |
+
outputs="Result",
|
| 32 |
+
title="British American Tobacco Bangladesh",
|
| 33 |
+
description="- powered by IDT",
|
| 34 |
+
theme="default"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Run the Gradio interface
|
| 38 |
+
iface.launch()
|