justina commited on
Commit
9bfc5a2
1 Parent(s): 71ba25b

add files from colab

Browse files
Files changed (2) hide show
  1. app.py +52 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import gradio as gr
3
+ from sklearn.metrics.pairwise import cosine_similarity
4
+
5
+ dataset = load_dataset("justina/celeb-identities")
6
+ model = SentenceTransformer("sentence-transformers/clip-ViT-B-16")
7
+
8
+ def predict(im1, im2):
9
+ image_embs = model.encode([im1, im2])
10
+ similarities = cosine_similarity(image_embs)
11
+ sim = similarities[0][1]
12
+ threshold = 0.65
13
+ if sim > threshold:
14
+ return sim, "SAME PERSON, UNLOCK PHONE"
15
+ else:
16
+ return sim, "DIFFERENT PEOPLE, DON'T UNLOCK"
17
+
18
+
19
+ with gr.Blocks() as demo:
20
+ gr.Markdown("Based on two images, the goal is to recognize the similarities/differences between facial images and determine whether or not to unlock a phone based on a cosine similarity score.")
21
+
22
+ with gr.Tab("Image"):
23
+ with gr.Row():
24
+ with gr.Column():
25
+ img_inputs = [gr.Image(type="pil", source="upload"),
26
+ gr.Image(type="pil", source="upload")]
27
+ examples = gr.Examples([["https://live.staticflickr.com/2883/33785597726_47880fa539_b.jpg","https://live.staticflickr.com/65535/49086637987_f7622c3345.jpg"],
28
+ ["https://live.staticflickr.com/3423/3197571945_123937185f_b.jpg", "https://live.staticflickr.com/7259/7001667239_11cece02c8_b.jpg"],
29
+ ["https://live.staticflickr.com/4015/4334237247_08af133b4b_b.jpg", "https://live.staticflickr.com/3701/9364116426_87b8918e9d_b.jpg"]],
30
+ inputs=img_inputs)
31
+ btn = gr.Button("Run")
32
+ with gr.Column():
33
+ btn.click(fn=predict,
34
+ inputs=img_inputs,
35
+ outputs=[gr.Number(label="Similarity"),
36
+ gr.Textbox(label="Message")],
37
+ )
38
+
39
+ with gr.Tab("Webcam"):
40
+ with gr.Row():
41
+ with gr.Column():
42
+ img_inputs = [gr.Image(type="pil", source="webcam"),
43
+ gr.Image(type="pil", source="webcam")]
44
+ btn = gr.Button("Run")
45
+ with gr.Column():
46
+ btn.click(fn=predict,
47
+ inputs=img_inputs,
48
+ outputs=[gr.Number(label="Similarity"),
49
+ gr.Textbox(label="Message")],
50
+ )
51
+
52
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ datasets