Spaces:
Runtime error
Runtime error
Initial commit
Browse files- app.py +57 -0
- examples-01.jpg +0 -0
- examples-02.jpg +0 -0
- examples-03.jpg +0 -0
- requirements.txt +7 -0
- sentences_ja.txt +50 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import clip
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import simple_chalk as chalk
|
5 |
+
import torch
|
6 |
+
from googletrans import Translator
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
TOP_N = 5
|
10 |
+
|
11 |
+
|
12 |
+
def match_texts(in_img: Image) -> list:
|
13 |
+
|
14 |
+
"""ใขใใซๆบๅ"""
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
model, preprocess = clip.load("ViT-B/32", device=device)
|
17 |
+
|
18 |
+
""" ใใญในใๅๅฆ็ """
|
19 |
+
translator = Translator()
|
20 |
+
trans_dict = {}
|
21 |
+
with open("./sentences_ja.txt") as f:
|
22 |
+
for ja_sentence in f:
|
23 |
+
en_sentence = translator.translate(ja_sentence, dest="en", src="ja").text
|
24 |
+
trans_dict[en_sentence] = ja_sentence
|
25 |
+
en_sentences = list(trans_dict.keys())
|
26 |
+
texts = clip.tokenize(en_sentences).to(device)
|
27 |
+
|
28 |
+
""" ็ปๅๅๅฆ็ """
|
29 |
+
# image: Tensor (3, 224, 224) -> (1, 3, 224, 224)
|
30 |
+
image = preprocess(in_img).unsqueeze(0).to(device)
|
31 |
+
|
32 |
+
""" CLIP ใขใใซใงๅฆ็ """
|
33 |
+
with torch.no_grad():
|
34 |
+
logits_per_image, logits_per_text = model(image, texts)
|
35 |
+
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
|
36 |
+
probs_per_image = probs.reshape(-1)
|
37 |
+
sort_index = np.argsort(probs_per_image)[::-1]
|
38 |
+
|
39 |
+
""" ๅฆ็็ตๆ๏ผใใญในใ๏ผๅบๅ """
|
40 |
+
idxs = sort_index.tolist()
|
41 |
+
# ่ฑ่ชๅบๅ
|
42 |
+
# confidences = {en_sentences[i]: float(probs_per_image[i]) for i in idxs}
|
43 |
+
# ๆฅๆฌ่ชๅคๆๅบๅ
|
44 |
+
confidences = {trans_dict[en_sentences[i]]: float(probs_per_image[i]) for i in idxs}
|
45 |
+
return confidences
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
inputs = gr.Image(type="pil", label="็ปๅใๅ
ฅๅ")
|
50 |
+
outputs = gr.Label(num_top_classes=TOP_N, label=f"ไธ่ดใใใใญในใ Top-{TOP_N}")
|
51 |
+
gr.Interface(
|
52 |
+
fn=match_texts,
|
53 |
+
inputs=inputs,
|
54 |
+
outputs=outputs,
|
55 |
+
examples=["examples-01.jpg", "examples-02.jpg", "examples-03.jpg"],
|
56 |
+
allow_flagging="never",
|
57 |
+
).launch(share=False)
|
examples-01.jpg
ADDED
![]() |
examples-02.jpg
ADDED
![]() |
examples-03.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
clip-by-openai==1.1.0
|
2 |
+
googletrans==4.0.0rc1
|
3 |
+
gradio==3.0.13
|
4 |
+
numpy==1.22.3
|
5 |
+
pillow==9.1.0
|
6 |
+
simple-chalk==0.1.0
|
7 |
+
torch
|
sentences_ja.txt
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ๆฉใใงใใ
|
2 |
+
้ป่ฉฑใใใใฆใใ
|
3 |
+
่ฉฑใใชใใ็ฌใฃใฆใใ
|
4 |
+
ๆใฃใฆใใ
|
5 |
+
่ณๆใ่ชญใใงใใ
|
6 |
+
ๆฅๅฎขใใฆใใ
|
7 |
+
ๅใๅใใๅฏพๅฟใใฆใใ
|
8 |
+
่ชใใใใฆใใ
|
9 |
+
ๅฅ็ดๆธใ่ชฌๆใใฆใใ
|
10 |
+
ๅๅใๅงใใฆใใ
|
11 |
+
ใใผใใฃใผใธๆๅพ
ใใฆใใ
|
12 |
+
ๅพใใใๅฃฐใใใใฆใใ
|
13 |
+
ใๅฎขๆงใซใใใใคใใฆใใ
|
14 |
+
็ฌ้กใงๅพฎ็ฌใใงใใ
|
15 |
+
ๆถใๆตใใฆใใ
|
16 |
+
ๆฒใใใงใใ
|
17 |
+
ๅคงๅฃฐใๅบใใฆ็ฌใฃใฆใใ
|
18 |
+
ๅๅผใฎๆ็ถใใ่กใชใฃใฆใใ
|
19 |
+
ๆ็ถใใซใคใใฆใๆกๅ
ใใฆใใ
|
20 |
+
้ใ่ใใใฆใใ
|
21 |
+
ใใฝใณใณใงใใผใฟใๅ
ฅๅใใฆใใ
|
22 |
+
ใซใกใฉใงๆฎๅฝฑใใฆใใ
|
23 |
+
ๆใใคใใฆๆญฉใใฆใใ
|
24 |
+
่ปใ้่ปขใใฆใใ
|
25 |
+
่ช่ปข่ปใซไนใฃใฆใใ
|
26 |
+
ใกใฌใใใใใฆใใ
|
27 |
+
ใตใณใฐใฉในใใใใฆใใ
|
28 |
+
้ณๆฅฝใ่ใใฆใใ
|
29 |
+
ๆฑใๆตใใฆ่ตฐใฃใฆใใ
|
30 |
+
่พบใใ่ฆๅใใฆใใ
|
31 |
+
่
ใไธใใฆใใ
|
32 |
+
็ป้ขใ่ฆใ่พผใใงใใ
|
33 |
+
ใคในใซๅบงใฃใฆใใ
|
34 |
+
่ฉใซๆใๅใใฆใใ
|
35 |
+
ๆกๆใใฆใใ
|
36 |
+
็ฎใใคใถใฃใฆใใ
|
37 |
+
่
ใ็ตใใง่ใ่พผใใงใใ
|
38 |
+
้ ญใๆฑใใฆใใ
|
39 |
+
่
็ซใฆไผใใใใฆใใ
|
40 |
+
่
ใๅใใฆใใ
|
41 |
+
ๆ็ซ ใๆธใใฆใใ
|
42 |
+
ๆธ้กใไฝๆใใฆใใ
|
43 |
+
ไปไบใซ่ฟฝใใใฆใใ
|
44 |
+
ใ่ฉซใณใใใฆใใ
|
45 |
+
้ ญใไธใใฆใใ
|
46 |
+
ๅฌใใใใซ็ฌใฃใฆใใ
|
47 |
+
ไฝใใๆขใใฆใใ
|
48 |
+
ๆฉๆขฐใๆไฝใใฆใใ
|
49 |
+
ๆใๆฏใฃใฆใใ
|
50 |
+
็ตๆใ่ฆใฆ่ฝใก่พผใใงใใ
|