File size: 1,056 Bytes
66ab5ba
 
 
 
 
a7aae14
66ab5ba
 
 
a7aae14
 
66ab5ba
 
 
a7aae14
655ad30
66ab5ba
 
655ad30
 
a7aae14
adffcb2
 
 
 
655ad30
 
 
 
db9bbb8
adffcb2
 
 
655ad30
 
66ab5ba
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import base64
import os
from io import BytesIO

import gradio as gr
from huggingface_hub import InferenceClient

PROMPT = os.environ.get("PROMPT", "Describe this image.")

client = InferenceClient(model="https://text.pollinations.ai/openai")

def image_to_base64(image):
    buf = BytesIO()
    image.save(buf, "JPEG")
    buf.seek(0)
    return base64.b64encode(buf.getvalue()).decode("utf-8")

def caption(image):
    image = image_to_base64(image)
    return client.chat.completions.create(
        model="openai-large",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "image_url": {"url": f"data:image/jpeg;base64,{image}"}
                    },
                    {"type": "text", "text": PROMPT}
               ]
            }
        ],
        max_tokens=1024
    ).choices[0]

gr.Interface(
    caption,
    inputs=gr.Image(type="pil", label="Image"),
    outputs=gr.Textbox(label="Caption")
).launch(debug=True)