yonatanbitton commited on
Commit
7930e1d
·
1 Parent(s): 4621210

first commit

Browse files
.idea/.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
.idea/inspectionProfiles/Project_Default.xml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
5
+ <option name="ignoredPackages">
6
+ <value>
7
+ <list size="3">
8
+ <item index="0" class="java.lang.String" itemvalue="matplotlib" />
9
+ <item index="1" class="java.lang.String" itemvalue="CLIP" />
10
+ <item index="2" class="java.lang.String" itemvalue="transformers" />
11
+ </list>
12
+ </value>
13
+ </option>
14
+ </inspection_tool>
15
+ </profile>
16
+ </component>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (venv) (2)" project-jdk-type="Python SDK" />
4
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/whoops-dataset-viewer.iml" filepath="$PROJECT_DIR$/.idea/whoops-dataset-viewer.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
.idea/whoops-dataset-viewer.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="Python 3.9 (venv) (2)" jdkType="Python SDK" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from datasets import load_dataset
4
+
5
+ whoops = load_dataset("nlphuji/whoops")['test']
6
+ BUCKET_PATH = 'https://wmtis.s3.eu-west-1.amazonaws.com/wmtis_images'
7
+
8
+ df = whoops.to_pandas()
9
+ def get_image_url(img_id):
10
+ return f"{BUCKET_PATH}/{img_id}.png"
11
+
12
+ df['image_url'] = df['image_id'].apply(lambda x: get_image_url(x))
13
+ df['image_url'] = df['image_url'].apply(lambda x: '<a href= "' + str(x) + '" target="_blank"> <img src= "' + str(
14
+ x) + '"/> </a>')
15
+
16
+ def dumps(x, c):
17
+ if c in ['crowd_captions', 'crowd_underspecified_captions']:
18
+ return json.dumps(list(x))
19
+ elif c == 'question_answering_pairs':
20
+ return json.dumps([list(xi) for xi in x])
21
+ return json.dumps(x)
22
+
23
+ for c in ['designer_explanation', 'selected_caption', 'crowd_captions', 'crowd_underspecified_captions',
24
+ 'question_answering_pairs', 'commonsense_category', 'image_id', 'image_designer']:
25
+ print(c)
26
+ df[c] = df[c].apply(lambda x: dumps(x, c))
27
+
28
+ df = df[['image_url', 'designer_explanation', 'selected_caption', 'crowd_captions', 'crowd_underspecified_captions',
29
+ 'question_answering_pairs', 'commonsense_category', 'image_id', 'image_designer']]
30
+
31
+ LINES_NUMBER = 20
32
+
33
+ def display_df():
34
+ df_images = df.head(LINES_NUMBER)
35
+ return df_images
36
+
37
+ def display_next(dataframe, end):
38
+ start = (end or dataframe.index[-1]) + 1
39
+ end = start + (LINES_NUMBER-1)
40
+ df_images = df.loc[start:end]
41
+ return df_images, end
42
+
43
+ # Gradio Blocks
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("<h1><center>WHOOPS! Dataset Viewer</center></h1>")
46
+
47
+ with gr.Row():
48
+ num_end = gr.Number(visible=False)
49
+ b1 = gr.Button("Get Initial dataframe")
50
+ b2 = gr.Button("Next Rows")
51
+
52
+ with gr.Row():
53
+ out_dataframe = gr.Dataframe(wrap=True, max_rows=LINES_NUMBER, overflow_row_behaviour="paginate",
54
+ datatype=["markdown", "markdown", "str", "str", "str", "str", "str", "str","str","str"],
55
+ interactive=False)
56
+
57
+ b1.click(fn=display_df, outputs=out_dataframe, api_name="initial_dataframe")
58
+ b2.click(fn=display_next, inputs=[out_dataframe, num_end], outputs=[out_dataframe, num_end],
59
+ api_name="next_rows")
60
+
61
+ demo.launch(debug=True, show_error=True)
app2.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #
10
+ # # define inference function
11
+ # # First: Get initial images for the grid display
12
+ # def get_initial_images():
13
+ # response = requests.post(API_URL_INITIAL, json={
14
+ # "data": []
15
+ # }).json()
16
+ # # data = response["data"][0]['data'][0][0][:-1]
17
+ # response_dict = response['data'][0]
18
+ # return response_dict # , [resp[0][:-1] for resp in response["data"][0]["data"]]
19
+ #
20
+ #
21
+ # # Second: Process response dictionary to get imges as hyperlinked image tags
22
+ # def process_response(response_dict):
23
+ # return [resp[0][:-1] for resp in response_dict["data"]]
24
+ #
25
+ #
26
+ # response_dict = get_initial_images()
27
+ # initial = process_response(response_dict)
28
+ # 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(
29
+ # initial[:-1])
30
+ #
31
+ #
32
+ # # Third: Load more images for the grid
33
+ # def get_next10_images(response_dict, row_count):
34
+ # row_count = int(row_count)
35
+ # # print("(1)",type(response_dict))
36
+ # # Convert the string to a dictionary
37
+ # if isinstance(response_dict, dict) == False:
38
+ # response_dict = ast.literal_eval(response_dict)
39
+ # response = requests.post(API_URL_NEXT10, json={
40
+ # "data": [response_dict, row_count] # len(initial)-1
41
+ # }).json()
42
+ # row_count += 10
43
+ # response_dict = response['data'][0]
44
+ # # print("(2)",type(response))
45
+ # # print("(3)",type(response['data'][0]))
46
+ # next_set = [resp[0][:-1] for resp in response_dict["data"]]
47
+ # 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(
48
+ # next_set[:-1])
49
+ # return response_dict, row_count, next_set_images # response['data'][0]
50
+ #
51
+ #
52
+ # # get_next10_images(response_dict=response_dict, row_count=9)
53
+ # # position: fixed; top: 0; left: 0; width: 100%; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
54
+ #
55
+ # # Defining the Blocks layout
56
+ # with gr.Blocks(css="""#img_search img {width: 100%; height: 100%; object-fit: cover;}""") as demo:
57
+ # gr.HTML(value="top of page", elem_id="top", visible=False)
58
+ # gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
59
+ # <div
60
+ # style="
61
+ # display: inline-flex;
62
+ # align-items: center;
63
+ # gap: 0.8rem;
64
+ # font-size: 1.75rem;
65
+ # "
66
+ # >
67
+ # <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
68
+ # Using Gradio Demos as API - 2 </h1><br></div>
69
+ # <div><h4 style="font-weight: 500; margin-bottom: 7px; margin-top: 5px;">
70
+ # Stream <a href="https://github.com/playgroundai/liked_images" target="_blank">PlaygroundAI Images</a> ina beautiful grid</h4><br>
71
+ # </div>""")
72
+ # # with gr.Accordion(label="Details about the working:", open=False, elem_id='accordion'):
73
+ # # gr.HTML("""
74
+ # # <p style="margin-bottom: 10px; font-size: 90%"><br>
75
+ # # ▶️Do you see the "view api" link located in the footer of this application?
76
+ # # 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>
77
+ # # ▶️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>
78
+ # # ▶️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.
79
+ # # </p></div>""")
80
+ #
81
+ # with gr.Column(): # (elem_id = "col-container"):
82
+ # b1 = gr.Button("Load More Images").style(full_width=False)
83
+ # df = gr.Textbox(visible=False, elem_id='dataframe', value=response_dict)
84
+ # row_count = gr.Number(visible=False, value=19)
85
+ # img_search = gr.HTML(label='Images from PlaygroundAI dataset', elem_id="img_search",
86
+ # value=initial_imgs) # initial[:-1] )
87
+ #
88
+ # 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>
89
+ # # </p></div>''')
90
+ # b1.click(get_next10_images, [df, row_count], [df, row_count, img_search], api_name="load_playgroundai_images")
91
+ #
92
+ # demo.launch(debug=True)
93
+
94
+ import pandas as pd
95
+ import gradio as gr
96
+
97
+ df = pd.read_csv("/Users/yonatanbitton/Downloads/whoops_dataset.csv")
98
+ df['image_url'] = df['image_url'].apply(lambda x: '<a href= "' + str(x) + '" target="_blank"> <img src= "' + str(
99
+ x) + '"/> </a>')
100
+ df['designer_explanation'] = df['designer_explanation'].apply(lambda x: str(x))
101
+ df['selected_caption'] = df['selected_caption'].apply(lambda x: str(x))
102
+ df['crowd_captions'] = df['crowd_captions'].apply(lambda x: str(x))
103
+ df['crowd_underspecified_captions'] = df['crowd_underspecified_captions'].apply(lambda x: str(x))
104
+ df['question_answering_pairs'] = df['question_answering_pairs'].apply(lambda x: str(x))
105
+ df['commonsense_category'] = df['commonsense_category'].apply(lambda x: str(x))
106
+ df['image_id'] = df['image_id'].apply(lambda x: str(x))
107
+ df['image_designer'] = df['image_designer'].apply(lambda x: str(x))
108
+ df = df[['image_url', 'designer_explanation', 'selected_caption', 'crowd_captions', 'crowd_underspecified_captions',
109
+ 'question_answering_pairs', 'commonsense_category', 'image_id', 'image_designer']]
110
+
111
+ LINES_NUMBER = 20
112
+
113
+ def display_df():
114
+ df_images = df.head(LINES_NUMBER)
115
+ return df_images
116
+
117
+
118
+ def display_next10(dataframe, end):
119
+ start = (end or dataframe.index[-1]) + 1
120
+ end = start + (LINES_NUMBER-1)
121
+ df_images = df.loc[start:end]
122
+ return df_images, end
123
+
124
+
125
+ # Gradio Blocks
126
+ with gr.Blocks() as demo:
127
+ gr.Markdown("<h1><center>WHOOPS! Dataset Viewer</center></h1>")
128
+
129
+ with gr.Row():
130
+ num_end = gr.Number(visible=False)
131
+ b1 = gr.Button("Get Initial dataframe")
132
+ b2 = gr.Button("Next 10 Rows")
133
+
134
+ with gr.Row():
135
+ out_dataframe = gr.Dataframe(wrap=True, max_rows=LINES_NUMBER, overflow_row_behaviour="paginate",
136
+ datatype=["markdown", "markdown", "str", "str", "str", "str", "str", "str","str","str"],
137
+ interactive=False)
138
+
139
+ b1.click(fn=display_df, outputs=out_dataframe, api_name="initial_dataframe")
140
+ b2.click(fn=display_next10, inputs=[out_dataframe, num_end], outputs=[out_dataframe, num_end],
141
+ api_name="next_10_rows")
142
+
143
+ demo.launch(debug=True, show_error=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ datasets
2
+ gradio==3.0.5