jaykin01 commited on
Commit
5d1ed8d
·
verified ·
1 Parent(s): a1cf7d9

Update app.py

Browse files

I would like to propose adding support for multimodal capabilities to the Hugging Face API, enabling the use of vision models alongside text models. This would allow developers to pass both image and text data into models that can process both modalities

Files changed (1) hide show
  1. app.py +28 -32
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import os
2
  import gradio as gr
3
-
4
  from openai import OpenAI
5
-
6
  from optillm.cot_reflection import cot_reflection
7
  from optillm.rto import round_trip_optimization
8
  from optillm.z3_solver import Z3SymPySolverSystem
@@ -11,7 +9,6 @@ from optillm.plansearch import plansearch
11
  from optillm.leap import leap
12
  from optillm.reread import re2_approach
13
 
14
-
15
  API_KEY = os.environ.get("OPENROUTER_API_KEY")
16
 
17
  def compare_responses(message, model1, approach1, model2, approach2, system_message, max_tokens, temperature, top_p):
@@ -35,11 +32,12 @@ def parse_conversation(messages):
35
  initial_query = "\n".join(conversation)
36
  return system_prompt, initial_query
37
 
38
- def respond(message, history, model, approach, system_message, max_tokens, temperature, top_p):
39
  try:
40
  client = OpenAI(api_key=API_KEY, base_url="https://openrouter.ai/api/v1")
41
  messages = [{"role": "system", "content": system_message}]
42
 
 
43
  for val in history:
44
  if val[0]:
45
  messages.append({"role": "user", "content": val[0]})
@@ -49,21 +47,29 @@ def respond(message, history, model, approach, system_message, max_tokens, tempe
49
  messages.append({"role": "user", "content": message})
50
 
51
  if approach == "none":
 
 
 
 
 
 
 
 
 
 
 
52
  response = client.chat.completions.create(
53
  extra_headers={
54
  "HTTP-Referer": "https://github.com/codelion/optillm",
55
  "X-Title": "optillm"
56
  },
57
- model=model,
58
- messages=messages,
59
- max_tokens=max_tokens,
60
- temperature=temperature,
61
- top_p=top_p,
62
  )
63
  return response.choices[0].message.content
64
  else:
65
  system_prompt, initial_query = parse_conversation(messages)
66
 
 
67
  if approach == 'rto':
68
  final_response, _ = round_trip_optimization(system_prompt, initial_query, client, model)
69
  elif approach == 'z3':
@@ -87,22 +93,10 @@ def respond(message, history, model, approach, system_message, max_tokens, tempe
87
  error_message = f"Error in respond function: {str(e)}\nType: {type(e).__name__}"
88
  print(error_message)
89
 
90
- # for message in client.chat_completion(
91
- # messages,
92
- # max_tokens=max_tokens,
93
- # stream=True,
94
- # temperature=temperature,
95
- # top_p=top_p,
96
- # ):
97
- # token = message.choices[0].delta.content
98
-
99
- # response += token
100
- # yield response
101
-
102
  def create_model_dropdown():
103
  return gr.Dropdown(
104
- [ "meta-llama/llama-3.1-8b-instruct:free", "nousresearch/hermes-3-llama-3.1-405b:free","meta-llama/llama-3.2-1b-instruct:free",
105
- "mistralai/mistral-7b-instruct:free","mistralai/pixtral-12b:free","meta-llama/llama-3.1-70b-instruct:free",
106
  "qwen/qwen-2-7b-instruct:free", "qwen/qwen-2-vl-7b-instruct:free", "google/gemma-2-9b-it:free", "liquid/lfm-40b:free", "meta-llama/llama-3.1-405b-instruct:free",
107
  "openchat/openchat-7b:free", "meta-llama/llama-3.2-90b-vision-instruct:free", "meta-llama/llama-3.2-11b-vision-instruct:free",
108
  "meta-llama/llama-3-8b-instruct:free", "meta-llama/llama-3.2-3b-instruct:free", "microsoft/phi-3-medium-128k-instruct:free",
@@ -135,24 +129,25 @@ with gr.Blocks() as demo:
135
  approach = create_approach_dropdown()
136
  chatbot = gr.Chatbot()
137
  msg = gr.Textbox()
 
138
  with gr.Row():
139
  submit = gr.Button("Submit")
140
  clear = gr.Button("Clear")
141
 
142
- def user(user_message, history):
143
- return "", history + [[user_message, None]]
144
 
145
- def bot(history, model, approach, system_message, max_tokens, temperature, top_p):
146
  user_message = history[-1][0]
147
- bot_message = respond(user_message, history[:-1], model, approach, system_message, max_tokens, temperature, top_p)
148
  history[-1][1] = bot_message
149
  return history
150
 
151
- msg.submit(user, [msg, chatbot], [msg, chatbot]).then(
152
- bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p], chatbot
153
  )
154
- submit.click(user, [msg, chatbot], [msg, chatbot]).then(
155
- bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p], chatbot
156
  )
157
  clear.click(lambda: None, None, chatbot, queue=False)
158
 
@@ -165,6 +160,7 @@ with gr.Blocks() as demo:
165
 
166
  compare_input = gr.Textbox(label="Enter your message for comparison")
167
  compare_button = gr.Button("Compare")
 
168
 
169
  with gr.Row():
170
  output1 = gr.Textbox(label="Response 1")
@@ -177,4 +173,4 @@ with gr.Blocks() as demo:
177
  )
178
 
179
  if __name__ == "__main__":
180
- demo.launch()
 
1
  import os
2
  import gradio as gr
 
3
  from openai import OpenAI
 
4
  from optillm.cot_reflection import cot_reflection
5
  from optillm.rto import round_trip_optimization
6
  from optillm.z3_solver import Z3SymPySolverSystem
 
9
  from optillm.leap import leap
10
  from optillm.reread import re2_approach
11
 
 
12
  API_KEY = os.environ.get("OPENROUTER_API_KEY")
13
 
14
  def compare_responses(message, model1, approach1, model2, approach2, system_message, max_tokens, temperature, top_p):
 
32
  initial_query = "\n".join(conversation)
33
  return system_prompt, initial_query
34
 
35
+ def respond(message, history, model, approach, system_message, max_tokens, temperature, top_p, image=None):
36
  try:
37
  client = OpenAI(api_key=API_KEY, base_url="https://openrouter.ai/api/v1")
38
  messages = [{"role": "system", "content": system_message}]
39
 
40
+ # Add history if available
41
  for val in history:
42
  if val[0]:
43
  messages.append({"role": "user", "content": val[0]})
 
47
  messages.append({"role": "user", "content": message})
48
 
49
  if approach == "none":
50
+ # Prepare the API request data
51
+ data = {
52
+ "model": model,
53
+ "messages": messages,
54
+ "max_tokens": max_tokens,
55
+ "temperature": temperature,
56
+ "top_p": top_p,
57
+ }
58
+ if image:
59
+ data["image"] = image # Add image if provided
60
+
61
  response = client.chat.completions.create(
62
  extra_headers={
63
  "HTTP-Referer": "https://github.com/codelion/optillm",
64
  "X-Title": "optillm"
65
  },
66
+ **data
 
 
 
 
67
  )
68
  return response.choices[0].message.content
69
  else:
70
  system_prompt, initial_query = parse_conversation(messages)
71
 
72
+ # Handle different approaches
73
  if approach == 'rto':
74
  final_response, _ = round_trip_optimization(system_prompt, initial_query, client, model)
75
  elif approach == 'z3':
 
93
  error_message = f"Error in respond function: {str(e)}\nType: {type(e).__name__}"
94
  print(error_message)
95
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  def create_model_dropdown():
97
  return gr.Dropdown(
98
+ [ "meta-llama/llama-3.1-8b-instruct:free", "nousresearch/hermes-3-llama-3.1-405b:free", "meta-llama/llama-3.2-1b-instruct:free",
99
+ "mistralai/mistral-7b-instruct:free", "mistralai/pixtral-12b:free", "meta-llama/llama-3.1-70b-instruct:free",
100
  "qwen/qwen-2-7b-instruct:free", "qwen/qwen-2-vl-7b-instruct:free", "google/gemma-2-9b-it:free", "liquid/lfm-40b:free", "meta-llama/llama-3.1-405b-instruct:free",
101
  "openchat/openchat-7b:free", "meta-llama/llama-3.2-90b-vision-instruct:free", "meta-llama/llama-3.2-11b-vision-instruct:free",
102
  "meta-llama/llama-3-8b-instruct:free", "meta-llama/llama-3.2-3b-instruct:free", "microsoft/phi-3-medium-128k-instruct:free",
 
129
  approach = create_approach_dropdown()
130
  chatbot = gr.Chatbot()
131
  msg = gr.Textbox()
132
+ image = gr.Image(type="pil", label="Upload Image (optional)", optional=True)
133
  with gr.Row():
134
  submit = gr.Button("Submit")
135
  clear = gr.Button("Clear")
136
 
137
+ def user(user_message, history, uploaded_image):
138
+ return "", history + [[user_message, None]], uploaded_image
139
 
140
+ def bot(history, model, approach, system_message, max_tokens, temperature, top_p, uploaded_image):
141
  user_message = history[-1][0]
142
+ bot_message = respond(user_message, history[:-1], model, approach, system_message, max_tokens, temperature, top_p, image=uploaded_image)
143
  history[-1][1] = bot_message
144
  return history
145
 
146
+ msg.submit(user, [msg, chatbot, image], [msg, chatbot, image]).then(
147
+ bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p, image], chatbot
148
  )
149
+ submit.click(user, [msg, chatbot, image], [msg, chatbot, image]).then(
150
+ bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p, image], chatbot
151
  )
152
  clear.click(lambda: None, None, chatbot, queue=False)
153
 
 
160
 
161
  compare_input = gr.Textbox(label="Enter your message for comparison")
162
  compare_button = gr.Button("Compare")
163
+ compare_image = gr.Image(type="pil", label="Upload Image for Comparison", optional=True)
164
 
165
  with gr.Row():
166
  output1 = gr.Textbox(label="Response 1")
 
173
  )
174
 
175
  if __name__ == "__main__":
176
+ demo.launch()