Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import os
|
3 |
+
from io import BytesIO
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import requests
|
7 |
+
|
8 |
+
API_URL = "https://text.pollinations.ai/openai"
|
9 |
+
PROMPT = os.environ.get("PROMPT", "Describe this image.")
|
10 |
+
|
11 |
+
def image_to_base64(image):
|
12 |
+
buf = BytesIO()
|
13 |
+
image.save(buf, "JPEG")
|
14 |
+
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
15 |
+
|
16 |
+
def caption(image):
|
17 |
+
data = {
|
18 |
+
"model": "openai-large",
|
19 |
+
"messages": [
|
20 |
+
{
|
21 |
+
"role": "user",
|
22 |
+
"content": [
|
23 |
+
{"type": "text", "text": PROMPT},
|
24 |
+
{
|
25 |
+
"type": "image_url",
|
26 |
+
"image_url": {"url": f"data:image/jpeg;base64,{image_to_base64(image)}"}
|
27 |
+
}
|
28 |
+
]
|
29 |
+
}
|
30 |
+
],
|
31 |
+
"private": "true"
|
32 |
+
}
|
33 |
+
response = requests.post(API_URL, json=data)
|
34 |
+
if response.ok:
|
35 |
+
return response.json()["choices"][0]["message"]["content"]
|
36 |
+
return ""
|
37 |
+
|
38 |
+
gr.Interface(
|
39 |
+
caption,
|
40 |
+
inputs=gr.Image(type="pil", label="Image"),
|
41 |
+
outputs=gr.Textbox(label="Caption")
|
42 |
+
).launch(debug=True)
|