CoolFace
Apppublic

ysharma/Stream_PlaygroundAI_Images

sourceHugging Facemitupdated 4y agoView on Hugging Face
4likes
app.py85 linesDownload Raw Back to root
1import gradio as gr2import ast3import requests4 5#Using Gradio Demos as API - This is Hot!6API_URL_INITIAL = "https://ysharma-playground-ai-exploration.hf.space/run/initial_dataframe"7API_URL_NEXT10 = "https://ysharma-playground-ai-exploration.hf.space/run/next_10_rows"8 9#define inference function10#First: Get initial images for the grid display 11def 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 tags20def process_response(response_dict):21  return [resp[0][:-1] for resp in response_dict["data"]]22 23response_dict = get_initial_images()24initial = process_response(response_dict)25initial_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])26 27#Third: Load more images for the grid28def get_next10_images(response_dict, row_count):29    row_count = int(row_count)30    #print("(1)",type(response_dict))31    #Convert the string to a dictionary32    if isinstance(response_dict, dict) == False :33        response_dict = ast.literal_eval(response_dict)34    response = requests.post(API_URL_NEXT10, json={35              "data": [response_dict, row_count ] #len(initial)-136               }).json()37    row_count+=1038    response_dict = response['data'][0]39    #print("(2)",type(response))40    #print("(3)",type(response['data'][0]))41    next_set  = [resp[0][:-1] for resp in response_dict["data"]]42    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])43    return response_dict, row_count, next_set_images  #response['data'][0]44 45#get_next10_images(response_dict=response_dict, row_count=9)46#position: fixed; top: 0; left: 0; width: 100%; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);47 48#Defining the Blocks layout49with gr.Blocks(css = """#img_search img {width: 100%; height: 100%; object-fit: cover;}""") as demo:50  gr.HTML(value="top of page", elem_id="top",visible=False)51  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">52        <div53        style="54            display: inline-flex;55            align-items: center;56            gap: 0.8rem;57            font-size: 1.75rem;58        "59        >60        <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">61            Using Gradio Demos as API - 2 </h1><br></div>62        <div><h4 style="font-weight: 500; margin-bottom: 7px; margin-top: 5px;">63            Stream <a href="https://github.com/playgroundai/liked_images" target="_blank">PlaygroundAI Images</a> ina beautiful grid</h4><br>64        </div>""")65  with gr.Accordion(label="Details about the working:", open=False, elem_id='accordion'):66    gr.HTML("""67        <p style="margin-bottom: 10px; font-size: 90%"><br>68        ▶️Do you see the "view api" link located in the footer of this application? 69        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>70        ▶️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>71        ▶️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.72        </p></div>""")73 74  with gr.Column(): #(elem_id = "col-container"):75    b1 = gr.Button("Load More Images").style(full_width=False)76    df = gr.Textbox(visible=False,elem_id='dataframe', value=response_dict)77    row_count = gr.Number(visible=False, value=19 )78    img_search = gr.HTML(label = 'Images from PlaygroundAI dataset', elem_id="img_search", 79                         value=initial_imgs ) #initial[:-1] )80      81  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> 82        </p></div>''')83  b1.click(get_next10_images, [df, row_count], [df, row_count, img_search], api_name = "load_playgroundai_images" ) 84 85demo.launch(debug=True)