Spaces:
Build error
Build error
Update chatbot.py
Browse files- chatbot.py +9 -4
chatbot.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
| 3 |
-
#
|
| 4 |
generator = pipeline("text-generation", model="gpt2")
|
| 5 |
|
| 6 |
def generate_answer(question):
|
| 7 |
-
# Generate an answer using
|
| 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 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|