discus0434
commited on
Commit
•
db58a82
1
Parent(s):
a8836e6
Upload 2 files
Browse files- app.py +43 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from aesthetic_predictor_v2_5 import convert_v2_5_from_siglip
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
|
7 |
+
class AestheticPredictor:
|
8 |
+
def __init__(self):
|
9 |
+
# load model and preprocessor
|
10 |
+
self.model, self.preprocessor = convert_v2_5_from_siglip(
|
11 |
+
low_cpu_mem_usage=True,
|
12 |
+
trust_remote_code=True,
|
13 |
+
)
|
14 |
+
if torch.cuda.is_available():
|
15 |
+
self.model = self.model.to(torch.bfloat16).cuda()
|
16 |
+
|
17 |
+
def inference(self, image: Image.Image) -> float:
|
18 |
+
# preprocess image
|
19 |
+
pixel_values = self.preprocessor(
|
20 |
+
images=image.convert("RGB"), return_tensors="pt"
|
21 |
+
).pixel_values
|
22 |
+
|
23 |
+
if torch.cuda.is_available():
|
24 |
+
pixel_values = pixel_values.to(torch.bfloat16).cuda()
|
25 |
+
|
26 |
+
# predict aesthetic score
|
27 |
+
with torch.inference_mode():
|
28 |
+
score = self.model(pixel_values).logits.squeeze().float().cpu().numpy()
|
29 |
+
|
30 |
+
return score
|
31 |
+
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
aesthetic_predictor = AestheticPredictor()
|
35 |
+
with gr.Blocks(theme="soft") as blocks:
|
36 |
+
with gr.Column():
|
37 |
+
image = gr.Image(label="Input Image", type="pil")
|
38 |
+
button = gr.Button("Predict")
|
39 |
+
score = gr.Textbox(label="Aesthetic Score")
|
40 |
+
|
41 |
+
button.click(aesthetic_predictor.inference, inputs=image, outputs=score)
|
42 |
+
|
43 |
+
blocks.queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
aesthetic-predictor-v2-5
|
2 |
+
gradio
|