Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,6 +58,48 @@ demo = gr.ChatInterface(
|
|
| 58 |
),
|
| 59 |
],
|
| 60 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 58 |
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
+
import gradio as gr
|
| 62 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 63 |
+
|
| 64 |
+
# Load your fine-tuned GPT-2 model from Hugging Face
|
| 65 |
+
MODEL_NAME = "hackergeek98/therapist01" # Replace w
|
| 66 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 67 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 68 |
+
|
| 69 |
+
# Initialize conversation history
|
| 70 |
+
conversation_history = ""
|
| 71 |
+
|
| 72 |
+
# Function to generate responses
|
| 73 |
+
def generate_response(user_input):
|
| 74 |
+
global conversation_history
|
| 75 |
+
|
| 76 |
+
# Update conversation history with user input
|
| 77 |
+
conversation_history += f"User: {user_input}\n"
|
| 78 |
+
|
| 79 |
+
# Tokenize the conversation history
|
| 80 |
+
inputs = tokenizer(conversation_history, return_tensors="pt", truncation=True, max_length=1024)
|
| 81 |
+
|
| 82 |
+
# Generate a response from the model
|
| 83 |
+
outputs = model.generate(inputs['input_ids'], max_length=1024, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 84 |
+
|
| 85 |
+
# Decode the model's output
|
| 86 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 87 |
+
|
| 88 |
+
# Update conversation history with the model's response
|
| 89 |
+
conversation_history += f"Therapist: {response}\n"
|
| 90 |
+
|
| 91 |
+
# Return the therapist's response
|
| 92 |
+
return response
|
| 93 |
+
|
| 94 |
+
# Create Gradio interface
|
| 95 |
+
interface = gr.Interface(fn=generate_response,
|
| 96 |
+
inputs=gr.Textbox(label="Enter your message", lines=2),
|
| 97 |
+
outputs=gr.Textbox(label="Therapist Response", lines=2),
|
| 98 |
+
title="Virtual Therapist",
|
| 99 |
+
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.")
|
| 100 |
+
|
| 101 |
+
# Launch the app
|
| 102 |
+
interface.launch()
|
| 103 |
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|