Create play.py
Browse files
play.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
import requests
|
3 |
+
|
4 |
+
#Using Gradio Demos as API - This is Hot!
|
5 |
+
API_URL_INITIAL = "https://ysharma-playground-ai-exploration.hf.space/run/initial_dataframe"
|
6 |
+
API_URL_NEXT10 = "https://ysharma-playground-ai-exploration.hf.space/run/next_10_rows"
|
7 |
+
|
8 |
+
#define inference function
|
9 |
+
#First: Get initial images for the grid display
|
10 |
+
def get_initial_images():
|
11 |
+
response = requests.post(API_URL_INITIAL, json={
|
12 |
+
"data": []
|
13 |
+
}).json()
|
14 |
+
#data = response["data"][0]['data'][0][0][:-1]
|
15 |
+
response_dict = response['data'][0]
|
16 |
+
return response_dict #, [resp[0][:-1] for resp in response["data"][0]["data"]]
|
17 |
+
|
18 |
+
#Second: Process response dictionary to get imges as hyperlinked image tags
|
19 |
+
def process_response(response_dict):
|
20 |
+
return [resp[0][:-1] for resp in response_dict["data"]]
|
21 |
+
|
22 |
+
response_dict = get_initial_images()
|
23 |
+
initial = process_response(response_dict)
|
24 |
+
initial_imgs = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);">\n' + "\n".join(initial[:-1])
|
25 |
+
|
26 |
+
#Third: Load more images for the grid
|
27 |
+
def get_next10_images(response_dict, row_count):
|
28 |
+
row_count = int(row_count)
|
29 |
+
#print("(1)",type(response_dict))
|
30 |
+
#Convert the string to a dictionary
|
31 |
+
if isinstance(response_dict, dict) == False :
|
32 |
+
response_dict = ast.literal_eval(response_dict)
|
33 |
+
response = requests.post(API_URL_NEXT10, json={
|
34 |
+
"data": [response_dict, row_count ] #len(initial)-1
|
35 |
+
}).json()
|
36 |
+
row_count+=10
|
37 |
+
response_dict = response['data'][0]
|
38 |
+
#print("(2)",type(response))
|
39 |
+
#print("(3)",type(response['data'][0]))
|
40 |
+
next_set = [resp[0][:-1] for resp in response_dict["data"]]
|
41 |
+
next_set_images = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2); ">\n' + "\n".join(next_set[:-1])
|
42 |
+
return response_dict, row_count, next_set_images #response['data'][0]
|
43 |
+
|
44 |
+
#get_next10_images(response_dict=response_dict, row_count=9)
|
45 |
+
#position: fixed; top: 0; left: 0; width: 100%; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
|
46 |
+
|
47 |
+
#Defining the Blocks layout
|
48 |
+
with gr.Blocks(css = """#img_search img {width: 100%; height: 100%; object-fit: cover;}""") as demo:
|
49 |
+
gr.HTML(value="top of page", elem_id="top",visible=False)
|
50 |
+
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
51 |
+
<div
|
52 |
+
style="
|
53 |
+
display: inline-flex;
|
54 |
+
align-items: center;
|
55 |
+
gap: 0.8rem;
|
56 |
+
font-size: 1.75rem;
|
57 |
+
"
|
58 |
+
>
|
59 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
60 |
+
Using Gradio Demos as API - 2 </h1><br></div>
|
61 |
+
<div><h4 style="font-weight: 500; margin-bottom: 7px; margin-top: 5px;">
|
62 |
+
Stream <a href="https://github.com/playgroundai/liked_images" target="_blank">PlaygroundAI Images</a> ina beautiful grid</h4><br>
|
63 |
+
</div>""")
|
64 |
+
with gr.Accordion(label="Details about the working:", open=False, elem_id='accordion'):
|
65 |
+
gr.HTML("""
|
66 |
+
<p style="margin-bottom: 10px; font-size: 90%"><br>
|
67 |
+
▶️Do you see the "view api" link located in the footer of this application?
|
68 |
+
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>
|
69 |
+
▶️In this demo, I am making such an API request to the <a href="https://huggingface.co/spaces/ysharma/Playground_AI_Exploration" target="_blank">Playground_AI_Exploration</a> Space.<br>
|
70 |
+
▶️I am exposing an API endpoint of this Gradio app as well. This can easily be done by one line of code, just set the api_name parameter of the event listener.
|
71 |
+
</p></div>""")
|
72 |
+
|
73 |
+
with gr.Column(): #(elem_id = "col-container"):
|
74 |
+
b1 = gr.Button("Load More Images").style(full_width=False)
|
75 |
+
df = gr.Textbox(visible=False,elem_id='dataframe', value=response_dict)
|
76 |
+
row_count = gr.Number(visible=False, value=19 )
|
77 |
+
img_search = gr.HTML(label = 'Images from PlaygroundAI dataset', elem_id="img_search",
|
78 |
+
value=initial_imgs ) #initial[:-1] )
|
79 |
+
|
80 |
+
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/Stream_PlaygroundAI_Images?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a></center>
|
81 |
+
</p></div>''')
|
82 |
+
b1.click(get_next10_images, [df, row_count], [df, row_count, img_search], api_name = "load_playgroundai_images" )
|