Spaces:
Build error
Build error
Download chatbot.py from dschandra/Question_And_Answer: direct link, hf CLI and curl.
- Browser
- Download file 759 Bytes
-
https://huggingface.co/spaces/dschandra/Question_And_Answer/resolve/main/chatbot.py
- Command line
-
hf download hf://spaces/dschandra/Question_And_Answer/chatbot.py
-
curl -L -o chatbot.py https://huggingface.co/spaces/dschandra/Question_And_Answer/resolve/main/chatbot.py
759 Bytes
| from transformers import pipeline | |
| # Initialize the GPT-2 model using Hugging Face's pipeline for text generation | |
| generator = pipeline("text-generation", model="gpt2") | |
| def generate_answer(question): | |
| # Generate an answer using GPT-2 | |
| response = generator(question, max_length=100, num_return_sequences=1) | |
| # Extract the generated text and return it as the answer | |
| answer = response[0]['generated_text'] | |
| # Clean the answer to remove the prompt (question) | |
| answer = answer.replace(question, '').strip() | |
| # Format the answer for Gradio's chatbot component using the 'messages' format | |
| messages = [ | |
| {"role": "user", "content": question}, | |
| {"role": "assistant", "content": answer} | |
| ] | |
| return messages | |