File size: 5,627 Bytes
2a6a910
3b85b9a
a38ae12
2a6a910
 
 
826e03f
2a6a910
4d3ff27
2a6a910
5c85beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a38ae12
4d55ef2
5c85beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09f41cc
5c85beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b85b9a
40ccb44
5c85beb
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python

import os
import string
import gradio as gr
import PIL.Image
import spaces
import torch
from transformers import AutoProcessor, BitsAndBytesConfig, Blip2ForConditionalGeneration

# ์Šคํƒ€์ผ ์ƒ์ˆ˜ ์ •์˜
CUSTOM_CSS = """
.container {
    max-width: 1000px;
    margin: auto;
    padding: 2rem;
    background: linear-gradient(to bottom right, #ffffff, #f8f9fa);
    border-radius: 15px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.title {
    font-size: 2.5rem;
    color: #1a73e8;
    text-align: center;
    margin-bottom: 2rem;
    font-weight: bold;
}

.tab-nav {
    background: #f8f9fa;
    border-radius: 10px;
    padding: 0.5rem;
    margin-bottom: 1rem;
}

.input-box {
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.input-box:focus {
    border-color: #1a73e8;
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
}

.button-primary {
    background: #1a73e8;
    color: white;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: all 0.3s ease;
}

.button-primary:hover {
    background: #1557b0;
    transform: translateY(-1px);
}

.output-box {
    background: #ffffff;
    border-radius: 8px;
    padding: 1rem;
    margin-top: 1rem;
    border: 1px solid #e0e0e0;
}

.chatbot-message {
    padding: 1rem;
    margin: 0.5rem 0;
    border-radius: 8px;
    background: #f8f9fa;
}

.advanced-settings {
    background: #ffffff;
    border-radius: 8px;
    padding: 1rem;
    margin-top: 1rem;
}

.slider-container {
    padding: 0.5rem;
    background: #f8f9fa;
    border-radius: 6px;
}
"""

DESCRIPTION = """
<div class="title">
    ๐Ÿ–ผ๏ธ BLIP-2 Visual Intelligence System
</div>
<p style='text-align: center; color: #666;'>
    Advanced AI system for image understanding and natural conversation
</p>
"""

if not torch.cuda.is_available():
    DESCRIPTION += "\n<p style='color: #dc3545;'>Running on CPU ๐Ÿฅถ This demo requires GPU to function properly.</p>"

# ๋ชจ๋ธ ์„ค์ • ๋ถ€๋ถ„์€ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€...

def create_interface():
    with gr.Blocks(css=CUSTOM_CSS) as demo:
        gr.Markdown(DESCRIPTION)
        
        with gr.Group(elem_classes="container"):
            with gr.Row():
                with gr.Column(scale=1):
                    image = gr.Image(
                        type="pil",
                        label="Upload Image",
                        elem_classes="input-box"
                    )
                
                with gr.Column(scale=2):
                    with gr.Tabs(elem_classes="tab-nav"):
                        with gr.Tab(label="โœจ Image Captioning"):
                            caption_button = gr.Button(
                                "Generate Caption",
                                elem_classes="button-primary"
                            )
                            caption_output = gr.Textbox(
                                label="Generated Caption",
                                elem_classes="output-box"
                            )
                            
                        with gr.Tab(label="๐Ÿ’ญ Visual Q&A"):
                            chatbot = gr.Chatbot(
                                elem_classes="chatbot-message"
                            )
                            vqa_input = gr.Textbox(
                                placeholder="Ask me anything about the image...",
                                elem_classes="input-box"
                            )
                            
                            with gr.Row():
                                clear_button = gr.Button(
                                    "Clear Chat",
                                    elem_classes="button-secondary"
                                )
                                submit_button = gr.Button(
                                    "Send Message",
                                    elem_classes="button-primary"
                                )

            with gr.Accordion("๐Ÿ› ๏ธ Advanced Settings", open=False, elem_classes="advanced-settings"):
                # ๊ณ ๊ธ‰ ์„ค์ • ์ปจํŠธ๋กค๋“ค...
                with gr.Row():
                    with gr.Column():
                        text_decoding_method = gr.Radio(
                            choices=["Beam search", "Nucleus sampling"],
                            value="Nucleus sampling",
                            label="Decoding Method"
                        )
                        temperature = gr.Slider(
                            minimum=0.5,
                            maximum=1.0,
                            value=1.0,
                            label="Temperature",
                            elem_classes="slider-container"
                        )
                    with gr.Column():
                        length_penalty = gr.Slider(
                            minimum=-1.0,
                            maximum=2.0,
                            value=1.0,
                            label="Length Penalty",
                            elem_classes="slider-container"
                        )
                        repetition_penalty = gr.Slider(
                            minimum=1.0,
                            maximum=5.0,
                            value=1.5,
                            label="Repetition Penalty",
                            elem_classes="slider-container"
                        )

        # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์—ฐ๊ฒฐ...
        
    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.queue(max_size=10).launch()