File size: 759 Bytes
1a1c627
7b76ad3
d40057f
1a1c627
370e85e
1a1c627
d40057f
1a1c627
 
 
 
 
 
 
370e85e
d40057f
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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