IFMedTechdemo commited on
Commit
d396774
·
verified ·
1 Parent(s): 73504a2

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile (1) +14 -0
  2. README (3).md +10 -0
  3. main.py +75 -0
  4. requirements (3).txt +8 -0
Dockerfile (1) ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11
2
+ WORKDIR /app
3
+
4
+ RUN apt-get update && apt-get install -y \
5
+ libgl1 \
6
+ libglib2.0-0 \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ COPY requirements.txt .
10
+ RUN pip install --no-cache-dir -r requirements.txt
11
+ COPY . .
12
+ EXPOSE 8000
13
+ EXPOSE 7860
14
+ CMD ["python", "main.py"]
README (3).md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: SkinAnalysis
3
+ emoji: 🔎
4
+ colorFrom: blue
5
+ colorTo: gray
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
main.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
+ import os
5
+ from huggingface_hub import hf_hub_download
6
+ import importlib.util
7
+
8
+ REPO_ID = "IFMedTech/Skin-Analysis"
9
+ # List of Python files and corresponding class names
10
+ PY_MODULES = {
11
+ "dark_circles.py": "DarkCircleDetector",
12
+ "inflammation.py": "RednessDetector",
13
+ "texture.py": "TextureDetector",
14
+ "skin_tone.py": "SkinToneDetector"
15
+ }
16
+
17
+ def dynamic_import(module_path, class_name):
18
+ spec = importlib.util.spec_from_file_location(class_name, module_path)
19
+ module = importlib.util.module_from_spec(spec)
20
+ spec.loader.exec_module(module)
21
+ return getattr(module, class_name)
22
+
23
+ # Dynamically download and import modules
24
+ detector_classes = {}
25
+ token = os.environ.get("HUGGINGFACE_TOKEN")
26
+ if not token:
27
+ raise ValueError("Please set the HUGGINGFACE_TOKEN environment variable in repo secrets!")
28
+
29
+ for py_file, class_name in PY_MODULES.items():
30
+ py_path = hf_hub_download(
31
+ repo_id=REPO_ID,
32
+ filename=py_file,
33
+ token=token
34
+ )
35
+ detector_classes[class_name] = dynamic_import(py_path, class_name)
36
+
37
+ # --- Skin analysis function using downloaded detectors ---
38
+ def analyze_skin(image: np.ndarray, analysis_type: str) -> np.ndarray:
39
+ output = image.copy()
40
+ if analysis_type == "dark circles":
41
+ detector = detector_classes["DarkCircleDetector"](image)
42
+ result = detector.predict_json()
43
+ output = detector.draw_json()
44
+ elif analysis_type == "redness":
45
+ detector = detector_classes["RednessDetector"](image)
46
+ result = detector.predict_json()
47
+ output = result.get("overlay_image")
48
+ elif analysis_type == "texture":
49
+ detector = detector_classes["TextureDetector"](image)
50
+ result = detector.predict_json()
51
+ print(result)
52
+ output = result.get("overlay_image")
53
+ elif analysis_type == "skin tone":
54
+ detector = detector_classes["SkinToneDetector"](image)
55
+ result = detector.predict_json()
56
+ output = result.get("output_image")
57
+ return output
58
+
59
+ # --- Gradio Interface code ---
60
+ app = gr.Interface(
61
+ fn=analyze_skin,
62
+ inputs=[
63
+ gr.Image(type="numpy", label="Upload your face image"),
64
+ gr.Radio(
65
+ ["dark circles", "redness", "texture", "skin tone"],
66
+ label="Select Skin Analysis Type"
67
+ ),
68
+ ],
69
+ outputs=gr.Image(type="numpy", label="Analyzed Image"),
70
+ title="Skin Analysis Demo",
71
+ description="Upload an image and choose a skin analysis parameter."
72
+ )
73
+
74
+ if __name__ == "__main__":
75
+ app.launch(server_name="0.0.0.0", server_port=7860)
requirements (3).txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ opencv-python
2
+ gradio
3
+ mediapipe
4
+ pandas
5
+ scipy
6
+ skin-tone-classifier
7
+ numpy
8
+ huggingface-hub