p1atdev commited on
Commit
38739b3
1 Parent(s): c50a0a6

feat: add support of cafe_aesthetic

Browse files
Files changed (6) hide show
  1. LICENSE +21 -0
  2. README.md +8 -3
  3. __pycache__/predictor.cpython-310.pyc +0 -0
  4. app.py +61 -1
  5. predictor.py +23 -0
  6. requirements.txt +9 -0
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Plat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- title: Saltacc Beit Bestimage Salt
3
- emoji: 🐨
4
  colorFrom: yellow
5
  colorTo: indigo
6
  sdk: gradio
@@ -9,4 +9,9 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
1
  ---
2
+ title: Waifu Aesthetics
3
+ emoji: 🧂☕
4
  colorFrom: yellow
5
  colorTo: indigo
6
  sdk: gradio
 
9
  pinned: false
10
  ---
11
 
12
+ Original repos:
13
+
14
+ - https://huggingface.co/cafeai/cafe_aesthetic
15
+ - https://huggingface.co/saltacc/beit-bestimage-salt
16
+
17
+ About BEiT: https://huggingface.co/docs/transformers/v4.30.0/en/model_doc/beit#beit
__pycache__/predictor.cpython-310.pyc ADDED
Binary file (1.11 kB). View file
 
app.py CHANGED
@@ -1,3 +1,63 @@
1
  import gradio as gr
2
 
3
- gr.Interface.load("models/saltacc/beit-bestimage-salt").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ from PIL import Image
4
+
5
+ from predictor import Predictor
6
+
7
+ MODEL_SALT_BESTIMAGE = "saltacc/beit-bestimage-salt"
8
+ MODEL_CAFE_AESTHETIC = "cafeai/cafe_aesthetic"
9
+
10
+ MODEL_NAMES = [
11
+ MODEL_SALT_BESTIMAGE,
12
+ MODEL_CAFE_AESTHETIC,
13
+ ]
14
+
15
+ models = {
16
+ MODEL_SALT_BESTIMAGE: Predictor(MODEL_SALT_BESTIMAGE),
17
+ MODEL_CAFE_AESTHETIC: Predictor(MODEL_CAFE_AESTHETIC),
18
+ }
19
+
20
+
21
+ def predict(image: Image.Image) -> list[dict[str, float]]:
22
+ results = []
23
+
24
+ for model_name in MODEL_NAMES:
25
+ results.append(models[model_name].predict([image])[0])
26
+
27
+ return results
28
+
29
+
30
+ def construct_ui():
31
+ with gr.Blocks() as ui:
32
+ with gr.Row():
33
+ with gr.Column():
34
+ input_image = gr.Image(
35
+ label="Input Image",
36
+ type="pil",
37
+ source="upload",
38
+ interactive=True,
39
+ )
40
+
41
+ submit_btn = gr.Button(
42
+ value="Submit",
43
+ variant="primary",
44
+ )
45
+
46
+ with gr.Column():
47
+ result_salt = gr.Label(
48
+ label=MODEL_SALT_BESTIMAGE,
49
+ )
50
+
51
+ result_cafe = gr.Label(
52
+ label=MODEL_CAFE_AESTHETIC,
53
+ )
54
+
55
+ submit_btn.click(
56
+ predict, inputs=[input_image], outputs=[result_salt, result_cafe]
57
+ )
58
+
59
+ return ui
60
+
61
+
62
+ if __name__ == "__main__":
63
+ construct_ui().launch()
predictor.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+
3
+ from transformers import BeitImageProcessor, BeitForImageClassification
4
+
5
+
6
+ class Predictor:
7
+ def __init__(self, model_id: str) -> None:
8
+ self.processor = BeitImageProcessor.from_pretrained(model_id)
9
+ self.model = BeitForImageClassification.from_pretrained(model_id)
10
+
11
+ def predict(self, images: list[Image.Image]) -> list[dict[str, float]]:
12
+ inputs = self.processor(images, return_tensors="pt")
13
+ logits = self.model(**inputs).logits.softmax(1) # 一応見た目が良いのでsoftmaxをかける
14
+
15
+ results = []
16
+
17
+ for scores in logits:
18
+ result = {}
19
+ for i, score in enumerate(scores):
20
+ result[self.model.config.id2label[i]] = score.item()
21
+ results.append(result)
22
+
23
+ return results
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ transformers
4
+ scipy
5
+ omegaconf
6
+ safetensors
7
+ numpy
8
+ opencv-python
9
+ torch