dschandra commited on
Commit
1a1c627
·
verified ·
1 Parent(s): 515a1fd

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +14 -15
chatbot.py CHANGED
@@ -1,18 +1,17 @@
1
- from knowledge_base import get_answer_from_knowledge_base, load_qa_model
2
 
3
- def generate_answer(question):
4
- # Check if the question is in the custom answers dictionary
5
- custom_answers = {
6
- "Who is the chief minister of AP?": "Y. S. Jagan Mohan Reddy",
7
- # Add other custom answers if necessary
8
- }
9
 
10
- if question in custom_answers:
11
- return custom_answers[question]
 
 
 
 
 
 
 
12
 
13
- # If not, get the answer from the knowledge base using Hugging Face model
14
- answer = get_answer_from_knowledge_base(question)
15
- if answer:
16
- return answer
17
- else:
18
- return "Sorry, I couldn't find an answer to that question. Please try again later."
 
1
+ from transformers import pipeline
2
 
3
+ # Load the GPT-2 model using Hugging Face's pipeline for text generation
4
+ generator = pipeline("text-generation", model="gpt2")
 
 
 
 
5
 
6
+ def generate_answer(question):
7
+ # Generate an answer using the GPT-2 model
8
+ response = generator(question, max_length=100, num_return_sequences=1)
9
+
10
+ # Extract the generated text and return it as the answer
11
+ answer = response[0]['generated_text']
12
+
13
+ # Clean the answer to remove the prompt (question)
14
+ answer = answer.replace(question, '').strip()
15
 
16
+ # Return the cleaned answer
17
+ return answer