File size: 2,406 Bytes
d5a06f6
8ffbf51
 
6e2e0c5
8ffbf51
7c56b7b
8ffbf51
 
 
 
 
79ec99d
8ffbf51
 
 
 
 
a93e14b
 
788e1b9
a93e14b
 
 
8ffbf51
 
 
 
 
 
 
 
 
 
 
 
a93e14b
 
8ffbf51
 
 
 
 
7c56b7b
8ffbf51
 
 
6e2e0c5
8ffbf51
a93e14b
8ffbf51
 
 
 
 
 
 
 
 
6e2e0c5
8ffbf51
 
 
6e2e0c5
8ffbf51
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import streamlit as st
from openai import OpenAI
from PIL import Image
import io

# Set up the OpenAI client
client = OpenAI(
    base_url="https://api.studio.nebius.ai/v1/",
    api_key=os.environ.get("NEBIUS_API_KEY")
)

# Function to generate caption from image URL
def generate_caption(image_data):
    completion = client.chat.completions.create(
        model="Qwen/Qwen2-VL-72B-Instruct",
        messages=[
            {
                "role": "system",
                "content": """You are an image to prompt converter. Your work is to observe each and every detail of the image and craft a detailed prompt under 75 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering]."""
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": """Write a caption for this image"""
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": image_data
                        }
                    }
                ]
            }
        ],
        temperature=0
    )
    
    caption = completion.to_json().get("choices", [{}])[0].get("message", {}).get("content", "")
    return caption

# Streamlit UI
st.title("Image to Caption Generator")
st.write("Upload an image, and the app will generate a detailed caption for it.")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Display the uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)
    
    # Convert image to a base64 string
    buffered = io.BytesIO()
    image.save(buffered, format="PNG")
    img_base64 = buffered.getvalue().decode("utf-8")

    # Generate caption using the OpenAI API
    st.write("Generating caption...")
    caption = generate_caption(img_base64)

    # Display the generated caption
    if caption:
        st.subheader("Generated Caption:")
        st.write(caption)
    else:
        st.write("No caption could be generated.")