Spaces:
Build error
Build error
Update chatbot.py
Browse files- chatbot.py +14 -15
chatbot.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
-
from
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 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 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
answer
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|