Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
-
import base64
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import io
|
| 7 |
|
| 8 |
# OpenRouter API key
|
| 9 |
OPENROUTER_API_KEY = "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"
|
|
@@ -11,28 +8,19 @@ OPENROUTER_API_KEY = "sk-or-v1-e2894f0aab5790d69078bd57090b6001bf34f80057bea8fba
|
|
| 11 |
# Available models
|
| 12 |
MODELS = {
|
| 13 |
"Mistral Small": "mistralai/mistral-small-3.2-24b-instruct:free",
|
| 14 |
-
"
|
| 15 |
"Gemini Pro": "google/gemini-2.5-pro-exp-03-25",
|
| 16 |
-
"Qwen
|
| 17 |
"Mistral 3.1": "mistralai/mistral-small-3.1-24b-instruct:free",
|
| 18 |
"Gemma": "google/gemma-3-4b-it:free",
|
| 19 |
-
"Llama 3
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# If image is a file path
|
| 25 |
-
if isinstance(image, str):
|
| 26 |
-
with open(image, "rb") as img_file:
|
| 27 |
-
return base64.b64encode(img_file.read()).decode()
|
| 28 |
-
|
| 29 |
-
# If image is already a PIL Image
|
| 30 |
-
buffered = io.BytesIO()
|
| 31 |
-
image.save(buffered, format="JPEG")
|
| 32 |
-
return base64.b64encode(buffered.getvalue()).decode()
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
"""
|
| 36 |
try:
|
| 37 |
# Initialize OpenAI client with OpenRouter base URL
|
| 38 |
client = OpenAI(
|
|
@@ -40,8 +28,14 @@ def analyze_image(image, prompt, model_name):
|
|
| 40 |
api_key=OPENROUTER_API_KEY,
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Create the completion request
|
| 47 |
completion = client.chat.completions.create(
|
|
@@ -50,23 +44,7 @@ def analyze_image(image, prompt, model_name):
|
|
| 50 |
"X-Title": "Gradio OpenRouter Interface",
|
| 51 |
},
|
| 52 |
model=MODELS[model_name],
|
| 53 |
-
messages=
|
| 54 |
-
{
|
| 55 |
-
"role": "user",
|
| 56 |
-
"content": [
|
| 57 |
-
{
|
| 58 |
-
"type": "text",
|
| 59 |
-
"text": prompt
|
| 60 |
-
},
|
| 61 |
-
{
|
| 62 |
-
"type": "image_url",
|
| 63 |
-
"image_url": {
|
| 64 |
-
"url": f"data:image/jpeg;base64,{img_base64}"
|
| 65 |
-
}
|
| 66 |
-
}
|
| 67 |
-
]
|
| 68 |
-
}
|
| 69 |
-
]
|
| 70 |
)
|
| 71 |
|
| 72 |
# Return the model's response
|
|
@@ -76,92 +54,63 @@ def analyze_image(image, prompt, model_name):
|
|
| 76 |
return f"Error: {str(e)}"
|
| 77 |
|
| 78 |
# Create the Gradio interface
|
| 79 |
-
with gr.Blocks(title="OpenRouter AI
|
| 80 |
gr.Markdown(
|
| 81 |
"""
|
| 82 |
-
#
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
*
|
| 87 |
"""
|
| 88 |
)
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
-
with gr.Column():
|
| 92 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Group():
|
| 94 |
-
image_input = gr.Image(type="pil", label="Upload Image")
|
| 95 |
-
prompt_input = gr.Textbox(label="Your Question", placeholder="What is in this image?", value="What is in this image?")
|
| 96 |
model_dropdown = gr.Dropdown(
|
| 97 |
choices=list(MODELS.keys()),
|
| 98 |
value="Mistral Small",
|
| 99 |
label="Select AI Model",
|
| 100 |
-
info="Choose from different
|
| 101 |
)
|
| 102 |
-
submit_button = gr.Button("Analyze Image", variant="primary")
|
| 103 |
-
|
| 104 |
-
with gr.Column():
|
| 105 |
-
# Output component with custom styling
|
| 106 |
-
with gr.Group():
|
| 107 |
-
output_text = gr.Textbox(label="AI Response", lines=12)
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
|
| 122 |
# Set up the submit action
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
outputs=output_text
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
# Add example
|
| 130 |
-
gr.Examples(
|
| 131 |
-
examples=[
|
| 132 |
-
["examples/nature.jpg", "What is in this image?", "Mistral Small"],
|
| 133 |
-
["examples/nature.jpg", "Describe this scene in detail", "Kimi Vision"],
|
| 134 |
-
],
|
| 135 |
-
inputs=[image_input, prompt_input, model_dropdown],
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
# Create examples directory if it doesn't exist
|
| 139 |
-
os.makedirs("examples", exist_ok=True)
|
| 140 |
-
|
| 141 |
-
# Download example image if it doesn't exist
|
| 142 |
-
def download_example_image():
|
| 143 |
-
if not os.path.exists("examples/nature.jpg"):
|
| 144 |
-
import requests
|
| 145 |
-
from PIL import Image
|
| 146 |
-
from io import BytesIO
|
| 147 |
-
|
| 148 |
-
# URL of the example image
|
| 149 |
-
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
print("Example image downloaded successfully!")
|
| 160 |
-
else:
|
| 161 |
-
print(f"Failed to download image. Status code: {response.status_code}")
|
| 162 |
-
|
| 163 |
-
# Download example image before launching the app
|
| 164 |
-
download_example_image()
|
| 165 |
|
| 166 |
# For Hugging Face Spaces compatibility
|
| 167 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# OpenRouter API key
|
| 6 |
OPENROUTER_API_KEY = "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"
|
|
|
|
| 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",
|
| 13 |
+
"Qwen": "qwen/qwen2.5-32b-instruct:free",
|
| 14 |
"Mistral 3.1": "mistralai/mistral-small-3.1-24b-instruct:free",
|
| 15 |
"Gemma": "google/gemma-3-4b-it:free",
|
| 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
|
| 26 |
client = OpenAI(
|
|
|
|
| 28 |
api_key=OPENROUTER_API_KEY,
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Format the conversation history for the API
|
| 32 |
+
messages = []
|
| 33 |
+
for human, assistant in 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(
|
|
|
|
| 44 |
"X-Title": "Gradio OpenRouter Interface",
|
| 45 |
},
|
| 46 |
model=MODELS[model_name],
|
| 47 |
+
messages=messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
# Return the model's response
|
|
|
|
| 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 |
|
| 68 |
with gr.Row():
|
| 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__":
|