bradley6597
commited on
Commit
•
df4c3d4
1
Parent(s):
fdf4fd8
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,34 @@
|
|
1 |
import functions as funky
|
2 |
import pandas as pd
|
3 |
import gradio as gr
|
4 |
-
import numpy as np
|
5 |
-
from datetime import datetime
|
6 |
import os
|
7 |
-
|
8 |
from datasets import load_dataset
|
9 |
from huggingface_hub import login
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
login(token = os.environ['HUB_TOKEN'])
|
12 |
-
|
13 |
-
df = pd.DataFrame(dataset['train'])
|
14 |
|
15 |
logger = gr.HuggingFaceDatasetSaver(os.environ['HUB_TOKEN'], dataset_name='illustration_gdrive_logging_main', organization=None, private=True)
|
16 |
logger.setup([gr.Text(label="clicked_url"), gr.Text(label="seach_term"), gr.Text(label = 'sessionhash'), gr.Text(label = 'datetime')], './flagged_data_points')
|
17 |
|
18 |
-
|
19 |
logging_js = '''
|
20 |
function magicFunc(x){
|
21 |
let script = document.createElement('script');
|
22 |
-
script.innerHTML = "async function magicFunc(x){let z = document.getElementById('search_term').getElementsByTagName('textarea')[0].value;
|
23 |
document.head.appendChild(script);
|
24 |
}
|
25 |
'''
|
26 |
|
|
|
|
|
27 |
|
28 |
-
ill_links = df
|
29 |
ill_links = ill_links[ill_links['Description'] != 'Moved'].copy()
|
30 |
ill_links['code'] = ill_links['link'].str.replace("https://drive.google.com/file/d/", "", regex = False)
|
31 |
ill_links['code'] = ill_links['code'].str.replace("/view?usp=drivesdk", "", regex = False)
|
@@ -36,7 +39,6 @@ ill_links['filename'] = ill_links['file'].str.replace(".*\\/", "", regex = True)
|
|
36 |
ill_links['shared_drive'] = ill_links['file'].str.replace("/content/drive/Shareddrives/", "", regex = False)
|
37 |
ill_links['shared_drive'] = ill_links['shared_drive'].str.replace("(.*?)\\/.*", "\\1", regex = True)
|
38 |
ill_links['Description'] = ill_links['Description'].str.replace("No Description", "", regex = False)
|
39 |
-
ill_links['image_code'].iloc[0]
|
40 |
|
41 |
ill_links_title = ill_links.copy()
|
42 |
|
@@ -124,7 +126,7 @@ def search_index(search_text, sd, ks, sort_by, max_results, user_num, search_tit
|
|
124 |
final_df = final_df.fillna('')
|
125 |
else:
|
126 |
final_df = pd.DataFrame(['<h3>No Results Found :(</h3>'])
|
127 |
-
|
128 |
if final_df.shape[0] == 0 :
|
129 |
final_df = pd.DataFrame(['<h3>No Results Found :(</h3>'])
|
130 |
|
@@ -132,11 +134,10 @@ def search_index(search_text, sd, ks, sort_by, max_results, user_num, search_tit
|
|
132 |
final_df.to_html(escape = False, render_links = True, index = False, header = False) +
|
133 |
'</center>')
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
url = ''
|
138 |
session_id = getattr(request.cookies, 'access-token')
|
139 |
-
logger.flag([
|
140 |
|
141 |
|
142 |
with gr.Blocks(css="style.css") as app:
|
@@ -158,56 +159,43 @@ with gr.Blocks(css="style.css") as app:
|
|
158 |
search_button = gr.Button(value="Search!")
|
159 |
with gr.Row():
|
160 |
output_df = gr.HTML()
|
161 |
-
|
162 |
search_button.click(search_index, inputs=[search_prompt, shared_drive, key_stage, sort_by, max_return, user_num, title_search], outputs=output_df)
|
163 |
-
search_prompt.submit(search_index, inputs=[search_prompt, shared_drive, key_stage, sort_by, max_return, user_num, title_search], outputs=output_df)
|
164 |
-
search_button.click(search_logging, inputs=[search_prompt
|
165 |
-
search_prompt.submit(search_logging, inputs=[search_prompt
|
|
|
166 |
app.load(_js = logging_js)
|
167 |
|
168 |
app.auth = (same_auth)
|
169 |
app.auth_message = ''
|
170 |
|
171 |
-
app.launch(debug=True,
|
172 |
-
share=False,
|
173 |
-
height=768,
|
174 |
-
auth=same_auth
|
175 |
-
)
|
176 |
-
|
177 |
-
# app.close()
|
178 |
-
|
179 |
-
# from fastapi import FastAPI, Request
|
180 |
-
# import uvicorn
|
181 |
-
# from starlette.middleware.sessions import SessionMiddleware
|
182 |
-
# fapi = FastAPI()
|
183 |
-
|
184 |
-
# fapi.add_middleware(SessionMiddleware, secret_key=os.environ['session_key'])
|
185 |
-
|
186 |
-
# @fapi.middleware("http")
|
187 |
-
# async def add_session_hash(request: Request, call_next):
|
188 |
-
# response = await call_next(request)
|
189 |
-
# session = request.cookies.get('session')
|
190 |
-
# if session:
|
191 |
-
# response.set_cookie(key='session', value=request.cookies.get('session'), httponly=True)
|
192 |
-
# return response
|
193 |
-
|
194 |
-
# # custom get request handler with params to flag clicks
|
195 |
-
# @ fapi.get("/track")
|
196 |
-
# async def track(url: str, q: str, request: Request):
|
197 |
-
|
198 |
-
# print(request)
|
199 |
-
# if q is None:
|
200 |
-
# q = ''
|
201 |
-
|
202 |
-
# logger.flag([url, q, request.cookies['access-token'], str(datetime.now())])
|
203 |
-
# return {"message": "ok"}
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
-
# logger.flag(['test', 'test', 'test', str(datetime.now())])
|
208 |
|
209 |
-
#
|
210 |
-
|
211 |
-
#
|
212 |
-
|
213 |
-
|
|
|
|
1 |
import functions as funky
|
2 |
import pandas as pd
|
3 |
import gradio as gr
|
|
|
|
|
4 |
import os
|
|
|
5 |
from datasets import load_dataset
|
6 |
from huggingface_hub import login
|
7 |
+
import numpy as np
|
8 |
+
from fastapi import FastAPI, Request
|
9 |
+
import uvicorn
|
10 |
+
from starlette.middleware.sessions import SessionMiddleware
|
11 |
+
import fastapi
|
12 |
+
from datetime import datetime
|
13 |
|
14 |
login(token = os.environ['HUB_TOKEN'])
|
15 |
+
|
|
|
16 |
|
17 |
logger = gr.HuggingFaceDatasetSaver(os.environ['HUB_TOKEN'], dataset_name='illustration_gdrive_logging_main', organization=None, private=True)
|
18 |
logger.setup([gr.Text(label="clicked_url"), gr.Text(label="seach_term"), gr.Text(label = 'sessionhash'), gr.Text(label = 'datetime')], './flagged_data_points')
|
19 |
|
|
|
20 |
logging_js = '''
|
21 |
function magicFunc(x){
|
22 |
let script = document.createElement('script');
|
23 |
+
script.innerHTML = "async function magicFunc(x){let z = document.getElementById('search_term').getElementsByTagName('textarea')[0].value; await fetch('/track?url=' + x + '&q=' + z)}";
|
24 |
document.head.appendChild(script);
|
25 |
}
|
26 |
'''
|
27 |
|
28 |
+
dataset = load_dataset("bradley6597/illustration-test")
|
29 |
+
df = pd.DataFrame(dataset['train']).drop_duplicates()
|
30 |
|
31 |
+
ill_links = df.copy()
|
32 |
ill_links = ill_links[ill_links['Description'] != 'Moved'].copy()
|
33 |
ill_links['code'] = ill_links['link'].str.replace("https://drive.google.com/file/d/", "", regex = False)
|
34 |
ill_links['code'] = ill_links['code'].str.replace("/view?usp=drivesdk", "", regex = False)
|
|
|
39 |
ill_links['shared_drive'] = ill_links['file'].str.replace("/content/drive/Shareddrives/", "", regex = False)
|
40 |
ill_links['shared_drive'] = ill_links['shared_drive'].str.replace("(.*?)\\/.*", "\\1", regex = True)
|
41 |
ill_links['Description'] = ill_links['Description'].str.replace("No Description", "", regex = False)
|
|
|
42 |
|
43 |
ill_links_title = ill_links.copy()
|
44 |
|
|
|
126 |
final_df = final_df.fillna('')
|
127 |
else:
|
128 |
final_df = pd.DataFrame(['<h3>No Results Found :(</h3>'])
|
129 |
+
|
130 |
if final_df.shape[0] == 0 :
|
131 |
final_df = pd.DataFrame(['<h3>No Results Found :(</h3>'])
|
132 |
|
|
|
134 |
final_df.to_html(escape = False, render_links = True, index = False, header = False) +
|
135 |
'</center>')
|
136 |
|
137 |
+
|
138 |
+
def search_logging(x: str, request: gr.Request):
|
|
|
139 |
session_id = getattr(request.cookies, 'access-token')
|
140 |
+
logger.flag(['', x, session_id, str(datetime.now())])
|
141 |
|
142 |
|
143 |
with gr.Blocks(css="style.css") as app:
|
|
|
159 |
search_button = gr.Button(value="Search!")
|
160 |
with gr.Row():
|
161 |
output_df = gr.HTML()
|
|
|
162 |
search_button.click(search_index, inputs=[search_prompt, shared_drive, key_stage, sort_by, max_return, user_num, title_search], outputs=output_df)
|
163 |
+
search_prompt.submit(search_index, inputs=[search_prompt, shared_drive, key_stage, sort_by, max_return, user_num, title_search], outputs=output_df)
|
164 |
+
search_button.click(search_logging, inputs=[search_prompt], outputs=None)
|
165 |
+
search_prompt.submit(search_logging, inputs=[search_prompt], outputs=None)
|
166 |
+
|
167 |
app.load(_js = logging_js)
|
168 |
|
169 |
app.auth = (same_auth)
|
170 |
app.auth_message = ''
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
+
fapi = FastAPI()
|
174 |
+
|
175 |
+
fapi.add_middleware(SessionMiddleware, secret_key=os.environ['session_key'])
|
176 |
+
|
177 |
+
@fapi.middleware("http")
|
178 |
+
async def add_session_hash(request: Request, call_next):
|
179 |
+
response = await call_next(request)
|
180 |
+
session = request.cookies.get('session')
|
181 |
+
if session:
|
182 |
+
response.set_cookie(key='session', value=request.cookies.get('session'), httponly=True)
|
183 |
+
return response
|
184 |
|
185 |
+
# custom get request handler with params to flag clicks
|
186 |
+
@ fapi.get("/track")
|
187 |
+
async def track(url: str, q: str, request: Request):
|
188 |
+
|
189 |
+
if q is None:
|
190 |
+
q = ''
|
191 |
+
|
192 |
+
logger.flag([url, q, request.cookies['access-token'], str(datetime.now())])
|
193 |
+
return {"message": "ok"}
|
194 |
|
|
|
195 |
|
196 |
+
# mount Gradio app to FastAPI app
|
197 |
+
app2 = gr.mount_gradio_app(fapi, app, path="/")
|
198 |
+
# serve the app
|
199 |
+
if __name__ == "__main__":
|
200 |
+
uvicorn.run(app2, host="0.0.0.0", port=7860)
|
201 |
+
|