Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
4 |
+
from transformers import CLIPProcessor, CLIPModel
|
5 |
+
import torch
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model_id = "openai/clip-vit-base-patch16" # You can choose a different CLIP model from Hugging Face
|
13 |
+
clipprocessor = CLIPProcessor.from_pretrained(model_id)
|
14 |
+
clipmodel = CLIPModel.from_pretrained(model_id).to(device)
|
15 |
+
|
16 |
+
|
17 |
+
model_id = "Salesforce/blip-image-captioning-base" ## load modelID for BLIP
|
18 |
+
blipmodel = BlipForConditionalGeneration.from_pretrained(model_id)
|
19 |
+
blipprocessor = BlipProcessor.from_pretrained(model_id)
|
20 |
+
|
21 |
+
|
22 |
+
def evaluate_caption(image, caption):
|
23 |
+
# # Pre-process image
|
24 |
+
# image = processor(images=image, return_tensors="pt").to(device)
|
25 |
+
|
26 |
+
# # Tokenize and encode the caption
|
27 |
+
# text = processor(text=caption, return_tensors="pt").to(device)
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
blip_input = blipprocessor(image, return_tensors="pt")
|
32 |
+
out = blipmodel.generate(**blip_input,max_new_tokens=50)
|
33 |
+
blip_caption = blipprocessor.decode(out[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
inputs = clipprocessor(text=[caption,blip_caption], images=image, return_tensors="pt", padding=True)
|
36 |
+
|
37 |
+
similarity_score = clipmodel(**inputs).logits_per_image
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
# Convert score to a float
|
42 |
+
score = similarity_score.softmax(dim=1).detach().numpy()
|
43 |
+
print(score)
|
44 |
+
if score[0][0]>score[0][1]:
|
45 |
+
winner = "The first caption is the human"
|
46 |
+
else:
|
47 |
+
winner = "The second caption is the human"
|
48 |
+
|
49 |
+
|
50 |
+
return blip_caption,winner
|
51 |
+
# ,gr.Image(type="pil", value="mukherjee_kushin_WIDPICS1.jpg")
|
52 |
+
|
53 |
+
|
54 |
+
callback = gr.HuggingFaceDatasetSaver('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', "gradioTest")
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
im_path_str = 'n01677366_12918.JPEG'
|
57 |
+
im_path = gr.Textbox(label="Image fname",value=im_path_str,interactive=False, visible=False)
|
58 |
+
# fn=evaluate_caption,
|
59 |
+
# inputs=["image", "text"]
|
60 |
+
|
61 |
+
with gr.Column():
|
62 |
+
im = gr.Image(label="Target Image", interactive = False, type="pil",value =f'images/{im_path_str}',height=500)
|
63 |
+
caps = gr.Textbox(label="Player 1 Caption")
|
64 |
+
submit_btn = gr.Button("Submit!!")
|
65 |
+
# outputs=["text","text"],
|
66 |
+
with gr.Column():
|
67 |
+
out1 = gr.Textbox(label="Player 2 (Machine) Caption",interactive=False)
|
68 |
+
out2 = gr.Textbox(label="Winner",interactive=False)
|
69 |
+
|
70 |
+
|
71 |
+
# live=False,
|
72 |
+
# interpretation="default"
|
73 |
+
callback.setup([caps, out1, out2, im_path], "flagged_data_points")
|
74 |
+
# callback.flag([image, caption, blip_caption, winner])
|
75 |
+
submit_btn.click(fn = evaluate_caption,inputs = [im,caps], outputs = [out1, out2],api_name="test").success(lambda *args: callback.flag(args), [caps, out1, out2, im_path], None, preprocess=False)
|
76 |
+
# with gr.Row():
|
77 |
+
# btn = gr.Button("Flag")
|
78 |
+
# btn.click(lambda *args: callback.flag(args), [im, caps, out1, out2], None, preprocess=False)
|
79 |
+
|
80 |
+
demo.launch(debug=False)
|