Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import ast
|
3 |
+
import requests
|
4 |
+
|
5 |
+
#Using Gradio Demos as API - This is Hot!
|
6 |
+
API_URL_INITIAL = "https://ysharma-playground-ai-exploration.hf.space/run/initial_dataframe"
|
7 |
+
API_URL_NEXT10 = "https://ysharma-playground-ai-exploration.hf.space/run/next_10_rows"
|
8 |
+
|
9 |
+
#define inference function
|
10 |
+
#First: Get initial images for the grid display
|
11 |
+
def get_initial_images():
|
12 |
+
response = requests.post(API_URL_INITIAL, json={
|
13 |
+
"data": []
|
14 |
+
}).json()
|
15 |
+
#data = response["data"][0]['data'][0][0][:-1]
|
16 |
+
response_dict = response['data'][0]
|
17 |
+
return response_dict #, [resp[0][:-1] for resp in response["data"][0]["data"]]
|
18 |
+
|
19 |
+
#Second: Process response dictionary to get imges as hyperlinked image tags
|
20 |
+
def process_response(response_dict):
|
21 |
+
return [resp[0][:-1] for resp in response_dict["data"]]
|
22 |
+
|
23 |
+
response_dict = get_initial_images()
|
24 |
+
initial = process_response(response_dict)
|
25 |
+
initial_imgs = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0;">\n' + "\n".join(initial[:-1])
|
26 |
+
#print(initial_imgs )
|
27 |
+
#print("*****************")
|
28 |
+
#response_dict
|
29 |
+
|
30 |
+
#Third: Load more images for the grid
|
31 |
+
def get_next10_images(response_dict, row_count):
|
32 |
+
row_count = int(row_count)
|
33 |
+
#print("(1)",type(response_dict))
|
34 |
+
#Convert the string to a dictionary
|
35 |
+
response_dict = ast.literal_eval(response_dict)
|
36 |
+
response = requests.post(API_URL_NEXT10, json={
|
37 |
+
"data": [response_dict, row_count ] #len(initial)-1
|
38 |
+
}).json()
|
39 |
+
row_count+=10
|
40 |
+
#print("(2)",type(response))
|
41 |
+
#print("(3)",type(response['data'][0]))
|
42 |
+
next_set = [resp[0][:-1] for resp in response_dict["data"]]
|
43 |
+
next_set_images = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0;">\n' + "\n".join(next_set[:-1])
|
44 |
+
return response['data'][0], row_count, next_set_images
|
45 |
+
|
46 |
+
get_next10_images(response_dict=response_dict, row_count=9)
|
47 |
+
|
48 |
+
#def process_response(response_dict):
|
49 |
+
# return [resp[0][:-1] for resp in response_dict["data"]]
|
50 |
+
|
51 |
+
#response_dict = get_initial_images()
|
52 |
+
#initial = process_response(response_dict)
|
53 |
+
#initial_imgs = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0;">\n' + "\n".join(initial[:-1])
|
54 |
+
#print(initial_imgs )
|
55 |
+
#print("*****************")
|
56 |
+
#response_dict
|
57 |
+
|
58 |
+
#Defining the Blocks layout
|
59 |
+
with gr.Blocks(css = """#img_search img {width: 100%; height: 100%; object-fit: cover;}""") as demo:
|
60 |
+
gr.HTML(value="top of page", elem_id="top",visible=False)
|
61 |
+
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
62 |
+
<div
|
63 |
+
style="
|
64 |
+
display: inline-flex;
|
65 |
+
align-items: center;
|
66 |
+
gap: 0.8rem;
|
67 |
+
font-size: 1.75rem;
|
68 |
+
"
|
69 |
+
>
|
70 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
71 |
+
Using Gradio Demos as API - 2 </h1><br></div>
|
72 |
+
<div><h4 style="font-weight: 500; margin-bottom: 7px; margin-top: 5px;">
|
73 |
+
Stream <a href="" target="_blank">PlaygroundAI Images</a> ina beautiful grid</h4><br>
|
74 |
+
</div>""")
|
75 |
+
with gr.Accordion(label="Details about the working:", open=False, elem_id='accordion'):
|
76 |
+
gr.HTML("""
|
77 |
+
<p style="margin-bottom: 10px; font-size: 90%"><br>
|
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, xyzzzz
|
81 |
+
</p></div>""")
|
82 |
+
|
83 |
+
with gr.Column(): #(elem_id = "col-container"):
|
84 |
+
b1 = gr.Button("Load More Images").style(full_width=False)
|
85 |
+
df = gr.Textbox(visible=False,elem_id='dataframe', value=response_dict)
|
86 |
+
row_count = gr.Number(visible=False, value=19 )
|
87 |
+
img_search = gr.HTML(label = 'Images from PlaygroundAI dataset', elem_id="img_search",
|
88 |
+
value=initial_imgs ) #initial[:-1] )
|
89 |
+
|
90 |
+
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/Blip_PlaygroundAI?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a></center>
|
91 |
+
</p></div>''')
|
92 |
+
gr.HTML(value='<a href="#top">go ot top</a>', elem_id="bottom",)
|
93 |
+
#image_in.change(Image_similarity_search, [image_in, search_query], [img_search], api_name="PlaygroundAI_image_search" )
|
94 |
+
#search_query.submit(Image_similarity_search, [image_in, search_query], [img_search], api_name='PlaygroundAI_text_search' )
|
95 |
+
b1.click(get_next10_images, [df, row_count], [df, row_count, img_search] )
|
96 |
+
|
97 |
+
demo.launch(debug=True)
|