ttherapist / app.py
hackergeek98's picture
Update app.py
af31357 verified
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load your fine-tuned GPT-2 model from Hugging Face
MODEL_NAME = "hackergeek98/therapist01" # Replace with your model name
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
# Initialize conversation history
conversation_history = ""
# Function to generate responses using GPT-2
def generate_response(user_input):
global conversation_history
# Update conversation history with user input
conversation_history += f"User: {user_input}\n"
# Tokenize the conversation history
inputs = tokenizer(conversation_history, return_tensors="pt", truncation=True, max_length=1024)
# Generate a response from the model
outputs = model.generate(inputs['input_ids'], max_length=1024, num_return_sequences=1, no_repeat_ngram_size=2)
# Decode the model's output
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Update conversation history with the model's response
conversation_history += f"Therapist: {response}\n"
# Return the therapist's response
return response
# Create Gradio interface
interface = gr.Interface(
fn=generate_response,
inputs=gr.Textbox(label="Enter your message", lines=2),
outputs=gr.Textbox(label="Therapist Response", lines=2),
title="Virtual Therapist",
description="A fine-tuned GPT-2 model acting as a virtual therapist. Chat with the model and receive responses as if you are talking to a therapist."
)
# Launch the app
if __name__ == "__main__":
interface.launch()