Spaces:
Running
Running
| import gradio as gr | |
| import numpy as np | |
| import cv2 | |
| import os | |
| from huggingface_hub import hf_hub_download | |
| import importlib.util | |
| REPO_ID = "IFMedTech/Skin-Analysis" | |
| # List of Python files and corresponding class names | |
| PY_MODULES = { | |
| "dark_circles.py": "DarkCircleDetector", | |
| "inflammation.py": "RednessDetector", | |
| "texture.py": "TextureDetector", | |
| "skin_tone.py": "SkinToneDetector", | |
| "oiliness.py": "OilinessDetector" # Added oiliness module | |
| } | |
| def dynamic_import(module_path, class_name): | |
| spec = importlib.util.spec_from_file_location(class_name, module_path) | |
| module = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(module) | |
| return getattr(module, class_name) | |
| # Dynamically download and import modules | |
| detector_classes = {} | |
| token = os.environ.get("HUGGINGFACE_TOKEN") | |
| if not token: | |
| raise ValueError("Please set the HUGGINGFACE_TOKEN environment variable in repo secrets!") | |
| for py_file, class_name in PY_MODULES.items(): | |
| py_path = hf_hub_download( | |
| repo_id=REPO_ID, | |
| filename=py_file, | |
| token=token | |
| ) | |
| detector_classes[class_name] = dynamic_import(py_path, class_name) | |
| # --- Skin analysis function using downloaded detectors --- | |
| def analyze_skin(image: np.ndarray, analysis_type: str) -> np.ndarray: | |
| output = image.copy() | |
| if analysis_type == "dark circles": | |
| detector = detector_classes["DarkCircleDetector"](image) | |
| result = detector.predict_json() | |
| output = detector.draw_json() | |
| elif analysis_type == "redness": | |
| detector = detector_classes["RednessDetector"](image) | |
| result = detector.predict_json() | |
| output = result.get("overlay_image") | |
| elif analysis_type == "texture": | |
| detector = detector_classes["TextureDetector"](image) | |
| result = detector.predict_json() | |
| print(result) | |
| output = result.get("overlay_image") | |
| elif analysis_type == "skin tone": | |
| detector = detector_classes["SkinToneDetector"](image) | |
| result = detector.predict_json() | |
| output = result.get("output_image") | |
| elif analysis_type == "oiliness": | |
| detector = detector_classes["OilinessDetector"](image) | |
| result = detector.predict_json() | |
| if result.get("detected"): | |
| output = result.get("overlay_image") | |
| print(f"Oiliness scores: {result.get('scores')}") | |
| else: | |
| print(f"Oiliness detection error: {result.get('error')}") | |
| return output | |
| # --- Gradio Interface code --- | |
| app = gr.Interface( | |
| fn=analyze_skin, | |
| inputs=[ | |
| gr.Image(type="numpy", label="Upload your face image"), | |
| gr.Radio( | |
| ["dark circles", "redness", "texture", "skin tone", "oiliness"], # Added oiliness option | |
| label="Select Skin Analysis Type" | |
| ), | |
| ], | |
| outputs=gr.Image(type="numpy", label="Analyzed Image"), | |
| title="Skin Analysis Demo", | |
| description="Upload an image and choose a skin analysis parameter." | |
| ) | |
| if __name__ == "__main__": | |
| app.launch(server_name="0.0.0.0", server_port=7860) | |