mlmPenguin commited on
Commit
445520d
·
verified ·
1 Parent(s): 9aacf3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -49
app.py CHANGED
@@ -3,31 +3,23 @@ import gradio as gr
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
 
5
  ### Model Setup ###
6
- # Names for the two models
7
- LLAMA_MODEL_NAME = "meta-llama/Llama-3.2-11B-Vision"
8
- QWEN_MODEL_NAME = "Qwen/Qwen2.5-VL-7B-Instruct"
9
 
10
- # Load Meta-Llama Vision model
11
- llama_tokenizer = AutoTokenizer.from_pretrained(LLAMA_MODEL_NAME)
12
- llama_model = AutoModelForCausalLM.from_pretrained(LLAMA_MODEL_NAME, device_map="auto")
13
- llama_pipe = pipeline(
14
- "text-generation",
15
- model=llama_model,
16
- tokenizer=llama_tokenizer
17
- )
18
 
19
- # Load Qwen
20
- qwen_tokenizer = AutoTokenizer.from_pretrained(QWEN_MODEL_NAME)
21
- qwen_model = AutoModelForCausalLM.from_pretrained(QWEN_MODEL_NAME, device_map="auto")
22
- qwen_pipe = pipeline(
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
- Given a prompt and a model choice, generate a response.
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 == "Llama":
39
- result = llama_pipe(prompt, **kwargs)
40
- else: # "Qwen"
41
- result = qwen_pipe(prompt, **kwargs)
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, decide which model responds,
48
- and update the conversation.
49
  """
50
  if chat_history is None:
51
  chat_history = []
52
 
53
- # If the user provided input, record it and choose a random model to respond.
54
  if user_input.strip():
55
  chat_history.append(("User", user_input.strip()))
56
- selected_model = random.choice(["Llama", "Qwen"])
57
  else:
58
- # If no user input, let the models take turns.
59
  if not chat_history:
60
- selected_model = random.choice(["Llama", "Qwen"])
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 == "Llama":
65
- selected_model = "Qwen"
66
- elif last_speaker == "Qwen":
67
- selected_model = "Llama"
68
  else:
69
- selected_model = random.choice(["Llama", "Qwen"])
70
 
71
- # Construct the conversation prompt.
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 both the updated chat for display and as state.
82
  return chat_history, chat_history
83
 
84
  ### Gradio Interface ###
85
  with gr.Blocks() as demo:
86
- gr.Markdown("# Group Chat: Meta-Llama Vision and Qwen Models")
87
  gr.Markdown(
88
- "This demo allows two models to converse with each other. "
89
- "Leave the textbox blank to let the models continue the conversation automatically, "
90
- "or type a message to interject (a random model will then respond)."
91
  )
92
 
93
- # Chatbot display component.
94
  chatbot = gr.Chatbot()
95
 
96
- # Input row: text input and a send button.
97
  with gr.Row():
98
  user_message = gr.Textbox(
99
- placeholder="Type a message or leave blank to continue conversation...",
100
  show_label=False
101
  )
102
  send_btn = gr.Button("Send")
103
 
104
- # Maintain the conversation history in Gradio's state.
105
  state = gr.State([])
106
 
107
- # Update the chat when the send button is clicked.
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()