File size: 963 Bytes
f2d8bf0
f675467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08cf1b7
18ab628
f675467
 
fffbd16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gradio as gr
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer

# Load the pre-trained model and tokenizer
model_name = "distilbert-base-uncased-distilled-squad"
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Load custom knowledge document
with open("knowledge.txt", "r") as file:
    custom_knowledge = file.read()

# Initialize the question-answering pipeline
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)

def answer_question(question):
    result = qa_pipeline(question=question, context=custom_knowledge)
    return result['answer']

# Set up the Gradio interface
interface = gr.Interface(
    fn=answer_question,
    inputs="text",
    outputs="text",
    title="Custom Knowledge QA",
    description="Ask questions based on the custom knowledge document."
)

if __name__ == "__main__":
    interface.launch()