Tojichok commited on
Commit
5047ef1
·
verified ·
1 Parent(s): 337ff63

создал файл App

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import CLIPProcessor, CLIPModel
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Загружаем модель и препроцессор
7
+ model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
8
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
9
+
10
+ # Основная функция для классификации
11
+ def classify(image, labels):
12
+ # labels — строка вида "Titanic, Avatar, Interstellar"
13
+ candidate_labels = [label.strip() for label in labels.split(",") if label.strip()]
14
+ inputs = processor(text=candidate_labels, images=image, return_tensors="pt", padding=True)
15
+
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+ logits_per_image = outputs.logits_per_image
19
+ probs = logits_per_image.softmax(dim=1)
20
+
21
+ results = {label: round(float(p) * 100, 2) for label, p in zip(candidate_labels, probs[0])}
22
+ return results
23
+
24
+ # Интерфейс Gradio
25
+ iface = gr.Interface(
26
+ fn=classify,
27
+ inputs=[
28
+ gr.Image(type="pil", label="Загрузите постер фильма"),
29
+ gr.Textbox(label="Введите названия фильмов через запятую")
30
+ ],
31
+ outputs="label",
32
+ title="Zero-Shot CLIP Movie Classifier",
33
+ description="Сравни изображение с предложенными названиями фильмов"
34
+ )
35
+
36
+ iface.launch()