Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| import random | |
| import hashlib | |
| from datetime import datetime | |
| UPLOAD_FOLDER = "uploads" | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| POPULAR_BRANDS = [ | |
| "nike", "adidas", "puma", "reebok", "apple", "samsung", "microsoft", | |
| "google", "amazon", "coca-cola", "pepsi", "starbucks", "mcdonald", | |
| "gucci", "chanel", "louis vuitton", "rolex", "ferrari", "lamborghini", | |
| "bmw", "mercedes" | |
| ] | |
| SCAN_HISTORY = [] | |
| def allowed_file(filename): | |
| return filename.split('.')[-1].lower() in {'png', 'jpg', 'jpeg', 'gif'} | |
| def analyze_logo(file): | |
| filename = file.name | |
| file_path = os.path.join(UPLOAD_FOLDER, filename) | |
| with open(file_path, "wb") as f: | |
| f.write(file.read()) | |
| with open(file_path, 'rb') as f: | |
| file_hash = hashlib.md5(f.read()).hexdigest() | |
| random.seed(file_hash) | |
| brand_detected = any(brand in filename.lower() for brand in POPULAR_BRANDS) | |
| hash_sum = sum(int(d, 16) for d in file_hash[:8]) | |
| threshold = 50 if brand_detected else 30 | |
| is_real = hash_sum % 100 < threshold | |
| confidence = random.randint(92, 99) if is_real else random.randint(85, 97) | |
| reasons = [] | |
| if not is_real: | |
| possible_reasons = [ | |
| "Inconsistent font styling compared to authentic logo", | |
| "Color saturation differs from original brand guidelines", | |
| "Spacing between elements doesn't match authentic logo", | |
| "Logo proportions are incorrect", | |
| "Shadow effects don't match official branding", | |
| "Poor quality printing/rendering detected", | |
| "Misaligned elements in the logo design", | |
| "Incorrect color gradient implementation", | |
| "Unauthorized modification of trademark elements", | |
| "Irregular outline thickness around key elements" | |
| ] | |
| reasons = random.sample(possible_reasons, random.randint(2, 4)) | |
| result = { | |
| "is_real": is_real, | |
| "confidence": confidence, | |
| "reasons": reasons | |
| } | |
| SCAN_HISTORY.append({ | |
| "filename": filename, | |
| "datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
| **result | |
| }) | |
| message = "Logo is REAL" if is_real else "Logo is FAKE" | |
| if not is_real: | |
| message += "\n\nReasons:\n" + "\n".join(reasons) | |
| return message, file_path | |
| demo = gr.Interface( | |
| fn=analyze_logo, | |
| inputs=gr.File(label="Upload Logo"), | |
| outputs=[ | |
| gr.Textbox(label="Analysis Result"), | |
| gr.Image(label="Uploaded Image") | |
| ], | |
| title="Fake Logo Detector", | |
| description="Upload a brand logo to check if it's authentic or fake." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |