SHIKARICHACHA commited on
Commit
8cb4e28
·
verified ·
1 Parent(s): 9dc1a4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -28
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import os
3
  from openai import OpenAI
@@ -6,7 +8,7 @@ from openai import OpenAI
6
  OPENROUTER_API_KEY = "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"
7
 
8
  # Available models
9
- MODELS = {
10
  "Mistral Small": "mistralai/mistral-small-3.2-24b-instruct:free",
11
  "Claude 3 Haiku": "anthropic/claude-3-haiku:free",
12
  "Gemini Pro": "google/gemini-2.5-pro-exp-03-25",
@@ -16,10 +18,39 @@ MODELS = {
16
  "Llama 3": "meta-llama/llama-3-70b-instruct:free",
17
  }
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Initialize chat history
20
  history = []
21
 
22
- def chat_with_ai(message, model_name, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  """Chat with the selected OpenRouter model"""
24
  try:
25
  # Initialize OpenAI client with OpenRouter base URL
@@ -34,8 +65,30 @@ def chat_with_ai(message, model_name, history):
34
  messages.append({"role": "user", "content": human})
35
  messages.append({"role": "assistant", "content": assistant})
36
 
37
- # Add the current message
38
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  # Create the completion request
41
  completion = client.chat.completions.create(
@@ -54,14 +107,16 @@ def chat_with_ai(message, model_name, history):
54
  return f"Error: {str(e)}"
55
 
56
  # Create the Gradio interface
57
- with gr.Blocks(title="OpenRouter AI Chat Interface", css="style.css") as demo:
58
  gr.Markdown(
59
  """
60
- # 💬 OpenRouter AI Chat Interface
 
 
61
 
62
- Chat with multiple AI models powered by OpenRouter API.
63
 
64
- *Select from various large language models and start chatting!*
65
  """
66
  )
67
 
@@ -69,48 +124,142 @@ with gr.Blocks(title="OpenRouter AI Chat Interface", css="style.css") as demo:
69
  with gr.Column(scale=4):
70
  # Chat interface
71
  chatbot = gr.Chatbot(height=500, label="Conversation")
 
 
 
 
72
  msg = gr.Textbox(label="Your message", placeholder="Type your message here...")
73
 
74
  with gr.Row():
75
  submit_btn = gr.Button("Send", variant="primary")
76
  clear_btn = gr.Button("Clear Chat")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  with gr.Column(scale=1):
79
  # Model selection
80
  with gr.Group():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  model_dropdown = gr.Dropdown(
82
- choices=list(MODELS.keys()),
83
  value="Mistral Small",
84
  label="Select AI Model",
85
- info="Choose from different language models"
86
  )
87
 
88
- gr.Markdown(
89
- """
90
- ### Available Models
91
- - **Mistral Small**: Powerful language model from Mistral AI
92
- - **Claude 3 Haiku**: Fast and efficient model from Anthropic
93
- - **Gemini Pro**: Google's advanced language model
94
- - **Qwen**: Alibaba's large language model
95
- - **Mistral 3.1**: Earlier version of Mistral's model
96
- - **Gemma**: Google's lightweight language model
97
- - **Llama 3**: Meta's large language model
98
- """
99
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Set up the submit action
102
- def respond(message, chat_history, model):
103
  if not message.strip():
104
  return "", chat_history
105
 
106
- bot_message = chat_with_ai(message, model, chat_history)
107
- chat_history.append((message, bot_message))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  return "", chat_history
109
 
110
  # Connect the components
111
- submit_btn.click(respond, [msg, chatbot, model_dropdown], [msg, chatbot])
112
- msg.submit(respond, [msg, chatbot, model_dropdown], [msg, chatbot])
113
- clear_btn.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  # For Hugging Face Spaces compatibility
116
  if __name__ == "__main__":
 
1
+ Multi ai
2
+
3
  import gradio as gr
4
  import os
5
  from openai import OpenAI
 
8
  OPENROUTER_API_KEY = "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"
9
 
10
  # Available models
11
+ TEXT_MODELS = {
12
  "Mistral Small": "mistralai/mistral-small-3.2-24b-instruct:free",
13
  "Claude 3 Haiku": "anthropic/claude-3-haiku:free",
14
  "Gemini Pro": "google/gemini-2.5-pro-exp-03-25",
 
18
  "Llama 3": "meta-llama/llama-3-70b-instruct:free",
19
  }
20
 
21
+ # Available image models
22
+ IMAGE_MODELS = {
23
+ "Kimi Vision": "moonshotai/kimi-vl-a3b-thinking:free",
24
+ "Claude 3 Opus Vision": "anthropic/claude-3-opus-vision:free",
25
+ "Claude 3 Sonnet Vision": "anthropic/claude-3-sonnet-vision:free",
26
+ "Gemini Pro Vision": "google/gemini-pro-vision:free",
27
+ "GPT-4 Vision": "openai/gpt-4-vision:free",
28
+ "Llava": "llava/llava-1.6-34b-vision:free",
29
+ }
30
+
31
+ # Combined models for dropdown
32
+ MODELS = {**TEXT_MODELS, **IMAGE_MODELS}
33
+
34
  # Initialize chat history
35
  history = []
36
 
37
+ import base64
38
+ from PIL import Image
39
+ import io
40
+
41
+ def image_to_base64(image):
42
+ """Convert an image to base64 encoding"""
43
+ # If image is a file path
44
+ if isinstance(image, str):
45
+ with open(image, "rb") as img_file:
46
+ return base64.b64encode(img_file.read()).decode()
47
+
48
+ # If image is already a PIL Image
49
+ buffered = io.BytesIO()
50
+ image.save(buffered, format="JPEG")
51
+ return base64.b64encode(buffered.getvalue()).decode()
52
+
53
+ def chat_with_ai(message, model_name, history, image=None):
54
  """Chat with the selected OpenRouter model"""
55
  try:
56
  # Initialize OpenAI client with OpenRouter base URL
 
65
  messages.append({"role": "user", "content": human})
66
  messages.append({"role": "assistant", "content": assistant})
67
 
68
+ # Check if we're using an image model and have an image
69
+ if model_name in IMAGE_MODELS and image is not None:
70
+ # Convert image to base64
71
+ img_base64 = image_to_base64(image)
72
+
73
+ # Add the current message with image
74
+ messages.append({
75
+ "role": "user",
76
+ "content": [
77
+ {
78
+ "type": "text",
79
+ "text": message
80
+ },
81
+ {
82
+ "type": "image_url",
83
+ "image_url": {
84
+ "url": f"data:image/jpeg;base64,{img_base64}"
85
+ }
86
+ }
87
+ ]
88
+ })
89
+ else:
90
+ # Add the current message (text only)
91
+ messages.append({"role": "user", "content": message})
92
 
93
  # Create the completion request
94
  completion = client.chat.completions.create(
 
107
  return f"Error: {str(e)}"
108
 
109
  # Create the Gradio interface
110
+ with gr.Blocks(title="OpenRouter AI Multi-Modal Interface", css="style.css") as demo:
111
  gr.Markdown(
112
  """
113
+ # 🤖 OpenRouter AI Multi-Modal Interface
114
+
115
+ Chat with multiple AI models powered by OpenRouter API - both text-only and vision models!
116
 
117
+ *Select your model type (Text or Image), choose a specific model, and start interacting!*
118
 
119
+ **All responses from image models will be provided in English**
120
  """
121
  )
122
 
 
124
  with gr.Column(scale=4):
125
  # Chat interface
126
  chatbot = gr.Chatbot(height=500, label="Conversation")
127
+
128
+ # Image upload (initially hidden)
129
+ image_input = gr.Image(type="pil", label="Upload Image for Analysis", visible=False)
130
+
131
  msg = gr.Textbox(label="Your message", placeholder="Type your message here...")
132
 
133
  with gr.Row():
134
  submit_btn = gr.Button("Send", variant="primary")
135
  clear_btn = gr.Button("Clear Chat")
136
+
137
+ # Instructions for image models
138
+ image_instructions = gr.Markdown(
139
+ """
140
+ ### 📷 Image Analysis Instructions
141
+ 1. Upload an image using the panel above
142
+ 2. Ask a question about the image
143
+ 3. The AI will analyze the image and respond in English
144
+
145
+ **Example prompts:**
146
+ - "What's in this image?"
147
+ - "Describe this scene in detail"
148
+ - "What objects can you identify?"
149
+ - "What's happening in this picture?"
150
+ """,
151
+ visible=False
152
+ )
153
 
154
  with gr.Column(scale=1):
155
  # Model selection
156
  with gr.Group():
157
+ model_type = gr.Radio(
158
+ choices=["Text Models", "Image Models"],
159
+ value="Text Models",
160
+ label="Model Type",
161
+ info="Choose between text-only or vision models"
162
+ )
163
+
164
+ # Function to update model dropdown based on selection
165
+ def update_model_choices(model_type):
166
+ if model_type == "Text Models":
167
+ return {
168
+ model_dropdown: gr.Dropdown.update(choices=list(TEXT_MODELS.keys()), value="Mistral Small"),
169
+ image_input: gr.Image.update(visible=False),
170
+ image_instructions: gr.Markdown.update(visible=False)
171
+ }
172
+ else: # Image Models
173
+ return {
174
+ model_dropdown: gr.Dropdown.update(choices=list(IMAGE_MODELS.keys()), value="Kimi Vision"),
175
+ image_input: gr.Image.update(visible=True),
176
+ image_instructions: gr.Markdown.update(visible=True)
177
+ }
178
+
179
  model_dropdown = gr.Dropdown(
180
+ choices=list(TEXT_MODELS.keys()),
181
  value="Mistral Small",
182
  label="Select AI Model",
183
+ info="Choose from different AI models"
184
  )
185
 
186
+ # Connect the radio button to update the dropdown and show/hide image components
187
+ model_type.change(fn=update_model_choices, inputs=model_type, outputs=[model_dropdown, image_input, image_instructions])
188
+
189
+ with gr.Tabs():
190
+ with gr.TabItem("Text Models"):
191
+ gr.Markdown(
192
+ """
193
+ ### Available Text Models
194
+ - **Mistral Small**: Powerful language model from Mistral AI
195
+ - **Claude 3 Haiku**: Fast and efficient model from Anthropic
196
+ - **Gemini Pro**: Google's advanced language model
197
+ - **Qwen**: Alibaba's large language model
198
+ - **Mistral 3.1**: Earlier version of Mistral's model
199
+ - **Gemma**: Google's lightweight language model
200
+ - **Llama 3**: Meta's large language model
201
+ """
202
+ )
203
+
204
+ with gr.TabItem("Image Models"):
205
+ gr.Markdown(
206
+ """
207
+ ### Available Image Models
208
+ - **Kimi Vision**: Moonshot AI's vision-language model
209
+ - **Claude 3 Opus Vision**: Anthropic's premium vision model
210
+ - **Claude 3 Sonnet Vision**: Mid-tier vision model from Anthropic
211
+ - **Gemini Pro Vision**: Google's multimodal vision model
212
+ - **GPT-4 Vision**: OpenAI's vision-enabled GPT model
213
+ - **Llava**: Open-source vision-language model
214
+
215
+ *Note: All responses will be provided in English*
216
+ """
217
+ )
218
 
219
  # Set up the submit action
220
+ def respond(message, chat_history, model, image, model_type):
221
  if not message.strip():
222
  return "", chat_history
223
 
224
+ # Check if we need to use image
225
+ use_image = model_type == "Image Models" and image is not None
226
+
227
+ # Add a note if using image model but no image uploaded
228
+ if model_type == "Image Models" and image is None:
229
+ return "", chat_history + [(message, "Please upload an image first before sending your message.")]
230
+
231
+ # Process with or without image
232
+ if use_image:
233
+ bot_message = chat_with_ai(message, model, chat_history, image)
234
+ # Add a note that the response is in English
235
+ if not bot_message.startswith("Error:"):
236
+ display_message = f"{bot_message}\n\n*Response provided in English*"
237
+ else:
238
+ display_message = bot_message
239
+ else:
240
+ bot_message = chat_with_ai(message, model, chat_history)
241
+ display_message = bot_message
242
+
243
+ chat_history.append((message, display_message))
244
  return "", chat_history
245
 
246
  # Connect the components
247
+ submit_btn.click(
248
+ respond,
249
+ [msg, chatbot, model_dropdown, image_input, model_type],
250
+ [msg, chatbot]
251
+ )
252
+ msg.submit(
253
+ respond,
254
+ [msg, chatbot, model_dropdown, image_input, model_type],
255
+ [msg, chatbot]
256
+ )
257
+
258
+ # Clear chat and image
259
+ def clear_all():
260
+ return None, None
261
+
262
+ clear_btn.click(clear_all, None, [chatbot, image_input], queue=False)
263
 
264
  # For Hugging Face Spaces compatibility
265
  if __name__ == "__main__":