Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Load your fine-tuned GPT-2 model from Hugging Face
|
| 5 |
+
MODEL_NAME = "hackergeek98/finetuned-gpt2" # Replace with your model
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 8 |
+
|
| 9 |
+
# Function to generate responses
|
| 10 |
+
def generate_response(user_input):
|
| 11 |
+
# Tokenize the input
|
| 12 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
# Generate a response
|
| 15 |
+
outputs = model.generate(inputs['input_ids'], max_length=1000, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 16 |
+
|
| 17 |
+
# Decode the output and return the result
|
| 18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
return response
|
| 20 |
+
|
| 21 |
+
# Create Gradio interface
|
| 22 |
+
interface = gr.Interface(fn=generate_response,
|
| 23 |
+
inputs=gr.Textbox(label="Enter your message"),
|
| 24 |
+
outputs=gr.Textbox(label="Therapist Response"),
|
| 25 |
+
title="Virtual Therapist",
|
| 26 |
+
description="A fine-tuned GPT-2 model acting as a virtual therapist.")
|
| 27 |
+
|
| 28 |
+
# Launch the app
|
| 29 |
+
interface.launch()
|