Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,31 +3,23 @@ import gradio as gr
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
|
5 |
### Model Setup ###
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
# Load
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
"text-generation",
|
15 |
-
model=llama_model,
|
16 |
-
tokenizer=llama_tokenizer
|
17 |
-
)
|
18 |
|
19 |
-
# Load
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
"text-generation",
|
24 |
-
model=qwen_model,
|
25 |
-
tokenizer=qwen_tokenizer
|
26 |
-
)
|
27 |
|
28 |
def generate_response(prompt: str, model_choice: str) -> str:
|
29 |
"""
|
30 |
-
|
31 |
"""
|
32 |
kwargs = {
|
33 |
"max_length": 256,
|
@@ -35,40 +27,38 @@ def generate_response(prompt: str, model_choice: str) -> str:
|
|
35 |
"top_p": 0.95,
|
36 |
"temperature": 0.9,
|
37 |
}
|
38 |
-
if model_choice == "
|
39 |
-
result =
|
40 |
-
else: # "
|
41 |
-
result =
|
42 |
-
# Extract and return the generated text
|
43 |
return result[0]["generated_text"]
|
44 |
|
45 |
def chat_logic(user_input: str, chat_history: list):
|
46 |
"""
|
47 |
-
Build the conversation prompt from the history,
|
48 |
-
and update the conversation.
|
49 |
"""
|
50 |
if chat_history is None:
|
51 |
chat_history = []
|
52 |
|
53 |
-
# If the user
|
54 |
if user_input.strip():
|
55 |
chat_history.append(("User", user_input.strip()))
|
56 |
-
selected_model = random.choice(["
|
57 |
else:
|
58 |
-
#
|
59 |
if not chat_history:
|
60 |
-
selected_model = random.choice(["
|
61 |
else:
|
62 |
-
# If the last speaker was a model, switch to the other model.
|
63 |
last_speaker = chat_history[-1][0]
|
64 |
-
if last_speaker == "
|
65 |
-
selected_model = "
|
66 |
-
elif last_speaker == "
|
67 |
-
selected_model = "
|
68 |
else:
|
69 |
-
selected_model = random.choice(["
|
70 |
|
71 |
-
#
|
72 |
prompt = ""
|
73 |
for speaker, message in chat_history:
|
74 |
prompt += f"{speaker}: {message}\n"
|
@@ -78,38 +68,38 @@ def chat_logic(user_input: str, chat_history: list):
|
|
78 |
model_response = generate_response(prompt, selected_model)
|
79 |
chat_history.append((selected_model, model_response))
|
80 |
|
81 |
-
# Return
|
82 |
return chat_history, chat_history
|
83 |
|
84 |
### Gradio Interface ###
|
85 |
with gr.Blocks() as demo:
|
86 |
-
gr.Markdown("# Group Chat:
|
87 |
gr.Markdown(
|
88 |
-
"This demo
|
89 |
-
"Leave the textbox blank to let the models
|
90 |
-
"or type a message to interject (a random model will then
|
91 |
)
|
92 |
|
93 |
-
#
|
94 |
chatbot = gr.Chatbot()
|
95 |
|
96 |
-
# Input row:
|
97 |
with gr.Row():
|
98 |
user_message = gr.Textbox(
|
99 |
-
placeholder="Type
|
100 |
show_label=False
|
101 |
)
|
102 |
send_btn = gr.Button("Send")
|
103 |
|
104 |
-
# Maintain the conversation history
|
105 |
state = gr.State([])
|
106 |
|
107 |
-
#
|
108 |
send_btn.click(
|
109 |
fn=chat_logic,
|
110 |
inputs=[user_message, state],
|
111 |
outputs=[chatbot, state]
|
112 |
)
|
113 |
|
114 |
-
# Launch the Space
|
115 |
demo.launch()
|
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
|
5 |
### Model Setup ###
|
6 |
+
# Define the two model names.
|
7 |
+
MODEL1_NAME = "Qwen/Qwen2.5-14B-Instruct-1M" # We'll refer to this as Qwen14B.
|
8 |
+
MODEL2_NAME = "Qwen/Qwen2.5-VL-7B-Instruct" # We'll refer to this as QwenVL.
|
9 |
|
10 |
+
# Load Qwen14B model.
|
11 |
+
tokenizer1 = AutoTokenizer.from_pretrained(MODEL1_NAME)
|
12 |
+
model1 = AutoModelForCausalLM.from_pretrained(MODEL1_NAME, device_map="auto")
|
13 |
+
pipe1 = pipeline("text-generation", model=model1, tokenizer=tokenizer1)
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Load QwenVL model.
|
16 |
+
tokenizer2 = AutoTokenizer.from_pretrained(MODEL2_NAME)
|
17 |
+
model2 = AutoModelForCausalLM.from_pretrained(MODEL2_NAME, device_map="auto")
|
18 |
+
pipe2 = pipeline("text-generation", model=model2, tokenizer=tokenizer2)
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def generate_response(prompt: str, model_choice: str) -> str:
|
21 |
"""
|
22 |
+
Generate a response given the conversation prompt using the chosen model.
|
23 |
"""
|
24 |
kwargs = {
|
25 |
"max_length": 256,
|
|
|
27 |
"top_p": 0.95,
|
28 |
"temperature": 0.9,
|
29 |
}
|
30 |
+
if model_choice == "Qwen14B":
|
31 |
+
result = pipe1(prompt, **kwargs)
|
32 |
+
else: # model_choice == "QwenVL"
|
33 |
+
result = pipe2(prompt, **kwargs)
|
|
|
34 |
return result[0]["generated_text"]
|
35 |
|
36 |
def chat_logic(user_input: str, chat_history: list):
|
37 |
"""
|
38 |
+
Build the conversation prompt from the history, choose which model responds,
|
39 |
+
generate the response, and update the conversation.
|
40 |
"""
|
41 |
if chat_history is None:
|
42 |
chat_history = []
|
43 |
|
44 |
+
# If the user provides input, add it to the history and randomly choose a model.
|
45 |
if user_input.strip():
|
46 |
chat_history.append(("User", user_input.strip()))
|
47 |
+
selected_model = random.choice(["Qwen14B", "QwenVL"])
|
48 |
else:
|
49 |
+
# When no user input is provided, let the models alternate.
|
50 |
if not chat_history:
|
51 |
+
selected_model = random.choice(["Qwen14B", "QwenVL"])
|
52 |
else:
|
|
|
53 |
last_speaker = chat_history[-1][0]
|
54 |
+
if last_speaker == "Qwen14B":
|
55 |
+
selected_model = "QwenVL"
|
56 |
+
elif last_speaker == "QwenVL":
|
57 |
+
selected_model = "Qwen14B"
|
58 |
else:
|
59 |
+
selected_model = random.choice(["Qwen14B", "QwenVL"])
|
60 |
|
61 |
+
# Build the prompt from the conversation history.
|
62 |
prompt = ""
|
63 |
for speaker, message in chat_history:
|
64 |
prompt += f"{speaker}: {message}\n"
|
|
|
68 |
model_response = generate_response(prompt, selected_model)
|
69 |
chat_history.append((selected_model, model_response))
|
70 |
|
71 |
+
# Return the updated conversation for display and to maintain state.
|
72 |
return chat_history, chat_history
|
73 |
|
74 |
### Gradio Interface ###
|
75 |
with gr.Blocks() as demo:
|
76 |
+
gr.Markdown("# Group Chat: Qwen Models")
|
77 |
gr.Markdown(
|
78 |
+
"This demo features two Qwen models conversing with each other. "
|
79 |
+
"Leave the textbox blank to let the models converse automatically, "
|
80 |
+
"or type a message to interject (a random model will then reply)."
|
81 |
)
|
82 |
|
83 |
+
# Chat display component.
|
84 |
chatbot = gr.Chatbot()
|
85 |
|
86 |
+
# Input row: textbox and send button.
|
87 |
with gr.Row():
|
88 |
user_message = gr.Textbox(
|
89 |
+
placeholder="Type your message here or leave blank...",
|
90 |
show_label=False
|
91 |
)
|
92 |
send_btn = gr.Button("Send")
|
93 |
|
94 |
+
# Maintain the conversation history.
|
95 |
state = gr.State([])
|
96 |
|
97 |
+
# On clicking the button, update the conversation.
|
98 |
send_btn.click(
|
99 |
fn=chat_logic,
|
100 |
inputs=[user_message, state],
|
101 |
outputs=[chatbot, state]
|
102 |
)
|
103 |
|
104 |
+
# Launch the Space.
|
105 |
demo.launch()
|