dzmu commited on
Commit
735c1df
·
verified ·
1 Parent(s): 592929b

Delete Testapp.txt

Browse files
Files changed (1) hide show
  1. Testapp.txt +0 -385
Testapp.txt DELETED
@@ -1,385 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- import clip
4
- import numpy as np
5
- import random
6
- import os
7
- from PIL import Image
8
- from ultralytics import YOLO # Still needed for person detection
9
- from gtts import gTTS
10
- import uuid
11
- import time
12
- import tempfile
13
-
14
- # --- Configuration ---
15
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
16
- YOLO_PERSON_MODEL_PATH = 'yolov8n.pt' # Standard YOLOv8 for person detection
17
- # YOLO_FASHION_MODEL_PATH = 'best.pt' # REMOVED - Not using fashion model anymore
18
- CLIP_MODEL_NAME = "ViT-B/32"
19
-
20
- # --- Load Models ---
21
- print(f"Using device: {DEVICE}")
22
- try:
23
- clip_model, clip_preprocess = clip.load(CLIP_MODEL_NAME, device=DEVICE)
24
- print(f"CLIP model ({CLIP_MODEL_NAME}) loaded successfully.")
25
- except Exception as e:
26
- print(f"Error loading CLIP model: {e}")
27
- # Handle error
28
- try:
29
- yolo_person_model = YOLO(YOLO_PERSON_MODEL_PATH).to(DEVICE)
30
- print(f"YOLO person detection model ({YOLO_PERSON_MODEL_PATH}) loaded successfully.")
31
- except Exception as e:
32
- print(f"Error loading YOLO person model: {e}")
33
- # Handle error
34
-
35
- # REMOVED Fashion Model Loading
36
-
37
- # --- Prompts and Responses ---
38
- style_prompts = {
39
- 'drippy': [
40
- "avant-garde streetwear", "high-fashion designer outfit", "trendsetting urban attire",
41
- "luxury sneakers and chic accessories", "cutting-edge, bold style"
42
- ],
43
- 'mid': [
44
- "casual everyday outfit", "modern minimalistic attire", "comfortable yet stylish look",
45
- "simple, relaxed streetwear", "balanced, practical fashion"
46
- ],
47
- 'not_drippy': [
48
- "disheveled outfit", "poorly coordinated fashion", "unfashionable, outdated attire",
49
- "tacky, mismatched ensemble", "sloppy, uninspired look"
50
- ]
51
- }
52
-
53
- # --- REINSTATED: Clothing prompts for CLIP ---
54
- clothing_prompts = [
55
- "t-shirt", "dress shirt", "blouse", "hoodie", "jacket", "sweater", "coat",
56
- "dress", "skirt", "pants", "jeans", "trousers", "shorts",
57
- "sneakers", "boots", "heels", "sandals",
58
- "cap", "hat", "scarf", "gloves", "bag", "accessory", "tank-top", "haircut"
59
- ]
60
-
61
- # --- REINSTATED: Combine all prompts for CLIP ---
62
- all_prompts = []
63
- for cat_prompts in style_prompts.values():
64
- all_prompts.extend(cat_prompts)
65
-
66
- # Record end of style prompts before adding clothing prompts
67
- style_prompts_end_index = len(all_prompts)
68
- all_prompts.extend(clothing_prompts)
69
- print(f"Total prompts for CLIP: {len(all_prompts)}")
70
-
71
- response_templates = {
72
- 'drippy': [
73
- "You're Drippy, bruh – fire {item}!", "{item} goes crazy, on god!", "Certified drippy with that {item}."
74
- ],
75
- 'mid': [
76
- "Drop the {item} and you might get a text back.", "It's alright, but I'd upgrade the {item}.",
77
- "Mid fit alert. That {item} is holding you back."
78
- ],
79
- 'not_drippy': [
80
- "Bro thought that {item} was tuff!", "Oh hell nah! Burn that {item}!",
81
- "Crimes against fashion, especially that {item}! Also… maybe get a haircut.",
82
- "Never walk out the house again with that {item}."
83
- ]
84
- }
85
- CATEGORY_LABEL_MAP = { "drippy": "drippy", "mid": "mid", "not_drippy": "trash" }
86
-
87
- # --- REINSTATED: Function to get top clothing items based on CLIP probabilities ---
88
- def get_top_clothing(probs, n=3):
89
- """Gets the top N clothing items based on CLIP probabilities."""
90
- clothing_probs_start_index = style_prompts_end_index
91
- clothing_probs = probs[clothing_probs_start_index:]
92
- actual_n = min(n, len(clothing_prompts))
93
- if actual_n <= 0:
94
- return ["item"]
95
- top_indices_in_slice = np.argsort(clothing_probs)[-actual_n:]
96
- return [clothing_prompts[i] for i in reversed(top_indices_in_slice)]
97
-
98
- # --- Core Logic ---
99
- def analyze_outfit(input_img: Image.Image):
100
- if input_img is None:
101
- return ("<p style='color: #FF5555; text-align: center;'>Please upload an image.</p>",
102
- None, "Error: No image provided.")
103
-
104
- img = input_img.copy()
105
- # 1) YOLO Person Detection
106
- person_results = yolo_person_model(img, verbose=False)
107
- boxes = person_results[0].boxes.xyxy.cpu().numpy()
108
- classes = person_results[0].boxes.cls.cpu().numpy()
109
- confidences = person_results[0].boxes.conf.cpu().numpy()
110
- person_indices = np.where(classes == 0)[0]
111
- cropped_img = img
112
- if len(person_indices) > 0:
113
- max_conf_person_idx = person_indices[np.argmax(confidences[person_indices])]
114
- x1, y1, x2, y2 = map(int, boxes[max_conf_person_idx])
115
- x1, y1 = max(0, x1), max(0, y1)
116
- x2, y2 = min(img.width, x2), min(img.height, y2)
117
- if x1 < x2 and y1 < y2:
118
- cropped_img = img.crop((x1, y1, x2, y2))
119
- print(f"Person detected and cropped: Box {x1, y1, x2, y2}")
120
- else:
121
- print("Warning: Invalid person bounding box after clipping. Using full image.")
122
- cropped_img = img
123
- else:
124
- print("No person detected by yolo_person_model. Analyzing full image.")
125
-
126
- # 2) CLIP Analysis
127
- detected_clothing_item = "look"
128
- try:
129
- image_tensor = clip_preprocess(cropped_img).unsqueeze(0).to(DEVICE)
130
- text_tokens = clip.tokenize(all_prompts).to(DEVICE)
131
-
132
- with torch.no_grad():
133
- logits, _ = clip_model(image_tensor, text_tokens)
134
- all_probs = logits.softmax(dim=-1).cpu().numpy()[0]
135
-
136
- drip_len = len(style_prompts['drippy'])
137
- mid_len = len(style_prompts['mid'])
138
- drip_score = np.mean(all_probs[0 : drip_len])
139
- mid_score = np.mean(all_probs[drip_len : drip_len + mid_len])
140
- not_score = np.mean(all_probs[drip_len + mid_len : style_prompts_end_index])
141
-
142
- if drip_score > mid_score and drip_score > not_score:
143
- category_key = 'drippy'
144
- final_score = drip_score
145
- elif mid_score > not_score:
146
- category_key = 'mid'
147
- final_score = mid_score
148
- else:
149
- category_key = 'not_drippy'
150
- final_score = not_score
151
-
152
- category_label = CATEGORY_LABEL_MAP[category_key]
153
- final_score_str = f"{final_score:.2f}"
154
- print(f"Style analysis: Category={category_label}, Score={final_score_str}")
155
-
156
- clothing_items_detected_by_clip = get_top_clothing(all_probs, n=1)
157
- if clothing_items_detected_by_clip:
158
- detected_clothing_item = clothing_items_detected_by_clip[0]
159
- print(f"Top clothing item identified by CLIP: {detected_clothing_item}")
160
- else:
161
- print("Warning: CLIP did not identify a top clothing item.")
162
- detected_clothing_item = "fit"
163
-
164
- except Exception as e:
165
- print(f"Error during CLIP analysis or clothing selection: {e}")
166
- return ("<p style='color: #FF5555;'>Error during analysis.</p>",
167
- None, f"Analysis Error: {e}")
168
-
169
- # 3) Generate Response and TTS
170
- try:
171
- response_text = random.choice(response_templates[category_key]).format(item=detected_clothing_item)
172
- tts_path = os.path.join(tempfile.gettempdir(), f"drip_{uuid.uuid4().hex}.mp3")
173
- tts = gTTS(text=response_text, lang='en', tld='com', slow=False)
174
- tts.save(tts_path)
175
- print(f"Generated TTS response: '{response_text}' saved to {tts_path}")
176
-
177
- # --- Updated HTML Output ---
178
- # Simpler structure, relies more on CSS for styling defined below
179
- category_html = f"""
180
- <div class='results-container'>
181
- <h2 class='result-category'>RATING: {category_label.upper()}</h2>
182
- <p class='result-score'>Style Score: {final_score_str}</p>
183
- </div>
184
- """
185
- return category_html, tts_path, response_text
186
-
187
- except Exception as e:
188
- print(f"Error during response/TTS generation: {e}")
189
- category_html = f"""
190
- <div class='results-container'>
191
- <h2 class='result-category'>Result: {category_label.upper()} (Score: {final_score_str})</h2>
192
- <p class='result-score' style='color: #FFAAAA;'>Error generating audio/full response.</p>
193
- </div>
194
- """
195
- return category_html, None, f"Analysis complete ({category_label}), but error generating audio/response."
196
-
197
-
198
- # --- Elite Fashion / Techno CSS ---
199
- custom_css = """
200
- :root {
201
- --primary-bg-color: #000000;
202
- --secondary-bg-color: #1A1A1A;
203
- --text-color: #FFFFFF;
204
- --accent-color: #1F04FF;
205
- --border-color: #333333; /* Slightly lighter than secondary bg for subtle definition */
206
- --input-bg-color: #1A1A1A;
207
- --button-text-color: #FFFFFF;
208
- --body-text-size: 16px; /* Base text size */
209
- }
210
- /* --- Global Styles --- */
211
- body, .gradio-container {
212
- background-color: var(--primary-bg-color) !important;
213
- color: var(--text-color) !important;
214
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; /* Modern font stack */
215
- font-size: var(--body-text-size);
216
- }
217
- /* Hide default Gradio footer */
218
- footer { display: none !important; }
219
- /* --- Component Styling --- */
220
- .gr-block { /* General block container */
221
- background-color: var(--secondary-bg-color) !important;
222
- border: 1px solid var(--border-color) !important;
223
- border-radius: 8px !important; /* Slightly rounded corners */
224
- padding: 15px !important;
225
- box-shadow: none !important; /* Remove default shadows */
226
- }
227
- /* Input/Output Text Areas & General inputs */
228
- .gr-input, .gr-output, .gr-textbox textarea, .gr-dropdown select, .gr-checkboxgroup input {
229
- background-color: var(--input-bg-color) !important;
230
- color: var(--text-color) !important;
231
- border: 1px solid var(--border-color) !important;
232
- border-radius: 5px !important;
233
- }
234
- .gr-textbox textarea::placeholder { /* Style placeholder text if needed */
235
- color: #888888 !important;
236
- }
237
- /* Component Labels */
238
- .gr-label span, .gr-label .label-text {
239
- color: var(--text-color) !important;
240
- font-weight: 500 !important; /* Slightly bolder labels */
241
- font-size: 0.95em !important;
242
- margin-bottom: 8px !important; /* Space below label */
243
- }
244
- /* Image Input/Output */
245
- .gr-image {
246
- background-color: var(--primary-bg-color) !important; /* Match main background */
247
- border: 1px dashed var(--border-color) !important; /* Dashed border for drop zone */
248
- border-radius: 8px !important;
249
- overflow: hidden; /* Ensure image stays within bounds */
250
- }
251
- .gr-image img {
252
- border-radius: 6px !important; /* Slightly round image corners */
253
- object-fit: contain; /* Ensure image fits well */
254
- }
255
- .gr-image .no-image, .gr-image .upload-button { /* Placeholder text/button inside image component */
256
- color: #AAAAAA !important;
257
- }
258
- /* Audio Component */
259
- .gr-audio > div:first-of-type { /* Target the container around the audio player */
260
- border: 1px solid var(--border-color) !important;
261
- background-color: var(--secondary-bg-color) !important;
262
- border-radius: 5px !important;
263
- padding: 10px !important;
264
- }
265
- .gr-audio audio { /* Style the audio player itself */
266
- width: 100%; /* Make player responsive */
267
- filter: invert(1) hue-rotate(180deg); /* Basic dark theme for player controls */
268
- }
269
- /* --- Button Styling --- */
270
- .gr-button { /* General button style reset */
271
- border: none !important;
272
- border-radius: 5px !important;
273
- transition: background-color 0.2s ease, transform 0.1s ease;
274
- font-weight: 600 !important;
275
- }
276
- .gr-button-primary { /* Specific styling for the primary Analyze button */
277
- background-color: var(--accent-color) !important;
278
- color: var(--button-text-color) !important;
279
- font-size: 1.1em !important; /* Make primary button slightly larger */
280
- padding: 12px 20px !important; /* Adjust padding */
281
- }
282
- .gr-button-primary:hover {
283
- background-color: #482FFF !important; /* Slightly lighter blue on hover */
284
- transform: scale(1.02); /* Subtle scale effect */
285
- box-shadow: 0 0 10px var(--accent-color); /* Add a glow effect */
286
- }
287
- .gr-button-primary:active {
288
- transform: scale(0.98); /* Press down effect */
289
- }
290
- /* --- Typography & Content --- */
291
- h1, h2, h3 {
292
- color: var(--text-color) !important;
293
- font-weight: 600; /* Bold headings */
294
- letter-spacing: 0.5px; /* Add slight letter spacing */
295
- }
296
- .prose h1 { /* Target Markdown H1 specifically if needed */
297
- text-align: center;
298
- margin-bottom: 25px !important;
299
- font-size: 2em !important; /* Larger title */
300
- text-transform: uppercase; /* Uppercase for impact */
301
- letter-spacing: 1.5px;
302
- }
303
- .prose p { /* Target Markdown Paragraph */
304
- color: #CCCCCC !important; /* Slightly dimmer text for descriptions */
305
- font-size: 0.95em;
306
- text-align: center;
307
- }
308
- /* Custom styling for the results HTML block */
309
- .results-container {
310
- text-align: center;
311
- padding: 20px;
312
- border: 1px solid var(--accent-color); /* Use accent color for border */
313
- border-radius: 8px;
314
- background: linear-gradient(145deg, var(--secondary-bg-color), #2a2a2a); /* Subtle gradient */
315
- }
316
- .result-category {
317
- color: var(--accent-color) !important; /* Use accent color for category */
318
- font-size: 1.5em;
319
- margin-bottom: 5px;
320
- font-weight: 700;
321
- text-transform: uppercase;
322
- }
323
- .result-score {
324
- color: var(--text-color) !important;
325
- font-size: 1.1em;
326
- margin-top: 0;
327
- }
328
- /* --- Layout Adjustments --- */
329
- .gradio-container {
330
- max-width: 850px !important; /* Slightly wider max-width */
331
- margin: auto !important;
332
- padding-top: 30px; /* Add some space at the top */
333
- }
334
- .gr-row {
335
- gap: 25px !important; /* Increase gap between columns */
336
- }
337
- """
338
-
339
-
340
- # --- Gradio Interface (Now using the custom CSS) ---
341
- with gr.Blocks(css=custom_css, theme=gr.themes.Base(primary_hue="neutral", secondary_hue="neutral", text_size=gr.themes.sizes.text_lg)) as demo: # Use Base theme to minimize default styles
342
- # Title using Markdown (styled by CSS)
343
- gr.Markdown("<h1>💧 DripAI: Rate Your Fit 💧</h1>")
344
-
345
- with gr.Row():
346
- with gr.Column(scale=1, min_width=350): # Assign min width for better responsiveness
347
- input_image = gr.Image(
348
- type='pil',
349
- label="Upload Your Outfit", # Simpler label
350
- sources=['upload', 'webcam', 'clipboard'],
351
- height=450 # Slightly taller image area
352
- )
353
- analyze_button = gr.Button(
354
- "Analyze Outfit",
355
- variant="primary",
356
- # size="lg" removed, controlled by CSS
357
- )
358
-
359
- with gr.Column(scale=1, min_width=350): # Assign min width
360
- gr.Markdown("### ANALYSIS RESULTS") # Simple heading
361
- category_html = gr.HTML(label="Rating & Score") # Label for screen readers/context
362
- response_box = gr.Textbox(
363
- lines=3,
364
- label="Verbal Feedback", # Updated label
365
- interactive=False
366
- )
367
- audio_output = gr.Audio(
368
- autoplay=True, # Changed default to false, user can click play
369
- label="Audio Feedback",
370
- streaming=False
371
- )
372
-
373
- # Bind the analysis function to the button click
374
- analyze_button.click(
375
- fn=analyze_outfit,
376
- inputs=[input_image],
377
- outputs=[category_html, audio_output, response_box]
378
- )
379
-
380
- # Footer description text
381
- gr.Markdown("<p>Upload, paste, or use your webcam to capture your outfit. DripAI evaluates your style.</p>")
382
-
383
- # --- Launch App ---
384
- if __name__ == "__main__":
385
- demo.launch(debug=True)