Spaces:
Sleeping
Sleeping
cheesexuebao
commited on
Commit
•
b6be546
1
Parent(s):
afc3372
feat: 加入登录页面和针对HFSpace存储设计的日志下载功能。
Browse files
app.py
CHANGED
@@ -1,10 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
from Prediction import *
|
5 |
import os
|
6 |
from datetime import datetime
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
examples = []
|
10 |
if os.path.exists("assets/examples.txt"):
|
@@ -27,8 +38,7 @@ model = model.to(device)
|
|
27 |
|
28 |
def single_sentence(sentence):
|
29 |
predictions = predict_single(sentence, tokenizer, model, device)
|
30 |
-
predictions
|
31 |
-
return list(zip(LABEL_COLUMNS, predictions))
|
32 |
|
33 |
def csv_process(csv_file, attr="content"):
|
34 |
current_time = datetime.now()
|
@@ -43,6 +53,72 @@ def csv_process(csv_file, attr="content"):
|
|
43 |
outputs.append(output_path)
|
44 |
return outputs
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
my_theme = gr.Theme.from_hub("JohnSmith9982/small_and_pretty")
|
48 |
with gr.Blocks(theme=my_theme, title='Brand_Tone_of_Voice_demo') as demo:
|
@@ -63,63 +139,115 @@ with gr.Blocks(theme=my_theme, title='Brand_Tone_of_Voice_demo') as demo:
|
|
63 |
</div>
|
64 |
""")
|
65 |
|
66 |
-
with gr.
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
gr.
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
...
|
73 |
-
"""
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
gr.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
# Paper Name
|
114 |
-
|
115 |
-
# Authors
|
116 |
-
|
117 |
-
+ First author
|
118 |
-
+ Corresponding author
|
119 |
-
|
120 |
-
# Detailed Information
|
121 |
-
|
122 |
-
...
|
123 |
-
"""
|
124 |
-
)
|
125 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
3 |
from Prediction import *
|
4 |
import os
|
5 |
from datetime import datetime
|
6 |
+
import re
|
7 |
+
import json
|
8 |
+
import hashlib
|
9 |
|
10 |
+
persistent_path = "/output"
|
11 |
+
# os.environ['HF_HOME'] = os.path.join(persistent_path, ".huggingface")
|
12 |
+
user_input_path = os.path.join(persistent_path, 'user.jsonl')
|
13 |
+
secret = "2fc9ff032e027e8f23bb9fb693234899"
|
14 |
+
|
15 |
+
def get_md5(s):
|
16 |
+
md = hashlib.md5()
|
17 |
+
md.update(s.encode('utf-8'))
|
18 |
+
return md.hexdigest()
|
19 |
|
20 |
examples = []
|
21 |
if os.path.exists("assets/examples.txt"):
|
|
|
38 |
|
39 |
def single_sentence(sentence):
|
40 |
predictions = predict_single(sentence, tokenizer, model, device)
|
41 |
+
return sorted(zip(LABEL_COLUMNS, predictions), key=lambda x:x[-1], reverse=True)
|
|
|
42 |
|
43 |
def csv_process(csv_file, attr="content"):
|
44 |
current_time = datetime.now()
|
|
|
53 |
outputs.append(output_path)
|
54 |
return outputs
|
55 |
|
56 |
+
def logfile_query(auth):
|
57 |
+
if get_md5(auth) == secret and os.path.exists(user_input_path):
|
58 |
+
return [user_input_path]
|
59 |
+
else:
|
60 |
+
return None
|
61 |
+
|
62 |
+
|
63 |
+
def check_save(fname, lname, cnum, email, oname, position):
|
64 |
+
errors = []
|
65 |
+
valid_vars = {}
|
66 |
+
|
67 |
+
if not fname.strip() or not lname.strip():
|
68 |
+
errors.append("Name cannot be empty")
|
69 |
+
elif fname.isdigit() or lname.isdigit():
|
70 |
+
errors.append("Name cannot be purely numerical")
|
71 |
+
else:
|
72 |
+
valid_vars["fname"] = fname
|
73 |
+
valid_vars["lname"] = lname
|
74 |
+
|
75 |
+
valid_vars["cnum"] = ''
|
76 |
+
if cnum:
|
77 |
+
if not cnum.isdigit():
|
78 |
+
errors.append("The phone number must be a pure number")
|
79 |
+
else:
|
80 |
+
valid_vars["cnum"] = cnum
|
81 |
+
|
82 |
+
if not email.strip():
|
83 |
+
errors.append("Email cannot be empty")
|
84 |
+
elif not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email):
|
85 |
+
errors.append("Incorrect email format")
|
86 |
+
else:
|
87 |
+
valid_vars["email"] = email
|
88 |
+
|
89 |
+
if not oname.strip():
|
90 |
+
errors.append("Organization name cannot be empty")
|
91 |
+
elif oname.isdigit():
|
92 |
+
errors.append("Organization cannot be purely numerical")
|
93 |
+
else:
|
94 |
+
valid_vars["oname"] = oname
|
95 |
+
|
96 |
+
valid_vars["position"] = ''
|
97 |
+
if position:
|
98 |
+
if position.isdigit():
|
99 |
+
errors.append("Position in your company cannot be purely numerical")
|
100 |
+
else:
|
101 |
+
valid_vars["position"] = position
|
102 |
+
|
103 |
+
if errors:
|
104 |
+
return errors
|
105 |
+
|
106 |
+
current_time = datetime.now()
|
107 |
+
formatted_time = current_time.strftime("%Y_%m_%d_%H_%M_%S")
|
108 |
+
valid_vars['time'] = formatted_time
|
109 |
+
|
110 |
+
with open(user_input_path, 'a+', encoding="utf8") as file:
|
111 |
+
file.write(json.dumps(valid_vars)+"\n")
|
112 |
+
|
113 |
+
records = {}
|
114 |
+
with open(user_input_path, 'r', encoding="utf8") as file:
|
115 |
+
for line in file:
|
116 |
+
line = line.strip()
|
117 |
+
dct = json.loads(line)
|
118 |
+
records[dct['time']] = dct
|
119 |
+
|
120 |
+
return records
|
121 |
+
|
122 |
|
123 |
my_theme = gr.Theme.from_hub("JohnSmith9982/small_and_pretty")
|
124 |
with gr.Blocks(theme=my_theme, title='Brand_Tone_of_Voice_demo') as demo:
|
|
|
139 |
</div>
|
140 |
""")
|
141 |
|
142 |
+
with gr.Column(visible=True) as regis:
|
143 |
+
gr.Markdown("# Welcome to BTV! Please fill out the form below to continue.\nI’m assuming that you mention somewhere that this project/research is conducted by the University of Manchester/AMBS. By ticking this box, I consent to be approached by the research team of the University of Manchester.")
|
144 |
+
with gr.Column(variant='panel'):
|
145 |
+
fname_tb = gr.Textbox(label="First Name: ", type='text')
|
146 |
+
lname_tb = gr.Textbox(label="Last Name: ", type='text')
|
147 |
+
email_tb = gr.Textbox(label="Email: ", type='email')
|
148 |
+
cnum_tb = gr.Textbox(label="Contact: (Optional)", type='text')
|
149 |
+
oname_tb = gr.Textbox(label="Organization name: ", type='text')
|
150 |
+
position_tb = gr.Textbox(label="Positions in your company: (Optional)", type='text')
|
151 |
+
error_box = gr.HTML(value="", visible=False)
|
152 |
+
submit_btn = gr.Button("Click here to start if you have fullfill all the item!")
|
153 |
+
|
154 |
+
with gr.Row(visible=False) as mainrow:
|
155 |
+
|
156 |
+
with gr.Tab("Single Sentence"):
|
157 |
+
with gr.Row():
|
158 |
+
tbox_input = gr.Textbox(label="Input",
|
159 |
+
info="Please input a sentence here:")
|
160 |
+
gr.Markdown("""
|
161 |
+
# Detailed information about our model:
|
162 |
+
...
|
163 |
+
""")
|
164 |
+
tab_output = gr.DataFrame(label='Predictions:',
|
165 |
+
headers=["Label", "Probability"],
|
166 |
+
datatype=["str", "number"],
|
167 |
+
interactive=False)
|
168 |
+
with gr.Row():
|
169 |
+
button_ss = gr.Button("Submit", variant="primary")
|
170 |
+
button_ss.click(fn=single_sentence, inputs=[tbox_input], outputs=[tab_output])
|
171 |
+
gr.ClearButton([tbox_input, tab_output])
|
172 |
+
|
173 |
+
gr.Examples(
|
174 |
+
examples=examples,
|
175 |
+
inputs=tbox_input,
|
176 |
+
examples_per_page=len(examples)
|
177 |
+
)
|
178 |
+
|
179 |
+
with gr.Tab("Csv File"):
|
180 |
+
with gr.Row():
|
181 |
+
csv_input = gr.File(label="CSV File:",
|
182 |
+
file_types=['.csv'],
|
183 |
+
file_count="single"
|
184 |
+
)
|
185 |
+
csv_output = gr.File(label="Predictions:")
|
186 |
+
|
187 |
+
with gr.Row():
|
188 |
+
button_cf = gr.Button("Submit", variant="primary")
|
189 |
+
button_cf.click(fn=csv_process, inputs=[csv_input], outputs=[csv_output])
|
190 |
+
gr.ClearButton([csv_input, csv_output])
|
191 |
+
|
192 |
+
gr.Markdown("## Examples \n The incoming CSV must include the ``content`` field, which represents the text that needs to be predicted!")
|
193 |
+
gr.DataFrame(label='Csv input format:',
|
194 |
+
value=[[i, examples[i]] for i in range(len(examples))],
|
195 |
+
headers=["index", "content"],
|
196 |
+
datatype=["number","str"],
|
197 |
+
interactive=False
|
198 |
+
)
|
199 |
+
|
200 |
+
with gr.Tab("Readme"):
|
201 |
+
gr.Markdown(
|
202 |
+
"""
|
203 |
+
# Paper Name
|
204 |
+
|
205 |
+
# Authors
|
206 |
+
|
207 |
+
+ First author
|
208 |
+
+ Corresponding author
|
209 |
+
|
210 |
+
# Detailed Information
|
211 |
+
|
212 |
...
|
213 |
+
"""
|
214 |
+
)
|
215 |
+
|
216 |
+
with gr.Tab("Log File"):
|
217 |
+
with gr.Row():
|
218 |
+
auth_token = gr.Textbox(label="Authentication Tokens: ", info="Enter the key to download persistent stored log information.")
|
219 |
+
log_output = gr.File(label="Log file: ")
|
220 |
+
|
221 |
+
with gr.Row():
|
222 |
+
button_lf = gr.Button("Validate", variant="primary")
|
223 |
+
button_lf.click(fn=logfile_query, inputs=[auth_token], outputs=[log_output])
|
224 |
+
gr.ClearButton([auth_token, log_output])
|
225 |
+
|
226 |
+
|
227 |
+
def submit(*user_input):
|
228 |
+
res = check_save(*user_input)
|
229 |
+
if isinstance(res, list):
|
230 |
+
return {
|
231 |
+
error_box: gr.HTML(
|
232 |
+
value=f"""
|
233 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
234 |
+
<div>
|
235 |
+
<p style="color:red;">{"; ".join(res)}</p>
|
236 |
+
</div>
|
237 |
+
</div>
|
238 |
+
""",
|
239 |
+
visible=True)
|
240 |
+
}
|
241 |
+
else:
|
242 |
+
return {
|
243 |
+
mainrow: gr.Row(visible=True),
|
244 |
+
regis: gr.Row(visible=False),
|
245 |
+
error_box: gr.HTML(visible=False)
|
246 |
+
}
|
247 |
+
|
248 |
+
submit_btn.click(
|
249 |
+
submit,
|
250 |
+
[fname_tb, lname_tb, cnum_tb, email_tb, oname_tb, position_tb],
|
251 |
+
[mainrow, regis, error_box],
|
252 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
demo.launch()
|