Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
-
# Initialize the Zephyr-7B client
|
| 6 |
-
zephyr_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
-
|
| 8 |
# Load your fine-tuned GPT-2 model from Hugging Face
|
| 9 |
MODEL_NAME = "hackergeek98/therapist01" # Replace with your model name
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# Initialize conversation history
|
| 14 |
conversation_history = ""
|
| 15 |
|
| 16 |
-
# Function to generate responses using Zephyr-7B
|
| 17 |
-
def respond_with_zephyr(
|
| 18 |
-
message,
|
| 19 |
-
history: list[tuple[str, str]],
|
| 20 |
-
system_message,
|
| 21 |
-
max_tokens,
|
| 22 |
-
temperature,
|
| 23 |
-
top_p,
|
| 24 |
-
):
|
| 25 |
-
messages = [{"role": "system", "content": system_message}]
|
| 26 |
-
|
| 27 |
-
for val in history:
|
| 28 |
-
if val[0]:
|
| 29 |
-
messages.append({"role": "user", "content": val[0]})
|
| 30 |
-
if val[1]:
|
| 31 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 32 |
-
|
| 33 |
-
messages.append({"role": "user", "content": message})
|
| 34 |
-
|
| 35 |
-
response = ""
|
| 36 |
-
|
| 37 |
-
for message in zephyr_client.chat_completion(
|
| 38 |
-
messages,
|
| 39 |
-
max_tokens=max_tokens,
|
| 40 |
-
stream=True,
|
| 41 |
-
temperature=temperature,
|
| 42 |
-
top_p=top_p,
|
| 43 |
-
):
|
| 44 |
-
token = message.choices[0].delta.content
|
| 45 |
-
|
| 46 |
-
response += token
|
| 47 |
-
yield response
|
| 48 |
-
|
| 49 |
# Function to generate responses using GPT-2
|
| 50 |
-
def
|
| 51 |
global conversation_history
|
| 52 |
|
| 53 |
# Update conversation history with user input
|
| 54 |
conversation_history += f"User: {user_input}\n"
|
| 55 |
|
| 56 |
# Tokenize the conversation history
|
| 57 |
-
inputs =
|
| 58 |
|
| 59 |
# Generate a response from the model
|
| 60 |
-
outputs =
|
| 61 |
|
| 62 |
# Decode the model's output
|
| 63 |
-
response =
|
| 64 |
|
| 65 |
# Update conversation history with the model's response
|
| 66 |
conversation_history += f"Therapist: {response}\n"
|
|
@@ -68,29 +31,15 @@ def respond_with_gpt2(user_input):
|
|
| 68 |
# Return the therapist's response
|
| 69 |
return response
|
| 70 |
|
| 71 |
-
# Function to handle the model selection and response generation
|
| 72 |
-
def respond(message, history, model_choice, system_message, max_tokens, temperature, top_p):
|
| 73 |
-
if model_choice == "Zephyr-7B":
|
| 74 |
-
return respond_with_zephyr(message, history, system_message, max_tokens, temperature, top_p)
|
| 75 |
-
elif model_choice == "GPT-2 Therapist":
|
| 76 |
-
return respond_with_gpt2(message)
|
| 77 |
-
else:
|
| 78 |
-
return "Invalid model selection."
|
| 79 |
-
|
| 80 |
# Create Gradio interface
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 88 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 89 |
-
],
|
| 90 |
-
title="Multi-Model Chat Interface",
|
| 91 |
-
description="Choose between Zephyr-7B and a fine-tuned GPT-2 model to chat with."
|
| 92 |
)
|
| 93 |
|
| 94 |
# Launch the app
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
|
|
|
|
| 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/therapist01" # Replace with your model name
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 8 |
|
| 9 |
+
# Initialize conversation history
|
| 10 |
conversation_history = ""
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function to generate responses using GPT-2
|
| 13 |
+
def generate_response(user_input):
|
| 14 |
global conversation_history
|
| 15 |
|
| 16 |
# Update conversation history with user input
|
| 17 |
conversation_history += f"User: {user_input}\n"
|
| 18 |
|
| 19 |
# Tokenize the conversation history
|
| 20 |
+
inputs = tokenizer(conversation_history, return_tensors="pt", truncation=True, max_length=1024)
|
| 21 |
|
| 22 |
# Generate a response from the model
|
| 23 |
+
outputs = model.generate(inputs['input_ids'], max_length=1024, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 24 |
|
| 25 |
# Decode the model's output
|
| 26 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 27 |
|
| 28 |
# Update conversation history with the model's response
|
| 29 |
conversation_history += f"Therapist: {response}\n"
|
|
|
|
| 31 |
# Return the therapist's response
|
| 32 |
return response
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Create Gradio interface
|
| 35 |
+
interface = gr.Interface(
|
| 36 |
+
fn=generate_response,
|
| 37 |
+
inputs=gr.Textbox(label="Enter your message", lines=2),
|
| 38 |
+
outputs=gr.Textbox(label="Therapist Response", lines=2),
|
| 39 |
+
title="Virtual Therapist",
|
| 40 |
+
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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# Launch the app
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
interface.launch()
|