File size: 1,647 Bytes
97cce4f
 
 
 
d2546a9
97cce4f
 
 
d08a780
 
 
97cce4f
 
d08a780
 
 
 
 
 
 
97cce4f
d08a780
 
97cce4f
d08a780
97cce4f
d08a780
 
 
 
 
97cce4f
 
 
 
d08a780
 
97cce4f
d08a780
97cce4f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load your fine-tuned GPT-2 model from Hugging Face
MODEL_NAME = "hackergeek98/therapist01"  # Replace w 
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)

# Initialize conversation history
conversation_history = ""

# Function to generate responses
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
interface.launch()