File size: 3,048 Bytes
d396774
 
 
 
 
 
 
 
 
 
 
 
 
ff07952
 
d396774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff07952
 
 
 
 
 
 
 
d396774
 
 
 
 
 
 
 
ff07952
d396774
 
 
 
 
 
 
 
 
 
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
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)