bradley6597
commited on
Commit
·
a1620bf
1
Parent(s):
d9a0fa3
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,18 @@
|
|
1 |
-
|
2 |
-
|
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;
|
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 =
|
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,6 +23,7 @@ ill_links['filename'] = ill_links['file'].str.replace(".*\\/", "", regex = True)
|
|
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,7 +111,7 @@ def search_index(search_text, sd, ks, sort_by, max_results, user_num, search_tit
|
|
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,10 +119,11 @@ def search_index(search_text, sd, ks, sort_by, max_results, user_num, search_tit
|
|
134 |
final_df.to_html(escape = False, render_links = True, index = False, header = False) +
|
135 |
'</center>')
|
136 |
|
137 |
-
|
138 |
-
|
|
|
139 |
session_id = getattr(request.cookies, 'access-token')
|
140 |
-
logger.flag([
|
141 |
|
142 |
|
143 |
with gr.Blocks(css="style.css") as app:
|
@@ -159,42 +145,18 @@ 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 |
app.load(_js = logging_js)
|
167 |
|
168 |
-
app.auth = (same_auth)
|
169 |
-
app.auth_message = ''
|
170 |
-
|
171 |
-
|
172 |
-
fapi = FastAPI()
|
173 |
-
|
174 |
-
fapi.add_middleware(SessionMiddleware, secret_key=os.environ['session_key'])
|
175 |
-
|
176 |
-
@fapi.middleware("http")
|
177 |
-
async def add_session_hash(request: Request, call_next):
|
178 |
-
response = await call_next(request)
|
179 |
-
session = request.cookies.get('session')
|
180 |
-
if session:
|
181 |
-
response.set_cookie(key='session', value=request.cookies.get('session'), httponly=True)
|
182 |
-
return response
|
183 |
-
|
184 |
-
# custom get request handler with params to flag clicks
|
185 |
-
@ fapi.get("/track")
|
186 |
-
async def track(url: str, q: str, request: Request):
|
187 |
-
|
188 |
-
if q is None:
|
189 |
-
q = ''
|
190 |
-
|
191 |
-
logger.flag([url, q, request.cookies['access-token'], str(datetime.now())])
|
192 |
-
return {"message": "ok"}
|
193 |
-
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
1 |
+
|
2 |
+
logger = gr.HuggingFaceDatasetSaver(os.environ['HUB_TOKEN'], dataset_name='illustration_gdrive_logging', organization=None, private=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
logger.setup([gr.Text(label="clicked_url"), gr.Text(label="seach_term"), gr.Text(label = 'sessionhash'), gr.Text(label = 'datetime')], './flagged_data_points')
|
4 |
|
5 |
+
|
6 |
logging_js = '''
|
7 |
function magicFunc(x){
|
8 |
let script = document.createElement('script');
|
9 |
+
script.innerHTML = "async function magicFunc(x){let z = document.getElementById('search_term').getElementsByTagName('textarea')[0].value; let data = {data: [x, z]}; var x = fetch('/api/track', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)})}";
|
10 |
document.head.appendChild(script);
|
11 |
}
|
12 |
'''
|
13 |
|
|
|
|
|
14 |
|
15 |
+
ill_links = pd.read_csv("./data/data.csv").drop_duplicates()
|
16 |
ill_links = ill_links[ill_links['Description'] != 'Moved'].copy()
|
17 |
ill_links['code'] = ill_links['link'].str.replace("https://drive.google.com/file/d/", "", regex = False)
|
18 |
ill_links['code'] = ill_links['code'].str.replace("/view?usp=drivesdk", "", regex = False)
|
|
|
23 |
ill_links['shared_drive'] = ill_links['file'].str.replace("/content/drive/Shareddrives/", "", regex = False)
|
24 |
ill_links['shared_drive'] = ill_links['shared_drive'].str.replace("(.*?)\\/.*", "\\1", regex = True)
|
25 |
ill_links['Description'] = ill_links['Description'].str.replace("No Description", "", regex = False)
|
26 |
+
ill_links['image_code'].iloc[0]
|
27 |
|
28 |
ill_links_title = ill_links.copy()
|
29 |
|
|
|
111 |
final_df = final_df.fillna('')
|
112 |
else:
|
113 |
final_df = pd.DataFrame(['<h3>No Results Found :(</h3>'])
|
114 |
+
|
115 |
if final_df.shape[0] == 0 :
|
116 |
final_df = pd.DataFrame(['<h3>No Results Found :(</h3>'])
|
117 |
|
|
|
119 |
final_df.to_html(escape = False, render_links = True, index = False, header = False) +
|
120 |
'</center>')
|
121 |
|
122 |
+
def search_logging(url: str, q: str, request: gr.Request):
|
123 |
+
if url == q:
|
124 |
+
url = ''
|
125 |
session_id = getattr(request.cookies, 'access-token')
|
126 |
+
logger.flag([url, q, session_id, str(datetime.now())])
|
127 |
|
128 |
|
129 |
with gr.Blocks(css="style.css") as app:
|
|
|
145 |
search_button = gr.Button(value="Search!")
|
146 |
with gr.Row():
|
147 |
output_df = gr.HTML()
|
148 |
+
|
149 |
search_button.click(search_index, inputs=[search_prompt, shared_drive, key_stage, sort_by, max_return, user_num, title_search], outputs=output_df)
|
150 |
+
search_prompt.submit(search_index, inputs=[search_prompt, shared_drive, key_stage, sort_by, max_return, user_num, title_search], outputs=output_df)
|
151 |
+
search_button.click(search_logging, inputs=[search_prompt, search_prompt], outputs=None, api_name='track')
|
152 |
+
search_prompt.submit(search_logging, inputs=[search_prompt, search_prompt], outputs=None, api_name='track')
|
153 |
app.load(_js = logging_js)
|
154 |
|
155 |
+
# app.auth = (same_auth)
|
156 |
+
# app.auth_message = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
+
app.launch(debug=True,
|
159 |
+
share=False,
|
160 |
+
height=768,
|
161 |
+
auth=(same_auth)
|
162 |
+
)
|
|