ysharma HF staff commited on
Commit
bf96618
1 Parent(s): 8ecc4c6

update layout

Browse files
Files changed (1) hide show
  1. app.py +35 -8
app.py CHANGED
@@ -38,10 +38,13 @@ def get_caption(image_in):
38
  data = response["data"][-1]
39
  return data
40
 
41
- def Image_similarity_search(image_in):
42
- #Get image caption from Bip2 Gradio space
43
- img_caption = get_caption(image_in)
44
- print(f"Image caption from Blip2 Gradio Space is - {img_caption}")
 
 
 
45
  #Searching the vector space
46
  search_result = search_index.similarity_search(img_caption)[0]
47
  #Formatting the search results
@@ -55,22 +58,46 @@ def Image_similarity_search(image_in):
55
  with gr.Blocks(css = """#label_mid {padding-top: 2px; padding-bottom: 2px;}
56
  #label_results {padding-top: 5px; padding-bottom: 1px;}
57
  """) as demo:
58
- with gr.Column(scale=2):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  pass
60
  with gr.Column(scale=1):
61
  label_top = gr.HTML(value= "<center>🖼️Upload an Image for your search📷</center>", elem_id="label_top")
62
  image_in = gr.Image(label="Upoload an Image for search", type='filepath', elem_id="image_in")
63
  label_mid = gr.HTML(value= "<p style='text-align: center; color: red;'>Or</center></p>", elem_id='label_mid')
64
  label_bottom = gr.HTML(value= "<center>🔍Type in your serch query and press Enter</center>", elem_id="label_bottom")
65
- search_query = gr.Textbox(placeholder="Example: A small cat sitting", label="", elem_id="search_query")
66
  #b1 = gr.Button("Search").style(full_width=False)
67
  label_results = gr.HTML(value= "<p style='text-align: center; color: blue; font-weight: bold;'>Search results from PlaygroundAI</center></p>", elem_id="label_results")
68
  img_search = gr.HTML(label = 'Image search results from PlaygroundAI dataset', elem_id="img_search")
69
  pai_prompt = gr.Textbox(label="Image prompt from PlaygroundAI dataset", elem_id="pai_prompt")
70
- with gr.Column(scale=2):
71
  pass
72
 
73
- image_in.change(Image_similarity_search, image_in, [pai_prompt, img_search] )
74
  #b1.click(Image_similarity_search, image_in, [pai_prompt, img_search] )
75
 
76
  demo.launch(debug=True)
 
38
  data = response["data"][-1]
39
  return data
40
 
41
+ def Image_similarity_search(image_in, search_query):
42
+ if search_query == '':
43
+ #Get image caption from Bip2 Gradio space
44
+ img_caption = get_caption(image_in)
45
+ else:
46
+ img_caption = search_query
47
+ print(f"Image caption from Blip2 Gradio Space or the search_query is - {img_caption}")
48
  #Searching the vector space
49
  search_result = search_index.similarity_search(img_caption)[0]
50
  #Formatting the search results
 
58
  with gr.Blocks(css = """#label_mid {padding-top: 2px; padding-bottom: 2px;}
59
  #label_results {padding-top: 5px; padding-bottom: 1px;}
60
  """) as demo:
61
+ with gr.Row():
62
+ gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
63
+ <div
64
+ style="
65
+ display: inline-flex;
66
+ align-items: center;
67
+ gap: 0.8rem;
68
+ font-size: 1.75rem;
69
+ "
70
+ >
71
+ <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
72
+ Using Gradio Demos as API</h1><br></div>
73
+ <div><h4 style="font-weight: 500; margin-bottom: 7px; margin-top: 5px;">
74
+ Get BLIP2 captions from <a href="https://langchain.readthedocs.io/en/latest/" target="_blank">Niels space</a> via API call,<br>
75
+ Use LangChain to create vector space with PlaygroundAI prompts</h4><br>
76
+ </div>
77
+ <p style="margin-bottom: 10px; font-size: 90%">
78
+ Do you see the "view api" link located in the footer of this application?
79
+ By clicking on this link, a page will open which provides documentation on the REST API that developers can use to query the Interface function / Block events.<br>
80
+ In this demo, the first step involves making an API call to the BLIP2 Gradio demo to retrieve image captions.
81
+ Next, Langchain is used to create an embedding and vector space for the image prompts and their respective "source" from the PlaygroundAI dataset.
82
+ Finally, a similarity search is performed over the vector space and the top result is returned.
83
+ <center><a href="https://huggingface.co/spaces/ysharma/Blip_PlaygroundAI?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a></center>
84
+ </p></div>""")
85
+ with gr.Column(scale=3):
86
  pass
87
  with gr.Column(scale=1):
88
  label_top = gr.HTML(value= "<center>🖼️Upload an Image for your search📷</center>", elem_id="label_top")
89
  image_in = gr.Image(label="Upoload an Image for search", type='filepath', elem_id="image_in")
90
  label_mid = gr.HTML(value= "<p style='text-align: center; color: red;'>Or</center></p>", elem_id='label_mid')
91
  label_bottom = gr.HTML(value= "<center>🔍Type in your serch query and press Enter</center>", elem_id="label_bottom")
92
+ search_query = gr.Textbox(placeholder="Example: A small cat sitting", label="", elem_id="search_query", value='')
93
  #b1 = gr.Button("Search").style(full_width=False)
94
  label_results = gr.HTML(value= "<p style='text-align: center; color: blue; font-weight: bold;'>Search results from PlaygroundAI</center></p>", elem_id="label_results")
95
  img_search = gr.HTML(label = 'Image search results from PlaygroundAI dataset', elem_id="img_search")
96
  pai_prompt = gr.Textbox(label="Image prompt from PlaygroundAI dataset", elem_id="pai_prompt")
97
+ with gr.Column(scale=3):
98
  pass
99
 
100
+ image_in.change(Image_similarity_search, [image_in, search_query], [pai_prompt, img_search], api_name="PlaygroundAI search" )
101
  #b1.click(Image_similarity_search, image_in, [pai_prompt, img_search] )
102
 
103
  demo.launch(debug=True)