Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def ask_llm(message):
|
| 8 |
response = requests.post(
|
| 9 |
"https://openrouter.ai/api/v1/chat/completions",
|
|
@@ -18,16 +29,70 @@ def ask_llm(message):
|
|
| 18 |
)
|
| 19 |
|
| 20 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
return data["choices"][0]["message"]["content"]
|
| 24 |
|
| 25 |
-
# Gradio Interface
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
ui.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 9 |
|
| 10 |
+
def process_input(message, image):
|
| 11 |
+
if image is None:
|
| 12 |
+
# Text-only query
|
| 13 |
+
return ask_llm(message)
|
| 14 |
+
else:
|
| 15 |
+
# Image + text query
|
| 16 |
+
return ask_llm_with_image(message, image)
|
| 17 |
+
|
| 18 |
def ask_llm(message):
|
| 19 |
response = requests.post(
|
| 20 |
"https://openrouter.ai/api/v1/chat/completions",
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
data = response.json()
|
| 32 |
+
return data["choices"][0]["message"]["content"]
|
| 33 |
+
|
| 34 |
+
def ask_llm_with_image(message, image):
|
| 35 |
+
# Convert image to base64
|
| 36 |
+
buffered = io.BytesIO()
|
| 37 |
+
image.save(buffered, format="PNG")
|
| 38 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 39 |
+
|
| 40 |
+
response = requests.post(
|
| 41 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 42 |
+
headers={
|
| 43 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 44 |
+
"Content-Type": "application/json",
|
| 45 |
+
},
|
| 46 |
+
json={
|
| 47 |
+
"model": "google/gemma-3-27b-it:free", # Note: Check if this model supports vision
|
| 48 |
+
"messages": [
|
| 49 |
+
{
|
| 50 |
+
"role": "user",
|
| 51 |
+
"content": [
|
| 52 |
+
{
|
| 53 |
+
"type": "text",
|
| 54 |
+
"text": message
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"type": "image_url",
|
| 58 |
+
"image_url": {
|
| 59 |
+
"url": f"data:image/png;base64,{img_base64}"
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
]
|
| 63 |
+
}
|
| 64 |
+
]
|
| 65 |
+
}
|
| 66 |
+
)
|
| 67 |
|
| 68 |
+
data = response.json()
|
| 69 |
return data["choices"][0]["message"]["content"]
|
| 70 |
|
| 71 |
+
# Gradio Interface with both text and image inputs
|
| 72 |
+
with gr.Blocks() as ui:
|
| 73 |
+
gr.Markdown("# OpenRouter Free Chat with Image Upload")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column():
|
| 77 |
+
text_input = gr.Textbox(label="Ask your question:", lines=3)
|
| 78 |
+
image_input = gr.Image(label="Upload an image (optional)", type="pil")
|
| 79 |
+
submit_btn = gr.Button("Submit")
|
| 80 |
+
|
| 81 |
+
with gr.Column():
|
| 82 |
+
output = gr.Textbox(label="LLM Reply", lines=10)
|
| 83 |
+
|
| 84 |
+
# Connect the submit button
|
| 85 |
+
submit_btn.click(
|
| 86 |
+
fn=process_input,
|
| 87 |
+
inputs=[text_input, image_input],
|
| 88 |
+
outputs=output
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Also allow pressing Enter in textbox to submit
|
| 92 |
+
text_input.submit(
|
| 93 |
+
fn=process_input,
|
| 94 |
+
inputs=[text_input, image_input],
|
| 95 |
+
outputs=output
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
ui.launch()
|