akhaliq HF Staff commited on
Commit
fc4a625
·
verified ·
1 Parent(s): dacebcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -117
app.py CHANGED
@@ -4,16 +4,64 @@ import torch
4
  from PIL import Image
5
  from transformers import AutoProcessor
6
  from longcat_image.models import LongCatImageTransformer2DModel
7
- from longcat_image.pipelines import LongCatImageEditPipeline, LongCatImagePipeline
8
  import numpy as np
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # --- Model Loading (Kept for completeness) ---
11
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
 
13
  # Text-to-Image Model
14
  t2i_model_id = 'meituan-longcat/LongCat-Image'
15
  print(f"🔄 Loading Text-to-Image model from {t2i_model_id}...")
16
-
17
  t2i_text_processor = AutoProcessor.from_pretrained(
18
  t2i_model_id,
19
  subfolder='tokenizer'
@@ -38,7 +86,7 @@ print(f"✅ Text-to-Image model loaded successfully")
38
  # Image Edit Model
39
  edit_model_id = 'meituan-longcat/LongCat-Image-Edit'
40
  print(f"🔄 Loading Image Edit model from {edit_model_id}...")
41
-
42
  edit_text_processor = AutoProcessor.from_pretrained(
43
  edit_model_id,
44
  subfolder='tokenizer'
@@ -139,113 +187,16 @@ t2i_example_prompts = [
139
  ["A cute robot sitting at a desk, digital art style", 1024, 1024, 44],
140
  ]
141
 
142
- # --- Custom CSS (Cleaned up for maximum layout flexibility) ---
143
- custom_css = """
144
- @import url('https://fonts.googleapis.com/css2?family=SF+Pro+Display:wght@300;400;500;600;700&display=swap');
145
-
146
- * {
147
- font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
148
- }
149
-
150
- /* Base styling relying on fill_width=True and Gradio's structure */
151
- .gradio-container {
152
- max-width: 100% !important; /* Allow full width */
153
- margin: auto !important;
154
- }
155
-
156
- /* Background gradient for the overall app */
157
- #component-0 {
158
- background: linear-gradient(180deg, #f5f5f7 0%, #ffffff 100%) !important;
159
- }
160
-
161
- /* Tab bar styling for the segmented control look */
162
- .tabs {
163
- border: none !important;
164
- background: transparent !important;
165
- }
166
-
167
- .tab-nav {
168
- border: none !important;
169
- background: rgba(255, 255, 255, 0.8) !important;
170
- backdrop-filter: blur(20px) !important;
171
- border-radius: 12px !important;
172
- padding: 4px !important;
173
- gap: 4px !important;
174
- }
175
-
176
- button.selected {
177
- background: white !important;
178
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08) !important;
179
- border-radius: 8px !important;
180
- font-weight: 500 !important;
181
- }
182
-
183
- /* Image and input component styling */
184
- .input-image, .output-image {
185
- border-radius: 16px !important;
186
- overflow: hidden !important;
187
- box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06) !important;
188
- }
189
-
190
- textarea, input[type="text"] {
191
- border: 1px solid #d2d2d7 !important;
192
- border-radius: 12px !important;
193
- padding: 12px 16px !important;
194
- font-size: 15px !important;
195
- transition: all 0.2s ease !important;
196
- }
197
-
198
- textarea:focus, input[type="text"]:focus {
199
- border-color: #007aff !important;
200
- box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1) !important;
201
- }
202
-
203
- /* Primary Button Styling */
204
- .primary-btn {
205
- background: linear-gradient(180deg, #007aff 0%, #0051d5 100%) !important;
206
- border: none !important;
207
- border-radius: 12px !important;
208
- padding: 14px 28px !important;
209
- font-size: 16px !important;
210
- font-weight: 500 !important;
211
- color: white !important;
212
- box-shadow: 0 4px 12px rgba(0, 122, 255, 0.3) !important;
213
- transition: all 0.2s ease !important;
214
- }
215
-
216
- .primary-btn:hover {
217
- transform: translateY(-2px) !important;
218
- box-shadow: 0 6px 16px rgba(0, 122, 255, 0.4) !important;
219
- }
220
-
221
- /* Card Style (targets gr-panel when variant="panel" is used) */
222
- .card {
223
- background: white !important;
224
- border-radius: 16px !important;
225
- padding: 24px !important;
226
- box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04) !important;
227
- }
228
-
229
- /* Mobile adjustments (retains padding) */
230
- @media (max-width: 768px) {
231
- .gradio-container {
232
- padding: 0 8px !important;
233
- }
234
- .card {
235
- padding: 16px !important;
236
- }
237
- }
238
- """
239
-
240
  # Build Gradio interface
241
- # Using fill_width=True on Blocks to maximize available horizontal space
242
- with gr.Blocks(fill_width=True) as demo:
 
243
  gr.HTML("""
244
  <div style="text-align: center; padding: 40px 20px 30px 20px;">
245
- <h1 style="font-size: 48px; font-weight: 700; margin: 0; background: linear-gradient(90deg, #007aff 0%, #5856d6 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
246
  LongCat Studio
247
  </h1>
248
- <p style="font-size: 20px; color: #86868b; margin-top: 12px; font-weight: 400;">
249
  AI-powered image generation and editing
250
  </p>
251
  </div>
@@ -256,14 +207,13 @@ with gr.Blocks(fill_width=True) as demo:
256
  with gr.TabItem("Edit Image", id=0):
257
  with gr.Row():
258
  # Left Column (Inputs)
259
- with gr.Column(scale=1, min_width=0, variant="panel"):
260
  gr.Markdown("### 🖼️ Input Image & Controls")
261
  input_image = gr.Image(
262
  label="Upload Image",
263
  type="pil",
264
  sources=["upload", "clipboard"],
265
  height=450,
266
- elem_classes=["input-image"]
267
  )
268
 
269
  prompt = gr.Textbox(
@@ -282,7 +232,7 @@ with gr.Blocks(fill_width=True) as demo:
282
  visible=False
283
  )
284
 
285
- edit_btn = gr.Button("Edit Image", variant="primary", size="lg", elem_classes=["primary-btn"])
286
 
287
  # Right Column (Output)
288
  with gr.Column(scale=1, min_width=0, variant="panel"):
@@ -291,7 +241,6 @@ with gr.Blocks(fill_width=True) as demo:
291
  label="Result",
292
  type="pil",
293
  height=450,
294
- elem_classes=["output-image"]
295
  )
296
 
297
  gr.HTML("<div style='margin: 30px 0 20px 0;'></div>")
@@ -344,7 +293,7 @@ with gr.Blocks(fill_width=True) as demo:
344
  visible=False
345
  )
346
 
347
- generate_btn = gr.Button("Generate Image", variant="primary", size="lg", elem_classes=["primary-btn"])
348
 
349
  # Right Column (Output)
350
  with gr.Column(scale=1, min_width=0, variant="panel"):
@@ -353,7 +302,6 @@ with gr.Blocks(fill_width=True) as demo:
353
  label="Result",
354
  type="pil",
355
  height=550,
356
- elem_classes=["output-image"]
357
  )
358
 
359
  gr.HTML("<div style='margin: 30px 0 20px 0;'></div>")
@@ -383,18 +331,18 @@ with gr.Blocks(fill_width=True) as demo:
383
 
384
  # Footer
385
  gr.HTML("""
386
- <div style="text-align: center; margin-top: 60px; padding: 30px 20px; border-top: 1px solid #d2d2d7;">
387
- <p style="color: #86868b; font-size: 13px; margin: 0;">
388
  Powered by LongCat • Built with
389
- <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007aff; text-decoration: none;">anycoder</a>
390
  </p>
391
  </div>
392
  """)
393
 
394
- # Launch the app with theme and custom CSS
395
  if __name__ == "__main__":
396
  demo.launch(
397
  mcp_server=True,
398
- theme=gr.themes.Soft(),
399
- css=custom_css
400
  )
 
4
  from PIL import Image
5
  from transformers import AutoProcessor
6
  from longcat_image.models import LongCatImageTransformer2DModel
7
+ from longcat_image.pipelines import LongCatImageEditPipeline, LongCatImageEditPipeline
8
  import numpy as np
9
 
10
+ # 1. Define the Custom Theme Class
11
+ # Inherit from Base for simplicity and modify core parameters.
12
+ class AppleSoft(gr.themes.Base):
13
+ def __init__(
14
+ self,
15
+ # Use a deep Indigo/Blue for primary actions (like the iOS/macOS blue)
16
+ primary_hue=gr.themes.colors.indigo,
17
+ secondary_hue=gr.themes.colors.gray,
18
+ neutral_hue=gr.themes.colors.neutral,
19
+ # Increase radius for "card" look (iOS uses large rounded corners)
20
+ radius_size=gr.themes.sizes.radius_lg,
21
+ # Set font to use a system font stack (like the old custom CSS)
22
+ font=[gr.themes.GoogleFont("Source Sans Pro"), "system-ui", "sans-serif"],
23
+ font_mono=gr.themes.GoogleFont("IBM Plex Mono"),
24
+ spacing_size=gr.themes.sizes.spacing_lg,
25
+ text_size=gr.themes.sizes.text_md,
26
+ **kwargs
27
+ ):
28
+ super().__init__(
29
+ primary_hue=primary_hue,
30
+ secondary_hue=secondary_hue,
31
+ neutral_hue=neutral_hue,
32
+ radius_size=radius_size,
33
+ font=font,
34
+ font_mono=font_mono,
35
+ spacing_size=spacing_size,
36
+ text_size=text_size,
37
+ **kwargs
38
+ )
39
+ # 2. Use .set() to override specific CSS variables for the Apple aesthetic
40
+ self.set(
41
+ # Remove main block shadow for a flatter, modern look
42
+ block_shadow="none",
43
+ block_label_padding="*spacing_sm *spacing_lg",
44
+ # Apply light shadow and clean background to input/panel elements (the "card" effect)
45
+ block_background_fill="white",
46
+ block_border_width="1px",
47
+ block_border_color="*neutral_200",
48
+ # Ensure primary button looks clean and elevated
49
+ button_primary_background_fill="*primary_500",
50
+ button_primary_background_fill_hover="*primary_400",
51
+ button_primary_border_color="*primary_500",
52
+ button_primary_shadow="0 2px 8px *primary_300", # Subtle shadow for depth
53
+ )
54
+
55
+ # Instantiate the custom theme
56
+ apple_theme = AppleSoft()
57
+
58
  # --- Model Loading (Kept for completeness) ---
59
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
60
 
61
  # Text-to-Image Model
62
  t2i_model_id = 'meituan-longcat/LongCat-Image'
63
  print(f"🔄 Loading Text-to-Image model from {t2i_model_id}...")
64
+ # ... (rest of model loading remains the same) ...
65
  t2i_text_processor = AutoProcessor.from_pretrained(
66
  t2i_model_id,
67
  subfolder='tokenizer'
 
86
  # Image Edit Model
87
  edit_model_id = 'meituan-longcat/LongCat-Image-Edit'
88
  print(f"🔄 Loading Image Edit model from {edit_model_id}...")
89
+ # ... (rest of model loading remains the same) ...
90
  edit_text_processor = AutoProcessor.from_pretrained(
91
  edit_model_id,
92
  subfolder='tokenizer'
 
187
  ["A cute robot sitting at a desk, digital art style", 1024, 1024, 44],
188
  ]
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  # Build Gradio interface
191
+ # Passed theme=apple_theme here and fill_width=True for responsiveness
192
+ with gr.Blocks(theme=apple_theme, fill_width=True) as demo:
193
+ # Retaining the HTML header for branding/title
194
  gr.HTML("""
195
  <div style="text-align: center; padding: 40px 20px 30px 20px;">
196
+ <h1 style="font-size: 48px; font-weight: 700; margin: 0; color: var(--primary-500);">
197
  LongCat Studio
198
  </h1>
199
+ <p style="font-size: 20px; color: var(--body-text-color-subdued); margin-top: 12px; font-weight: 400;">
200
  AI-powered image generation and editing
201
  </p>
202
  </div>
 
207
  with gr.TabItem("Edit Image", id=0):
208
  with gr.Row():
209
  # Left Column (Inputs)
210
+ with gr.Column(scale=1, min_width=0, variant="panel"): # variant="panel" applies the card styling from theme
211
  gr.Markdown("### 🖼️ Input Image & Controls")
212
  input_image = gr.Image(
213
  label="Upload Image",
214
  type="pil",
215
  sources=["upload", "clipboard"],
216
  height=450,
 
217
  )
218
 
219
  prompt = gr.Textbox(
 
232
  visible=False
233
  )
234
 
235
+ edit_btn = gr.Button("Edit Image", variant="primary", size="lg")
236
 
237
  # Right Column (Output)
238
  with gr.Column(scale=1, min_width=0, variant="panel"):
 
241
  label="Result",
242
  type="pil",
243
  height=450,
 
244
  )
245
 
246
  gr.HTML("<div style='margin: 30px 0 20px 0;'></div>")
 
293
  visible=False
294
  )
295
 
296
+ generate_btn = gr.Button("Generate Image", variant="primary", size="lg")
297
 
298
  # Right Column (Output)
299
  with gr.Column(scale=1, min_width=0, variant="panel"):
 
302
  label="Result",
303
  type="pil",
304
  height=550,
 
305
  )
306
 
307
  gr.HTML("<div style='margin: 30px 0 20px 0;'></div>")
 
331
 
332
  # Footer
333
  gr.HTML("""
334
+ <div style="text-align: center; margin-top: 60px; padding: 30px 20px; border-top: 1px solid var(--border-color-subdued);">
335
+ <p style="color: var(--body-text-color-subdued); font-size: var(--text-sm); margin: 0;">
336
  Powered by LongCat • Built with
337
+ <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: var(--primary-500); text-decoration: none;">anycoder</a>
338
  </p>
339
  </div>
340
  """)
341
 
342
+ # Launch the app with the custom, programmatic theme
343
  if __name__ == "__main__":
344
  demo.launch(
345
  mcp_server=True,
346
+ theme=apple_theme,
347
+ # Custom CSS is removed to ensure theme integrity
348
  )