File size: 4,639 Bytes
19dc712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Make a virtual environment with compulsory GPU!!
# python -m venv .venv
# Activate the virtual environment
# Windows: .venv\Scripts\activate
# Linux/Mac: source .venv/bin/activate

# Install packages
# pip install -r requirements.txt

import streamlit as st
from generate_caption import generate_caption
from PIL import Image
import io

# Set page config
st.set_page_config(
    page_title="AI Image Caption & Prompt Generator",
    page_icon="🖼️",
    layout="wide"
)

# Title and description
st.title("🖼️ AI Image Caption & Prompt Generator")

# Create two columns for layout
col1, col2 = st.columns([1, 1])

with col1:
    st.header("📤 Upload & Configure")

    # File uploader
    uploaded_file = st.file_uploader(
        "Choose an image file",
        type=['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp'],
        help="Supported formats: PNG, JPG, JPEG, GIF, BMP, TIFF, WebP"
    )

    # Caption options
    st.subheader("Caption Options")

    caption_type = st.selectbox(
        "Caption Type",
        options=["MidJourney", "Descriptive", "Training Prompt"],
        index=0,
        help="Choose the style of caption you want to generate"
    )

    caption_length = st.selectbox(
        "Caption Length",
        options=["short", "any", "long"],
        index=0,
        help="Select the desired length of the caption"
    )

    # Generate button
    generate_btn = st.button("🎯 Generate Caption", type="primary", use_container_width=True)

with col2:
    st.header("Preview & Results")

    if uploaded_file is not None:
        # Display uploaded image
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", use_container_width=True)

        # Generate caption when button is clicked
        if generate_btn:
            with st.spinner("Generating caption... This may take a moment."):
                try:
                    # Generate caption
                    prompt_used, caption = generate_caption(
                        image,
                        caption_type=caption_type,
                        caption_length=caption_length,
                        extra_options=None
                    )

                    # Display results
                    st.success("Caption generated successfully!")

                    # Caption result
                    st.subheader("📝 Generated Caption")
                    st.write(f"{caption}")

                    # Copy to clipboard button
                    st.code(caption, language=None)

                    # Additional info
                    with st.expander("ℹ️ Generation Details"):
                        st.write(f"**Caption Type:** {caption_type}")
                        st.write(f"**Caption Length:** {caption_length}")
                        if prompt_used:
                            st.write(f"**Prompt Used:** {prompt_used}")

                except Exception as e:
                    st.error(f"Error generating caption: {str(e)}")
                    st.info("Please make sure you have installed all required dependencies and your GPU is properly configured.")

    else:
        st.markdown("""
        ### How to use:
        1. Upload an image using the file uploader
        2. Select your preferred caption type and length
        3. Click 'Generate Caption' to create your AI caption

        ### Caption Types:
        - **MidJourney**: Optimized for AI art generation prompts
        - **Descriptive**: Detailed description of the image content
        - **Training Prompt**: Formatted for AI model training
        """)

# Sidebar with additional information
with st.sidebar:
    st.header("🔧 System Requirements")
    st.markdown("""
    **Required Setup:**
    - GPU-enabled environment
    - Virtual environment activated
    - All dependencies installed via `requirements.txt`

    **Supported Image Formats:**
    - PNG, JPG, JPEG
    - GIF, BMP, TIFF, WebP
    """)

    st.header("💡 Tips")
    st.markdown("""
    - Higher quality images produce better captions
    - Different caption types serve different purposes
    - Short captions are more focused, long ones more detailed
    """)

    st.header("⚙️ Setup Instructions")
    with st.expander("Click to view setup commands"):
        st.code("""
# Create virtual environment
python -m venv .venv

# Activate virtual environment
# Windows:
.venv\\Scripts\\activate
# Linux/Mac:
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Run Streamlit app
streamlit run app.py
        """, language="bash")

# Footer
st.markdown("Built by [Aditya Singh]")