File size: 9,456 Bytes
a3f0f2d
 
 
 
 
 
 
 
dfda583
 
18362de
fb1a9a7
dfda583
 
 
fb1a9a7
dfda583
fb1a9a7
 
18362de
dfda583
 
18362de
dfda583
 
fb1a9a7
dfda583
 
18362de
dfda583
fb1a9a7
dfda583
18362de
 
 
 
 
 
 
 
 
 
dfda583
fb1a9a7
 
 
 
 
18362de
dfda583
 
fb1a9a7
 
 
 
 
dfda583
fb1a9a7
 
 
 
 
 
 
 
18362de
 
fb1a9a7
 
18362de
fb1a9a7
 
 
18362de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfda583
 
18362de
 
dfda583
 
 
c5900d7
18362de
 
dfda583
a3f0f2d
c5900d7
a3f0f2d
fb1a9a7
c5900d7
3897042
c5900d7
 
dfda583
 
c5900d7
dfda583
18362de
dfda583
c5900d7
 
 
 
 
 
 
 
 
 
a3f0f2d
18362de
c5900d7
 
 
 
18362de
c5900d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18362de
dfda583
c5900d7
dfda583
c5900d7
dfda583
 
 
 
 
 
c5900d7
dfda583
 
18362de
c5900d7
 
 
 
 
18362de
 
 
 
 
 
 
c5900d7
 
 
18362de
 
 
a3f0f2d
c5900d7
1705ce6
a3f0f2d
fb1a9a7
d85418d
fd32459
c5900d7
54f4fee
a3f0f2d
c5900d7
d85418d
a3f0f2d
c5900d7
f90960c
a3f0f2d
 
fb1a9a7
c5900d7
f90960c
96cd1bf
fb1a9a7
96cd1bf
fb1a9a7
 
 
a3f0f2d
 
 
 
 
 
 
 
dfda583
a3f0f2d
c5900d7
dfda583
c5900d7
dfda583
 
d85418d
c5900d7
fb1a9a7
96cd1bf
 
fb1a9a7
c5900d7
a3f0f2d
 
1705ce6
fb1a9a7
b8e12f6
3e282cd
a3f0f2d
fb1a9a7
d85418d
f90960c
d85418d
dfda583
 
 
 
 
 
 
f90960c
a3f0f2d
 
 
 
 
18362de
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import gradio as gr
import cv2
import numpy as np
from registry import registry
from filters import *
from components import create_filter_controls

def create_app():
    with gr.Blocks(theme=gr.themes.Soft(), css="""
        .gradio-container {
            max-width: 1600px !important;
            margin: auto !important;
        }
        .filter-header {
            text-align: center;
            padding: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 15px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        .filter-header h1 {
            color: white !important;
            margin: 0;
            font-size: 2.5em;
            font-weight: bold;
        }
        .filter-header p {
            color: rgba(255,255,255,0.95) !important;
            margin: 10px 0 0 0;
            font-size: 1.1em;
        }
        .filter-header a {
            color: rgba(255,255,255,0.9) !important;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        .filter-header a:hover {
            color: white !important;
            text-decoration: underline;
        }
        .image-container {
            border-radius: 12px;
            padding: 15px;
            transition: all 0.3s ease;
        }
        .image-container:hover {
            transform: translateY(-2px);
        }
        .control-panel {
            border-radius: 12px;
            padding: 20px;
            margin-top: 15px;
        }
        .filter-description {
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            border-left: 4px solid #667eea;
        }
        .gr-button-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
            border: none !important;
            font-weight: bold !important;
            color: white !important;
            transition: all 0.3s ease !important;
        }
        .gr-button-primary:hover {
            transform: translateY(-2px) !important;
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4) !important;
        }
        .gr-button-secondary {
            transition: all 0.3s ease !important;
        }
        .gr-button-secondary:hover {
            transform: translateY(-2px) !important;
        }
        .stats-panel {
            border-radius: 8px;
            padding: 15px;
            margin-top: 10px;
            text-align: center;
        }
        /* Dark mode compatibility */
        .dark .filter-description {
            background: rgba(255,255,255,0.05);
        }
        .dark .image-container {
            background: rgba(255,255,255,0.02);
        }
        .dark .control-panel {
            background: rgba(255,255,255,0.03);
        }
        /* Light mode */
        .filter-description {
            background: #f0f4f8;
        }
        .image-container {
            background: rgba(0,0,0,0.02);
        }
        .control-panel {
            background: linear-gradient(to bottom, #f8f9fa, #ffffff);
        }
    """) as app:
        
        # Header
        with gr.Column(elem_classes="filter-header"):
            gr.Markdown("""
            # 📷 Photo Filter App
            Professional photo editing with powerful filters - Fast & Easy
            
            Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
            """)
        
        # Initialize components
        filter_names = list(registry.filters.keys())
        
        # Top Row - Images side by side
        with gr.Row():
            # Left Column - Input Image
            with gr.Column(scale=1):
                with gr.Group(elem_classes="image-container"):
                    input_image = gr.Image(
                        label="📤 Original Image",
                        type="numpy",
                        height=500
                    )
            
            # Right Column - Output Image
            with gr.Column(scale=1):
                with gr.Group(elem_classes="image-container"):
                    output_image = gr.Image(
                        label="✅ Edited Image",
                        height=500
                    )
                
                error_message = gr.Markdown(visible=False)
                
                with gr.Row():
                    download_button = gr.Button(
                        "💾 Download", 
                        visible=False,
                        size="lg"
                    )

        # Action Buttons
        with gr.Row():
            apply_button = gr.Button(
                "✨ Apply Filter",
                variant="primary",
                size="lg",
                scale=2
            )
            reset_button = gr.Button(
                "🔄 Reset", 
                variant="secondary",
                size="lg",
                scale=1
            )

        # Bottom Row - Filter Selection & Parameters
        with gr.Row():
            with gr.Column(scale=1):
                with gr.Group(elem_classes="control-panel"):
                    gr.Markdown("### 🎨 Select Filter")
                    filter_select = gr.Dropdown(
                        label="Filter",
                        choices=filter_names,
                        value="Original",
                        interactive=True
                    )
                    
                    filter_doc = gr.Markdown(
                        value="Select a filter to see detailed description.",
                        elem_classes="filter-description"
                    )
            
            with gr.Column(scale=1):
                with gr.Group(elem_classes="control-panel"):
                    gr.Markdown("### ⚙️ Customize")
                    # Create dynamic filter controls
                    filter_controls, control_components = create_filter_controls()

        # Stats panel
        with gr.Row():
            with gr.Column():
                gr.Markdown(
                    f"""
                    <div class="stats-panel">
                    📊 <b>Total Filters:</b> {len(filter_names)} | 
                    🎯 <b>Parameterized Filters:</b> {sum(1 for f in filter_names if registry.params_map.get(f))} | 
                    🚀 <b>Quick Filters:</b> {sum(1 for f in filter_names if not registry.params_map.get(f))}
                    </div>
                    """
                )

        # Handle UI updates
        def update_controls(filter_name):
            updates = []
            for group_name in filter_controls:
                updates.append(gr.update(visible=group_name == filter_name))
            
            doc = registry.filters[filter_name].__doc__ or "No detailed description available."
            return updates + [doc]

        # Handle image processing
        def process(image, filter_name, *args):
            if image is None:
                return None, gr.update(visible=True, value="⚠️ **Note:** Please upload an image before applying filters!"), gr.update(visible=False)
            
            try:
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
                
                # Get parameters for current filter
                params = {}
                if filter_name in control_components:
                    param_names = list(registry.params_map.get(filter_name, {}).keys())
                    controls_list = control_components[filter_name]
                    for i, param_name in enumerate(param_names):
                        if i < len(controls_list):
                            params[param_name] = args[i]
                
                processed = registry.filters[filter_name](image, **params)
                
                if len(processed.shape) == 2:
                    processed = cv2.cvtColor(processed, cv2.COLOR_GRAY2RGB)
                else:
                    processed = cv2.cvtColor(processed, cv2.COLOR_BGR2RGB)
                
                return processed, gr.update(visible=False), gr.update(visible=True)
            except Exception as e:
                return None, gr.update(visible=True, value=f"❌ **Error:** {str(e)}"), gr.update(visible=False)
        
        # Handle reset
        def reset_all():
            return None, None, gr.update(visible=False), gr.update(visible=False), "Original"

        # Collect all control components
        all_control_components = []
        for filter_name in control_components:
            all_control_components.extend(control_components[filter_name])
        
        # Connect events
        filter_select.change(
            update_controls,
            inputs=filter_select,
            outputs=list(filter_controls.values()) + [filter_doc],
            api_name=False
        )
        
        input_components = [input_image, filter_select] + all_control_components
        apply_button.click(
            process,
            inputs=input_components,
            outputs=[output_image, error_message, download_button],
        )
        
        reset_button.click(
            reset_all,
            inputs=None,
            outputs=[input_image, output_image, error_message, download_button, filter_select]
        )

    return app

if __name__ == "__main__":
    app = create_app()
    app.launch()