Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
|
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
13 |
system_message,
|
@@ -27,7 +34,7 @@ def respond(
|
|
27 |
|
28 |
response = ""
|
29 |
|
30 |
-
for message in
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
@@ -39,51 +46,21 @@ def respond(
|
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
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 =
|
81 |
|
82 |
# Generate a response from the model
|
83 |
-
outputs =
|
84 |
|
85 |
# Decode the model's output
|
86 |
-
response =
|
87 |
|
88 |
# Update conversation history with the model's response
|
89 |
conversation_history += f"Therapist: {response}\n"
|
@@ -91,16 +68,29 @@ def generate_response(user_input):
|
|
91 |
# Return the therapist's response
|
92 |
return response
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
# Create Gradio interface
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
# Launch the app
|
102 |
-
interface.launch()
|
103 |
-
|
104 |
-
|
105 |
if __name__ == "__main__":
|
106 |
-
demo.launch()
|
|
|
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 |
+
gpt2_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
11 |
+
gpt2_model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
12 |
|
13 |
+
# Initialize conversation history for GPT-2
|
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,
|
|
|
34 |
|
35 |
response = ""
|
36 |
|
37 |
+
for message in zephyr_client.chat_completion(
|
38 |
messages,
|
39 |
max_tokens=max_tokens,
|
40 |
stream=True,
|
|
|
46 |
response += token
|
47 |
yield response
|
48 |
|
49 |
+
# Function to generate responses using GPT-2
|
50 |
+
def respond_with_gpt2(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = gpt2_tokenizer(conversation_history, return_tensors="pt", truncation=True, max_length=1024)
|
58 |
|
59 |
# Generate a response from the model
|
60 |
+
outputs = gpt2_model.generate(inputs['input_ids'], max_length=1024, num_return_sequences=1, no_repeat_ngram_size=2)
|
61 |
|
62 |
# Decode the model's output
|
63 |
+
response = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
64 |
|
65 |
# Update conversation history with the model's response
|
66 |
conversation_history += f"Therapist: {response}\n"
|
|
|
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 |
+
demo = gr.ChatInterface(
|
82 |
+
respond,
|
83 |
+
additional_inputs=[
|
84 |
+
gr.Dropdown(choices=["Zephyr-7B", "GPT-2 Therapist"], label="Model", value="Zephyr-7B"),
|
85 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
86 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
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 |
+
demo.launch()
|