File size: 1,205 Bytes
66ab5ba
 
 
 
 
a7aae14
66ab5ba
 
 
a7aae14
 
66ab5ba
 
 
a7aae14
655ad30
66ab5ba
63d7e75
655ad30
c7ad52e
a7aae14
adffcb2
 
 
 
655ad30
f81830f
655ad30
 
63d7e75
adffcb2
 
 
c7ad52e
 
66ab5ba
 
 
63d7e75
 
0206a66
63d7e75
1ccbcc4
63d7e75
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
42
43
44
45
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, prompt):
    image = image_to_base64(image)
    return client.chat.completions.create(
        model="openai-large",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {"url": f"data:image/jpeg;base64,{image}"}
                    },
                    {"type": "text", "text": prompt}
               ]
            }
        ],
        max_tokens=1024
    ).choices[0].message.content

gr.Interface(
    caption,
    inputs=[
        gr.Image(type="pil", label="Image"), 
        gr.TextArea(label="Prompt", value=PROMPT)
    ],
    outputs=gr.Textbox(label="Caption", show_copy_button=True),
    title="Image Captioning"
).launch(debug=True)