Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
for d in data:
|
9 |
-
final[d["label"]] = d["score"]
|
10 |
-
return final
|
11 |
-
demo_aesthetic = gr.Interface(fn=aesthetic, inputs=gr.Image(type="pil"), outputs=gr.Label(label="aesthetic"))
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
final = {}
|
26 |
for d in data:
|
27 |
final[d["label"]] = d["score"]
|
28 |
return final
|
29 |
-
|
30 |
|
31 |
-
gr.Parallel(demo_aesthetic
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, ImageClassificationPipeline
|
3 |
|
4 |
+
class MultiClassLabel(ImageClassificationPipeline):
|
5 |
+
def postprocess(self, model_outputs, top_k=5):
|
6 |
+
if top_k > self.model.config.num_labels:
|
7 |
+
top_k = self.model.config.num_labels
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
if self.framework == "pt":
|
10 |
+
probs = model_outputs.logits.sigmoid()[0]
|
11 |
+
scores, ids = probs.topk(top_k)
|
12 |
+
elif self.framework == "tf":
|
13 |
+
probs = stable_softmax(model_outputs.logits, axis=-1)[0]
|
14 |
+
topk = tf.math.top_k(probs, k=top_k)
|
15 |
+
scores, ids = topk.values.numpy(), topk.indices.numpy()
|
16 |
+
else:
|
17 |
+
raise ValueError(f"Unsupported framework: {self.framework}")
|
18 |
|
19 |
+
scores = scores.tolist()
|
20 |
+
ids = ids.tolist()
|
21 |
+
return [{"score": score, "label": self.model.config.id2label[_id]} for score, _id in zip(scores, ids)]
|
22 |
+
|
23 |
+
pipe_aesthetic = pipeline("image-classification", "./sonic", pipeline_class=MultiClassLabel)
|
24 |
+
|
25 |
+
def aesthetic(input_img):
|
26 |
+
data = pipe_aesthetic(input_img, top_k=5)
|
27 |
final = {}
|
28 |
for d in data:
|
29 |
final[d["label"]] = d["score"]
|
30 |
return final
|
31 |
+
demo_aesthetic = gr.Interface(fn=aesthetic, inputs=gr.Image(type="pil"), outputs=gr.Label(label="characters"))
|
32 |
|
33 |
+
gr.Parallel(demo_aesthetic).launch()
|