Update app.py
Browse files
app.py
CHANGED
@@ -3,37 +3,33 @@ import os
|
|
3 |
from io import BytesIO
|
4 |
|
5 |
import gradio as gr
|
6 |
-
import
|
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 |
-
|
|
|
15 |
|
16 |
def caption(image):
|
17 |
-
|
18 |
-
|
19 |
-
"
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
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,
|
|
|
3 |
from io import BytesIO
|
4 |
|
5 |
import gradio as gr
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
|
|
|
8 |
PROMPT = os.environ.get("PROMPT", "Describe this image.")
|
9 |
|
10 |
+
client = InferenceClient(model="https://text.pollinations.ai/openai")
|
11 |
+
|
12 |
def image_to_base64(image):
|
13 |
buf = BytesIO()
|
14 |
image.save(buf, "JPEG")
|
15 |
+
buf.seek(0)
|
16 |
+
return f"data:image/jpeg;base64,{base64.b64encode(buf.getvalue()).decode()}"
|
17 |
|
18 |
def caption(image):
|
19 |
+
out = ""
|
20 |
+
for chunk in client.chat.completions.create(
|
21 |
+
model="openai-large",
|
22 |
+
messages=[{
|
23 |
+
"role": "user",
|
24 |
+
"content": [
|
25 |
+
{"type": "text", "text": PROMPT},
|
26 |
+
{"type": "image_url", "image_url": {"url": image_to_base64(image)}}
|
27 |
+
]
|
28 |
+
}],
|
29 |
+
stream=True
|
30 |
+
):
|
31 |
+
out += chunk.choices[0].delta.content
|
32 |
+
yield out
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
gr.Interface(
|
35 |
caption,
|