Catherine ZHOU commited on
Commit
2f404b6
·
1 Parent(s): 57ffe1d

update changes

Browse files
app.py CHANGED
@@ -26,17 +26,17 @@ with open(emb_filename, 'rb') as fIn:
26
  #print(f'img_emb: {print(img_emb)}')
27
  #print(f'img_names: {print(img_names)}')
28
 
29
-
30
- def search_text(query, top_k=1, top_rel_image=1):
31
  """" Search an image based on the text query.
32
 
33
  Args:
34
- query ([string]): [query you want search for]
35
- top_k (int, optional): [Amount of images o return]. Defaults to 1.
36
- top_rel_image (int, optional): [Relevance label of the image]. Defaults to 1
37
 
38
  Returns:
39
- [list]: [list of images that are related to the query.]
 
40
  """
41
  # First, we encode the query.
42
  inputs = tokenizer([query], padding=True, return_tensors="pt")
@@ -48,35 +48,102 @@ def search_text(query, top_k=1, top_rel_image=1):
48
  hits = util.semantic_search(query_emb, img_emb, top_k=top_k)[0]
49
 
50
  image = []
 
51
  for hit in hits:
52
  #print(img_names[hit['corpus_id']])
53
  object = Image.open(os.path.join(
54
  "photos/", img_names[hit['corpus_id']]))
55
  image.append(object)
 
56
  #print(f'array length is: {len(image)}')
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- ret_indx = int(top_rel_image)
59
- if ret_indx > top_k:
60
- raise IndexError("given relevance image label is out of range")
61
- else:
62
- return image[ret_indx-1]
 
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- iface = gr.Interface(
66
- title = "Text to Image using CLIP Model 📸",
67
- description = "My version of the Gradio Demo fo CLIP model. \n This demo is based on assessment for the 🤗 Huggingface course 2. \n To use it, simply write which image you are looking for. Read more at the links below.",
68
- article = "You find more information about this demo on my ✨ github repository [marcelcastrobr](https://github.com/marcelcastrobr/huggingface_course2)",
69
- fn=search_text,
70
- inputs=[
71
- gr.Textbox(lines=4,
72
- label="Write what you are looking for in an image...",
73
- placeholder="Text Here..."),
74
- gr.Slider(0, 5, step=1),
75
- gr.Dropdown(list(range(0, 6)), multiselect=False,
76
- label="Relevance Image Label")
77
- ],
78
- outputs=[gr.Image(
79
- label="Generated images", show_label=False, elem_id="output image"
80
- ).style(height="auto", width="auto")]
81
- ,examples=examples
82
- ).launch(debug=True)
 
26
  #print(f'img_emb: {print(img_emb)}')
27
  #print(f'img_names: {print(img_names)}')
28
 
29
+ # helper functions
30
+ def search_text(query, top_k=1):
31
  """" Search an image based on the text query.
32
 
33
  Args:
34
+ query ([string]): query you want search for
35
+ top_k (int, optional): Amount of images o return]. Defaults to 1.
 
36
 
37
  Returns:
38
+ [list]: list of images that are related to the query.
39
+ [list]: list of image embs that are related to the query.
40
  """
41
  # First, we encode the query.
42
  inputs = tokenizer([query], padding=True, return_tensors="pt")
 
48
  hits = util.semantic_search(query_emb, img_emb, top_k=top_k)[0]
49
 
50
  image = []
51
+ image_emb = []
52
  for hit in hits:
53
  #print(img_names[hit['corpus_id']])
54
  object = Image.open(os.path.join(
55
  "photos/", img_names[hit['corpus_id']]))
56
  image.append(object)
57
+ image_emb.append([img_emb[hit['corpus_id']]])
58
  #print(f'array length is: {len(image)}')
59
+ return image, image_emb
60
+
61
+ def select_image(evt: gr.SelectData):
62
+ """ Returns the index of the selected image
63
+
64
+ Argrs:
65
+ evt (SelectData): the event we are listening to
66
+
67
+ Returns:
68
+ int: index of the selected image
69
+ """
70
+ return evt.index
71
 
72
+ def select_image_relevance(evt: gr.SelectData, selected_embs, image_relevance_state):
73
+ """ Returns the relevance of the selected image
74
+
75
+ Args:
76
+ evt (SelectData): the event we are listening to
77
+ selected_embs (int): the index of the selected image
78
+ image_relevance_state (State): the current state of the image relevance
79
 
80
+ Returns:
81
+ state: the new state of the image relevance
82
+ """
83
+ image_relevance_state[selected_embs] = evt.value
84
+ return image_relevance_state
85
+
86
+
87
+ callback = gr.CSVLogger()
88
+ with gr.Blocks() as demo:
89
+ # create display
90
+ gr.Markdown(
91
+ """
92
+ # Text to Image using CLIP Model 📸
93
+
94
+ ---
95
+
96
+ My version of the Gradio Demo fo CLIP model with the option to select relevance level of each image.
97
+
98
+ This demo is based on assessment for the 🤗 Huggingface course 2. \n
99
+ To use it, simply write which image you are looking for. Read more at the links below.
100
+ """
101
+ )
102
+ with gr.Row():
103
+ with gr.Column():
104
+ query = gr.Textbox(lines=4,
105
+ label="Write what you are looking for in an image...",
106
+ placeholder="Text Here...")
107
+ top_k = gr.Slider(0, 5, step=1)
108
+ with gr.Column():
109
+ gallery = gr.Gallery(
110
+ label="Generated images", show_label=False, elem_id="gallery"
111
+ ).style(grid=[3], height="auto")
112
+ relevance = gr.Dropdown(list(range(0, 6)), multiselect=False,
113
+ label="How relevent is this image to your input text?")
114
+ with gr.Row():
115
+ with gr.Column():
116
+ submit_btn = gr.Button("Submit")
117
+ with gr.Column():
118
+ save_btn = gr.Button("Save")
119
+ gr.Markdown("## Here are some examples you can use:")
120
+ gr.Examples(examples, [query, top_k])
121
+
122
+ # when user input query and top_k
123
+ gallery_embs = [[] for _ in range(top_k.value)]
124
+ submit_btn.click(search_text, [query, top_k], [gallery, gallery_embs])
125
+ image_relevance = {embs: 0 for embs in gallery_embs}
126
+ image_relevance_state = gr.State(image_relevance, label="image_relevance_state")
127
+ selected_index = 0
128
+ callback.setup([image_relevance_state])
129
+
130
+ # when user select an image in the gallery
131
+ gallery.select(select_image, None, selected_index)
132
+ # when user select the relevance of the image
133
+ relevance.select(fn=select_image_relevance,
134
+ input=[gallery_embs[selected_index], image_relevance_state],
135
+ output=image_relevance_state)
136
+
137
+ # when user click save button
138
+ # we will flag the current image_relevance_state
139
+ save_btn.click(lambda *args: callback.flag(args), [image_relevance_state], None, preprocess=False)
140
+ gallery_embs = []
141
+
142
+ gr.Markdown(
143
+ """
144
+ You find more information about this demo on my ✨ github repository [marcelcastrobr](https://github.com/marcelcastrobr/huggingface_course2)
145
+ """
146
+ )
147
 
148
+ if __name__ == "__main__":
149
+ demo.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
flagged/Generated images/tmphv4zf24i.png ADDED
flagged/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Write what you are looking for in an image...,top_k,Relevance Image Label,Generated images,flag,username,timestamp
2
+ cat,3,2,/Users/zhilinzhou/Local-documents/Workspace/CLIP-image-search/flagged/Generated images/tmphv4zf24i.png,,,2023-04-03 20:56:01.812245