tasmiachow commited on
Commit
42cfb33
·
verified ·
1 Parent(s): e91f7b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import CLIPProcessor, CLIPModel
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load CLIP model and processor
7
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
8
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
9
+
10
+ # Define a list of target words for the game
11
+ words = ["cat", "car", "tree", "house", "dog"]
12
+
13
+
14
+ text_inputs = processor(text=words, return_tensors="pt", padding=True)
15
+ with torch.no_grad():
16
+ text_features = model.get_text_features(**text_inputs)
17
+
18
+
19
+ def guess_drawing(drawing):
20
+ image = Image.fromarray(drawing) # Convert drawing to PIL image
21
+ image_inputs = processor(images=image, return_tensors="pt")
22
+ with torch.no_grad():
23
+ image_features = model.get_image_features(**image_inputs)
24
+
25
+ # Calculate cosine similarity with each word
26
+ similarity = torch.nn.functional.cosine_similarity(image_features, text_features)
27
+ best_match = words[similarity.argmax().item()]
28
+ return f"AI's guess: {best_match}"
29
+
30
+
31
+ interface = gr.Interface(
32
+ fn=guess_drawing,
33
+ inputs=gr.Sketchpad(),
34
+ outputs="text",
35
+ live=True,
36
+ description="Draw something and see if the AI can guess it!"
37
+ )
38
+
39
+ interface.launch()
40
+