IELTS8 commited on
Commit
cdad349
1 Parent(s): 05b962c

Upload 3 files

Browse files

The first commit

Files changed (3) hide show
  1. app_ISF.py +417 -0
  2. custom.css +191 -0
  3. share_btn.py +98 -0
app_ISF.py ADDED
@@ -0,0 +1,417 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import logging
4
+ import sys
5
+ import torch
6
+ import gradio as gr
7
+ from huggingface_hub import Repository
8
+ from text_generation import Client
9
+ from app_modules.utils import convert_to_markdown
10
+ # from dialogues import DialogueTemplate
11
+ from share_btn import (community_icon_html, loading_icon_html, share_btn_css,
12
+ share_js)
13
+
14
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
15
+ API_TOKEN = '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'
16
+ API_URL = os.environ.get("API_URL", None)
17
+ API_URL = "https://api-inference.huggingface.co/models/timdettmers/guanaco-33b-merged"
18
+
19
+ client = Client(
20
+ API_URL,
21
+ headers={"Authorization": f"Bearer {API_TOKEN}"},
22
+ )
23
+
24
+ repo = None
25
+
26
+ logging.basicConfig(
27
+ format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
28
+ datefmt="%Y-%m-%dT%H:%M:%SZ",
29
+ )
30
+ logger = logging.getLogger(__name__)
31
+ logger.setLevel(logging.DEBUG)
32
+
33
+ examples = [
34
+ "Describe the advantages and disadvantages of Incremental Sheet Forming.",
35
+ "Describe the applications of Incremental Sheet Forming.",
36
+ "Describe the process parameters included in Incremental Sheet Forming in dot points."
37
+ ]
38
+
39
+
40
+ def get_total_inputs(inputs, chatbot, preprompt, user_name, assistant_name, sep):
41
+ past = []
42
+ for data in chatbot:
43
+ user_data, model_data = data
44
+
45
+ if not user_data.startswith(user_name):
46
+ user_data = user_name + user_data
47
+ if not model_data.startswith(sep + assistant_name):
48
+ model_data = sep + assistant_name + model_data
49
+
50
+ past.append(user_data + model_data.rstrip() + sep)
51
+
52
+ if not inputs.startswith(user_name):
53
+ inputs = user_name + inputs
54
+
55
+ total_inputs = preprompt + "".join(past) + inputs + sep + assistant_name.rstrip()
56
+
57
+ return total_inputs
58
+
59
+
60
+ def has_no_history(chatbot, history):
61
+ return not chatbot and not history
62
+
63
+
64
+ header = "A chat between a curious human and an artificial intelligence assistant about Incremental Sheet Forming (ISF). " \
65
+ "The assistant gives helpful, detailed, and polite answers to the user's questions."
66
+ prompt_template = "### Human: {query}\n### Assistant:{response}"
67
+
68
+
69
+ def generate(
70
+ user_message,
71
+ chatbot,
72
+ history,
73
+ temperature,
74
+ top_p,
75
+ top_k,
76
+ max_new_tokens,
77
+ repetition_penalty,
78
+ ):
79
+ # Don't return meaningless message when the input is empty
80
+ if not user_message:
81
+ print("Empty input")
82
+
83
+ history.append(user_message)
84
+
85
+ past_messages = []
86
+ for data in chatbot:
87
+ user_data, model_data = data
88
+
89
+ past_messages.extend(
90
+ [{"role": "user", "content": user_data}, {"role": "assistant", "content": model_data.rstrip()}]
91
+ )
92
+
93
+ if len(past_messages) < 1:
94
+ prompt = header + prompt_template.format(query=user_message, response="")
95
+ else:
96
+ prompt = header
97
+ for i in range(0, len(past_messages), 2):
98
+ intermediate_prompt = prompt_template.format(query=past_messages[i]["content"],
99
+ response=past_messages[i + 1]["content"])
100
+ print("intermediate: ", intermediate_prompt)
101
+ prompt = prompt + '\n' + intermediate_prompt
102
+
103
+ prompt = prompt + prompt_template.format(query=user_message, response="")
104
+
105
+ temperature = float(temperature)
106
+ if temperature < 1e-2:
107
+ temperature = 1e-2
108
+ top_p = float(top_p)
109
+
110
+ generate_kwargs = dict(
111
+ temperature=temperature,
112
+ max_new_tokens=max_new_tokens,
113
+ top_p=top_p,
114
+ top_k=top_k,
115
+ repetition_penalty=repetition_penalty,
116
+ do_sample=True,
117
+ truncate=999,
118
+ seed=42,
119
+ )
120
+
121
+ stream = client.generate_stream(
122
+ prompt,
123
+ **generate_kwargs,
124
+ )
125
+
126
+ output = ""
127
+ for idx, response in enumerate(stream):
128
+ if response.token.text == '':
129
+ break
130
+
131
+ if response.token.special:
132
+ continue
133
+ output += response.token.text
134
+ if idx == 0:
135
+ history.append(" " + output)
136
+ else:
137
+ history[-1] = output
138
+
139
+ chat = [(convert_to_markdown(history[i].strip()), convert_to_markdown(history[i + 1].strip())) for i in range(0, len(history) - 1, 2)]
140
+
141
+ yield chat, history, user_message, ""
142
+
143
+ return chat, history, user_message, ""
144
+
145
+
146
+ def clear_chat():
147
+ return [], []
148
+
149
+
150
+ def save(
151
+ history,
152
+ temperature=0.7,
153
+ top_p=0.9,
154
+ top_k=50,
155
+ max_new_tokens=512,
156
+ repetition_penalty=1.2,
157
+ max_memory=1024,
158
+ ):
159
+ history = [] if history is None else history
160
+ data_point = {'history': history, 'generation_parameter': {
161
+ "temperature": temperature,
162
+ "top_p": top_p,
163
+ "top_k": top_k,
164
+ "max_new_tokens": max_new_tokens,
165
+ "repetition_penalty": repetition_penalty,
166
+ "max_memory": max_memory,
167
+ }}
168
+ print(data_point)
169
+ file_name = "history.jsonl"
170
+ with open(file_name, 'a') as f:
171
+ for line in [data_point]:
172
+ f.write(json.dumps(line, ensure_ascii=False) + '\n')
173
+
174
+
175
+ def process_example(args):
176
+ for [x, y] in generate(args):
177
+ pass
178
+ return [x, y]
179
+
180
+
181
+ title = """<h1 align="center">ISF Alpaca 💬</h1>"""
182
+ custom_css = """
183
+ #banner-image {
184
+ display: block;
185
+ margin-left: auto;
186
+ margin-right: auto;
187
+ }
188
+ #chat-message {
189
+ font-size: 14px;
190
+ min-height: 300px;
191
+ }
192
+ """
193
+
194
+ with gr.Blocks(analytics_enabled=False,
195
+ theme=gr.themes.Soft(),
196
+ css=".disclaimer {font-variant-caps: all-small-caps;}") as demo:
197
+ gr.HTML(title)
198
+ # status_display = gr.Markdown("Success", elem_id="status_display")
199
+ with gr.Row():
200
+ with gr.Column():
201
+ gr.Markdown(
202
+ """
203
+ 🏭 The fine-tuned model primarily emphasizes **Knowledge Augmentation** in the Manufacturing domain,
204
+ with **Incremental Sheet Forming (ISF)** serving as a use case.
205
+ """
206
+ )
207
+ history = gr.components.State()
208
+
209
+ with gr.Row(scale=1).style(equal_height=True):
210
+ with gr.Column(scale=5):
211
+ with gr.Row(scale=1):
212
+ chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height=476)
213
+ with gr.Row(scale=1):
214
+ with gr.Column(scale=12):
215
+ user_message = gr.Textbox(
216
+ show_label=False, placeholder="Enter text"
217
+ ).style(container=False)
218
+ with gr.Column(min_width=70, scale=1):
219
+ submit_btn = gr.Button("Send")
220
+ with gr.Column(min_width=70, scale=1):
221
+ stop_btn = gr.Button("Stop")
222
+ with gr.Row():
223
+ gr.Examples(
224
+ examples=examples,
225
+ inputs=[user_message],
226
+ cache_examples=False,
227
+ outputs=[chatbot, history],
228
+ )
229
+ with gr.Row(scale=1):
230
+ clear_history = gr.Button(
231
+ "🧹 New Conversation",
232
+ )
233
+ reset_btn = gr.Button("🔄 Reset Parameter")
234
+ save_btn = gr.Button("📥 Save Chat")
235
+ with gr.Column():
236
+ input_component_column = gr.Column(min_width=50, scale=1)
237
+ with input_component_column:
238
+ with gr.Tab(label="Parameter Setting"):
239
+ gr.Markdown("# Parameters")
240
+ temperature = gr.components.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
241
+ top_p = gr.components.Slider(minimum=0, maximum=1, value=0.9, label="Top p")
242
+ top_k = gr.components.Slider(minimum=0, maximum=100, step=1, value=20, label="Top k")
243
+ max_new_tokens = gr.components.Slider(minimum=1, maximum=2048, step=1, value=512,
244
+ label="Max New Tokens")
245
+ repetition_penalty = gr.components.Slider(minimum=0.1, maximum=10.0, step=0.1, value=1.2,
246
+ label="Repetition Penalty")
247
+ max_memory = gr.components.Slider(minimum=0, maximum=2048, step=1, value=2048, label="Max Memory")
248
+
249
+ history = gr.State([])
250
+ last_user_message = gr.State("")
251
+
252
+ user_message.submit(
253
+ generate,
254
+ inputs=[
255
+ user_message,
256
+ chatbot,
257
+ history,
258
+ temperature,
259
+ top_p,
260
+ top_k,
261
+ max_new_tokens,
262
+ repetition_penalty,
263
+ ],
264
+ outputs=[chatbot, history, last_user_message, user_message],
265
+ )
266
+
267
+ submit_event = submit_btn.click(
268
+ generate,
269
+ inputs=[
270
+ user_message,
271
+ chatbot,
272
+ history,
273
+ temperature,
274
+ top_p,
275
+ top_k,
276
+ max_new_tokens,
277
+ repetition_penalty,
278
+ ],
279
+ outputs=[chatbot, history, last_user_message, user_message],
280
+ )
281
+ # submit_btn.click(
282
+ # lambda: (
283
+ # submit_btn.update(visible=False),
284
+ # stop_btn.update(visible=True),
285
+ # ),
286
+ # inputs=None,
287
+ # outputs=[submit_btn, stop_btn],
288
+ # queue=False,
289
+ # )
290
+
291
+ stop_btn.click(
292
+ lambda: (
293
+ submit_btn.update(visible=True),
294
+ stop_btn.update(visible=True),
295
+ ),
296
+ inputs=None,
297
+ outputs=[submit_btn, stop_btn],
298
+ cancels=[submit_event],
299
+ queue=False,
300
+ )
301
+
302
+ clear_history.click(clear_chat, outputs=[chatbot, history])
303
+ save_btn.click(
304
+ save,
305
+ inputs=[user_message, chatbot, history, temperature, top_p, top_k, max_new_tokens, repetition_penalty],
306
+ outputs=None,
307
+ )
308
+
309
+ input_components_except_states = [user_message, chatbot, history, temperature, top_p, top_k, max_new_tokens,
310
+ repetition_penalty]
311
+
312
+ reset_btn.click(
313
+ None,
314
+ [],
315
+ (input_components_except_states + [input_component_column]), # type: ignore
316
+ _js=f"""() => {json.dumps([getattr(component, "cleared_value", None) for component in input_components_except_states]
317
+ + ([gr.Column.update(visible=True)])
318
+ + ([])
319
+ )}
320
+ """,
321
+ )
322
+
323
+ demo.queue(concurrency_count=16).launch(debug=True, share=True)
324
+
325
+ # with gr.Row():
326
+ # with gr.Box():
327
+ # output = gr.Markdown()
328
+ # chatbot = gr.Chatbot(elem_id="chat-message", label="Chat")
329
+ #
330
+ # with gr.Row():
331
+ # with gr.Column(scale=3):
332
+ # user_message = gr.Textbox(placeholder="Enter your message here", show_label=False, elem_id="q-input")
333
+ # with gr.Row():
334
+ # send_button = gr.Button("Send", elem_id="send-btn", visible=True)
335
+ #
336
+ # clear_chat_button = gr.Button("Clear chat", elem_id="clear-btn", visible=True)
337
+ #
338
+ # with gr.Accordion(label="Parameters", open=False, elem_id="parameters-accordion"):
339
+ # temperature = gr.Slider(
340
+ # label="Temperature",
341
+ # value=0.7,
342
+ # minimum=0.0,
343
+ # maximum=1.0,
344
+ # step=0.1,
345
+ # interactive=True,
346
+ # info="Higher values produce more diverse outputs",
347
+ # )
348
+ # top_p = gr.Slider(
349
+ # label="Top-p (nucleus sampling)",
350
+ # value=0.9,
351
+ # minimum=0.0,
352
+ # maximum=1,
353
+ # step=0.05,
354
+ # interactive=True,
355
+ # info="Higher values sample more low-probability tokens",
356
+ # )
357
+ # max_new_tokens = gr.Slider(
358
+ # label="Max new tokens",
359
+ # value=1024,
360
+ # minimum=0,
361
+ # maximum=2048,
362
+ # step=4,
363
+ # interactive=True,
364
+ # info="The maximum numbers of new tokens",
365
+ # )
366
+ # repetition_penalty = gr.Slider(
367
+ # label="Repetition Penalty",
368
+ # value=1.2,
369
+ # minimum=0.0,
370
+ # maximum=10,
371
+ # step=0.1,
372
+ # interactive=True,
373
+ # info="The parameter for repetition penalty. 1.0 means no penalty.",
374
+ # )
375
+ # with gr.Row():
376
+ # gr.Examples(
377
+ # examples=examples,
378
+ # inputs=[user_message],
379
+ # cache_examples=False,
380
+ # fn=process_example,
381
+ # outputs=[output],
382
+ # )
383
+ #
384
+ # history = gr.State([])
385
+ # last_user_message = gr.State("")
386
+ #
387
+ # user_message.submit(
388
+ # generate,
389
+ # inputs=[
390
+ # user_message,
391
+ # chatbot,
392
+ # history,
393
+ # temperature,
394
+ # top_p,
395
+ # max_new_tokens,
396
+ # repetition_penalty,
397
+ # ],
398
+ # outputs=[chatbot, history, last_user_message, user_message],
399
+ # )
400
+ #
401
+ # send_button.click(
402
+ # generate,
403
+ # inputs=[
404
+ # user_message,
405
+ # chatbot,
406
+ # history,
407
+ # temperature,
408
+ # top_p,
409
+ # max_new_tokens,
410
+ # repetition_penalty,
411
+ # ],
412
+ # outputs=[chatbot, history, last_user_message, user_message],
413
+ # )
414
+ #
415
+ # clear_chat_button.click(clear_chat, outputs=[chatbot, history])
416
+
417
+ demo.queue(concurrency_count=16).launch(debug=True, share=True)
custom.css ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ --chatbot-color-light: #F3F3F3;
3
+ --chatbot-color-dark: #121111;
4
+ }
5
+
6
+ /* status_display */
7
+ #status_display {
8
+ display: flex;
9
+ min-height: 2.5em;
10
+ align-items: flex-end;
11
+ justify-content: flex-end;
12
+ }
13
+ #status_display p {
14
+ font-size: .85em;
15
+ font-family: monospace;
16
+ color: var(--body-text-color-subdued);
17
+ }
18
+
19
+
20
+
21
+ /* usage_display */
22
+ #usage_display {
23
+ height: 1em;
24
+ }
25
+ #usage_display p{
26
+ padding: 0 1em;
27
+ font-size: .85em;
28
+ font-family: monospace;
29
+ color: var(--body-text-color-subdued);
30
+ }
31
+ /* list */
32
+ ol:not(.options), ul:not(.options) {
33
+ padding-inline-start: 2em !important;
34
+ }
35
+
36
+ /* Thank @Keldos-Li for fixing it */
37
+ /* Light mode (default) */
38
+ #chuanhu_chatbot {
39
+ background-color: var(--chatbot-color-light) !important;
40
+ color: #000000 !important;
41
+ }
42
+ [data-testid = "bot"] {
43
+ background-color: #FFFFFF !important;
44
+ }
45
+ [data-testid = "user"] {
46
+ background-color: #95EC69 !important;
47
+ }
48
+
49
+ /* Dark mode */
50
+ .dark #chuanhu_chatbot {
51
+ background-color: var(--chatbot-color-dark) !important;
52
+ color: #FFFFFF !important;
53
+ }
54
+ .dark [data-testid = "bot"] {
55
+ background-color: #2C2C2C !important;
56
+ }
57
+ .dark [data-testid = "user"] {
58
+ background-color: #26B561 !important;
59
+ }
60
+
61
+ #chuanhu_chatbot {
62
+ height: 100%;
63
+ min-height: 400px;
64
+ }
65
+
66
+ [class *= "message"] {
67
+ border-radius: var(--radius-xl) !important;
68
+ border: none;
69
+ padding: var(--spacing-xl) !important;
70
+ font-size: var(--text-md) !important;
71
+ line-height: var(--line-md) !important;
72
+ min-height: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl));
73
+ min-width: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl));
74
+ }
75
+ [data-testid = "bot"] {
76
+ max-width: 85%;
77
+ border-bottom-left-radius: 0 !important;
78
+ }
79
+ [data-testid = "user"] {
80
+ max-width: 85%;
81
+ width: auto !important;
82
+ border-bottom-right-radius: 0 !important;
83
+ }
84
+ /* Table */
85
+ table {
86
+ margin: 1em 0;
87
+ border-collapse: collapse;
88
+ empty-cells: show;
89
+ }
90
+ td,th {
91
+ border: 1.2px solid var(--border-color-primary) !important;
92
+ padding: 0.2em;
93
+ }
94
+ thead {
95
+ background-color: rgba(175,184,193,0.2);
96
+ }
97
+ thead th {
98
+ padding: .5em .2em;
99
+ }
100
+ /* Inline code */
101
+ #chuanhu_chatbot code {
102
+ display: inline;
103
+ white-space: break-spaces;
104
+ border-radius: 6px;
105
+ margin: 0 2px 0 2px;
106
+ padding: .2em .4em .1em .4em;
107
+ background-color: rgba(175,184,193,0.2);
108
+ }
109
+ /* Code block */
110
+ #chuanhu_chatbot pre code {
111
+ display: block;
112
+ overflow: auto;
113
+ white-space: pre;
114
+ background-color: hsla(0, 0%, 0%, 80%)!important;
115
+ border-radius: 10px;
116
+ padding: 1.4em 1.2em 0em 1.4em;
117
+ margin: 1.2em 2em 1.2em 0.5em;
118
+ color: #FFF;
119
+ box-shadow: 6px 6px 16px hsla(0, 0%, 0%, 0.2);
120
+ }
121
+ /* Hightlight */
122
+ #chuanhu_chatbot .highlight { background-color: transparent }
123
+ #chuanhu_chatbot .highlight .hll { background-color: #49483e }
124
+ #chuanhu_chatbot .highlight .c { color: #75715e } /* Comment */
125
+ #chuanhu_chatbot .highlight .err { color: #960050; background-color: #1e0010 } /* Error */
126
+ #chuanhu_chatbot .highlight .k { color: #66d9ef } /* Keyword */
127
+ #chuanhu_chatbot .highlight .l { color: #ae81ff } /* Literal */
128
+ #chuanhu_chatbot .highlight .n { color: #f8f8f2 } /* Name */
129
+ #chuanhu_chatbot .highlight .o { color: #f92672 } /* Operator */
130
+ #chuanhu_chatbot .highlight .p { color: #f8f8f2 } /* Punctuation */
131
+ #chuanhu_chatbot .highlight .ch { color: #75715e } /* Comment.Hashbang */
132
+ #chuanhu_chatbot .highlight .cm { color: #75715e } /* Comment.Multiline */
133
+ #chuanhu_chatbot .highlight .cp { color: #75715e } /* Comment.Preproc */
134
+ #chuanhu_chatbot .highlight .cpf { color: #75715e } /* Comment.PreprocFile */
135
+ #chuanhu_chatbot .highlight .c1 { color: #75715e } /* Comment.Single */
136
+ #chuanhu_chatbot .highlight .cs { color: #75715e } /* Comment.Special */
137
+ #chuanhu_chatbot .highlight .gd { color: #f92672 } /* Generic.Deleted */
138
+ #chuanhu_chatbot .highlight .ge { font-style: italic } /* Generic.Emph */
139
+ #chuanhu_chatbot .highlight .gi { color: #a6e22e } /* Generic.Inserted */
140
+ #chuanhu_chatbot .highlight .gs { font-weight: bold } /* Generic.Strong */
141
+ #chuanhu_chatbot .highlight .gu { color: #75715e } /* Generic.Subheading */
142
+ #chuanhu_chatbot .highlight .kc { color: #66d9ef } /* Keyword.Constant */
143
+ #chuanhu_chatbot .highlight .kd { color: #66d9ef } /* Keyword.Declaration */
144
+ #chuanhu_chatbot .highlight .kn { color: #f92672 } /* Keyword.Namespace */
145
+ #chuanhu_chatbot .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
146
+ #chuanhu_chatbot .highlight .kr { color: #66d9ef } /* Keyword.Reserved */
147
+ #chuanhu_chatbot .highlight .kt { color: #66d9ef } /* Keyword.Type */
148
+ #chuanhu_chatbot .highlight .ld { color: #e6db74 } /* Literal.Date */
149
+ #chuanhu_chatbot .highlight .m { color: #ae81ff } /* Literal.Number */
150
+ #chuanhu_chatbot .highlight .s { color: #e6db74 } /* Literal.String */
151
+ #chuanhu_chatbot .highlight .na { color: #a6e22e } /* Name.Attribute */
152
+ #chuanhu_chatbot .highlight .nb { color: #f8f8f2 } /* Name.Builtin */
153
+ #chuanhu_chatbot .highlight .nc { color: #a6e22e } /* Name.Class */
154
+ #chuanhu_chatbot .highlight .no { color: #66d9ef } /* Name.Constant */
155
+ #chuanhu_chatbot .highlight .nd { color: #a6e22e } /* Name.Decorator */
156
+ #chuanhu_chatbot .highlight .ni { color: #f8f8f2 } /* Name.Entity */
157
+ #chuanhu_chatbot .highlight .ne { color: #a6e22e } /* Name.Exception */
158
+ #chuanhu_chatbot .highlight .nf { color: #a6e22e } /* Name.Function */
159
+ #chuanhu_chatbot .highlight .nl { color: #f8f8f2 } /* Name.Label */
160
+ #chuanhu_chatbot .highlight .nn { color: #f8f8f2 } /* Name.Namespace */
161
+ #chuanhu_chatbot .highlight .nx { color: #a6e22e } /* Name.Other */
162
+ #chuanhu_chatbot .highlight .py { color: #f8f8f2 } /* Name.Property */
163
+ #chuanhu_chatbot .highlight .nt { color: #f92672 } /* Name.Tag */
164
+ #chuanhu_chatbot .highlight .nv { color: #f8f8f2 } /* Name.Variable */
165
+ #chuanhu_chatbot .highlight .ow { color: #f92672 } /* Operator.Word */
166
+ #chuanhu_chatbot .highlight .w { color: #f8f8f2 } /* Text.Whitespace */
167
+ #chuanhu_chatbot .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
168
+ #chuanhu_chatbot .highlight .mf { color: #ae81ff } /* Literal.Number.Float */
169
+ #chuanhu_chatbot .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
170
+ #chuanhu_chatbot .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
171
+ #chuanhu_chatbot .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
172
+ #chuanhu_chatbot .highlight .sa { color: #e6db74 } /* Literal.String.Affix */
173
+ #chuanhu_chatbot .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
174
+ #chuanhu_chatbot .highlight .sc { color: #e6db74 } /* Literal.String.Char */
175
+ #chuanhu_chatbot .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
176
+ #chuanhu_chatbot .highlight .sd { color: #e6db74 } /* Literal.String.Doc */
177
+ #chuanhu_chatbot .highlight .s2 { color: #e6db74 } /* Literal.String.Double */
178
+ #chuanhu_chatbot .highlight .se { color: #ae81ff } /* Literal.String.Escape */
179
+ #chuanhu_chatbot .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
180
+ #chuanhu_chatbot .highlight .si { color: #e6db74 } /* Literal.String.Interpol */
181
+ #chuanhu_chatbot .highlight .sx { color: #e6db74 } /* Literal.String.Other */
182
+ #chuanhu_chatbot .highlight .sr { color: #e6db74 } /* Literal.String.Regex */
183
+ #chuanhu_chatbot .highlight .s1 { color: #e6db74 } /* Literal.String.Single */
184
+ #chuanhu_chatbot .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
185
+ #chuanhu_chatbot .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
186
+ #chuanhu_chatbot .highlight .fm { color: #a6e22e } /* Name.Function.Magic */
187
+ #chuanhu_chatbot .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
188
+ #chuanhu_chatbot .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
189
+ #chuanhu_chatbot .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
190
+ #chuanhu_chatbot .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
191
+ #chuanhu_chatbot .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
share_btn.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
2
+ <path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
3
+ <path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
4
+ </svg>"""
5
+
6
+ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
7
+ style="color: #ffffff;
8
+ "
9
+ xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
10
+
11
+ share_js = """async () => {
12
+ async function uploadFile(file){
13
+ const UPLOAD_URL = 'https://huggingface.co/uploads';
14
+ const response = await fetch(UPLOAD_URL, {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': file.type,
18
+ 'X-Requested-With': 'XMLHttpRequest',
19
+ },
20
+ body: file, /// <- File inherits from Blob
21
+ });
22
+ const url = await response.text();
23
+ return url;
24
+ }
25
+ async function getInputImgFile(imgEl){
26
+ const res = await fetch(imgEl.src);
27
+ const blob = await res.blob();
28
+ const imgId = Date.now() % 200;
29
+ const isPng = imgEl.src.startsWith(`data:image/png`);
30
+ if(isPng){
31
+ const fileName = `sd-perception-${{imgId}}.png`;
32
+ return new File([blob], fileName, { type: 'image/png' });
33
+ }else{
34
+ const fileName = `sd-perception-${{imgId}}.jpg`;
35
+ return new File([blob], fileName, { type: 'image/jpeg' });
36
+ }
37
+ }
38
+ // const gradioEl = document.querySelector('body > gradio-app');
39
+ const gradioEl = document.querySelector("gradio-app");
40
+ const inputTxt = gradioEl.querySelector('#q-input textarea').value;
41
+ const outputTxt = gradioEl.querySelector('#q-output').outerHTML;
42
+ const titleLength = 150;
43
+ let titleTxt = inputTxt;
44
+ if(titleTxt.length > titleLength){
45
+ titleTxt = titleTxt.slice(0, titleLength) + ' ...';
46
+ }
47
+ const shareBtnEl = gradioEl.querySelector('#share-btn');
48
+ const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
49
+ const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
50
+ if(!inputTxt || !outputTxt){
51
+ return;
52
+ };
53
+ shareBtnEl.style.pointerEvents = 'none';
54
+ shareIconEl.style.display = 'none';
55
+ loadingIconEl.style.removeProperty('display');
56
+ const descriptionMd = `### Question:
57
+ ${inputTxt}
58
+ ### Answer:
59
+ ${outputTxt}`;
60
+ const params = {
61
+ title: titleTxt,
62
+ description: descriptionMd,
63
+ };
64
+ const paramsStr = Object.entries(params)
65
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
66
+ .join('&');
67
+ window.open(`https://huggingface.co/spaces/HuggingFaceH4/star-chat-demo/discussions/new?${paramsStr}`, '_blank');
68
+ shareBtnEl.style.removeProperty('pointer-events');
69
+ shareIconEl.style.removeProperty('display');
70
+ loadingIconEl.style.display = 'none';
71
+ }"""
72
+
73
+ share_btn_css = """
74
+ a {text-decoration-line: underline; font-weight: 600;}
75
+ .animate-spin {
76
+ animation: spin 1s linear infinite;
77
+ }
78
+ @keyframes spin {
79
+ from { transform: rotate(0deg); }
80
+ to { transform: rotate(360deg); }
81
+ }
82
+ #share-btn-container {
83
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
84
+ }
85
+ #share-btn {
86
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
87
+ }
88
+ #share-btn * {
89
+ all: unset;
90
+ }
91
+ #share-btn-container div:nth-child(-n+2){
92
+ width: auto !important;
93
+ min-height: 0px !important;
94
+ }
95
+ #share-btn-container .wrap {
96
+ display: none !important;
97
+ }
98
+ """