kdhht2334 commited on
Commit
aac6a6d
·
1 Parent(s): 0a1a121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -31
app.py CHANGED
@@ -11,6 +11,19 @@ chk_point = "kdhht2334/autotrain-diffusion-emotion-facial-expression-recognition
11
 
12
  model = AutoModelForImageClassification.from_pretrained(chk_point)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  try:
15
  pipe = pipeline(
16
  "image-classification",
@@ -24,34 +37,46 @@ except NotImplementedError:
24
  pipe = pipeline("image-classification", chk_point, device=device)
25
 
26
 
27
- def make_label_folders():
28
- folders = model.config.label2id.keys()
29
- for folder in folders:
30
- folder = Path(folder)
31
- if not folder.exists():
32
- folder.mkdir()
33
- return folders
34
-
35
-
36
- def predictions_into_folders(files):
37
- files = [file.name for file in files]
38
- files = [
39
- file for file in files if not file.startswith(".") and "DS_Store" not in file
40
- ]
41
- folders = make_label_folders()
42
- predictions = pipe(files)
43
- for file, prediction in zip(files, predictions):
44
- label = prediction[0]["label"]
45
- file_name = Path(file).name
46
- shutil.copy(file, f"{label}/{file_name}")
47
- for folder in folders:
48
- shutil.make_archive(folder, "zip", ".", folder)
49
- return [f"{folder}.zip" for folder in folders]
50
-
51
- demo = gr.Interface(
52
- predictions_into_folders,
53
- gr.Files(file_count="directory", file_types=["image"]),
54
- gr.Files(),
55
- cache_examples=True,
56
- )
57
- demo.launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  model = AutoModelForImageClassification.from_pretrained(chk_point)
13
 
14
+ emotion_dict = {
15
+ 'neutral': '0',
16
+ 'happy': '1',
17
+ 'sad' :'2',
18
+ 'surprise': '3',
19
+ 'fear': '4',
20
+ 'disgust': '5',
21
+ 'angry': '6',
22
+ 'uncertain': '7',
23
+ 'nonface': '8',
24
+ }
25
+
26
+
27
  try:
28
  pipe = pipeline(
29
  "image-classification",
 
37
  pipe = pipeline("image-classification", chk_point, device=device)
38
 
39
 
40
+ # def make_label_folders():
41
+ # folders = model.config.label2id.keys()
42
+ # for folder in folders:
43
+ # folder = Path(folder)
44
+ # if not folder.exists():
45
+ # folder.mkdir()
46
+ # return folders
47
+
48
+
49
+ # def predictions_into_folders(files):
50
+ # files = [file.name for file in files]
51
+ # files = [
52
+ # file for file in files if not file.startswith(".") and "DS_Store" not in file
53
+ # ]
54
+ # folders = make_label_folders()
55
+ # predictions = pipe(files)
56
+ # for file, prediction in zip(files, predictions):
57
+ # label = prediction[0]["label"]
58
+ # file_name = Path(file).name
59
+ # shutil.copy(file, f"{label}/{file_name}")
60
+ # for folder in folders:
61
+ # shutil.make_archive(folder, "zip", ".", folder)
62
+ # return [f"{folder}.zip" for folder in folders]
63
+
64
+ # demo = gr.Interface(
65
+ # predictions_into_folders,
66
+ # gr.Files(file_count="directory", file_types=["image"]),
67
+ # gr.Files(),
68
+ # cache_examples=True,
69
+ # )
70
+ # demo.launch(enable_queue=True)
71
+
72
+
73
+ def predict(image):
74
+ predictions = pipe(image)
75
+ return {p["label"]: p["score"] for p in predictions}
76
+
77
+ gr.Interface(
78
+ predict,
79
+ inputs=gr.inputs.Image(label="Upload image", type="filepath"),
80
+ outputs=gr.outputs.Label(num_top_classes=7),
81
+ title="FER trained on DiffusionFER dataset",
82
+ ).launch()