dschandra commited on
Commit
d40057f
·
verified ·
1 Parent(s): 36bf9cf

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +9 -4
chatbot.py CHANGED
@@ -1,10 +1,10 @@
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
@@ -13,5 +13,10 @@ def generate_answer(question):
13
  # Clean the answer to remove the prompt (question)
14
  answer = answer.replace(question, '').strip()
15
 
16
- # Return the cleaned answer
17
- return answer
 
 
 
 
 
 
1
  from transformers import pipeline
2
 
3
+ # Initialize 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 GPT-2
8
  response = generator(question, max_length=100, num_return_sequences=1)
9
 
10
  # Extract the generated text and return it as the answer
 
13
  # Clean the answer to remove the prompt (question)
14
  answer = answer.replace(question, '').strip()
15
 
16
+ # Format the answer for Gradio's chatbot component using the 'messages' format
17
+ messages = [
18
+ {"role": "user", "content": question},
19
+ {"role": "assistant", "content": answer}
20
+ ]
21
+
22
+ return messages