Zhuoxuan-Zhang commited on
Commit
942ca41
·
verified ·
1 Parent(s): cb6d7fb

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. chatgpt.py +38 -30
chatgpt.py CHANGED
@@ -6,6 +6,8 @@ import tempfile
6
  import os
7
  import requests
8
  import time
 
 
9
 
10
  session = requests.Session()
11
 
@@ -17,6 +19,7 @@ base_url = "https://llm4socialisolation-fd4082d0a518.herokuapp.com"
17
  # base_url = "http://localhost:8080"
18
 
19
  timeout = 60
 
20
 
21
  # mapping between display names and internal chatbot_type values
22
  display_to_value = {
@@ -250,22 +253,31 @@ def update_chatbot_type(chatbot_display_name):
250
  chatbot_type = display_to_value.get(chatbot_display_name, 'enhanced')
251
  return chatbot_type
252
 
 
253
  def start_timer():
254
- seconds_left = 480
255
-
256
- while seconds_left >= 0:
257
- minutes, seconds = divmod(seconds_left, 60)
258
- time_str = f"Time remaining: {minutes:02}:{seconds:02}"
259
- yield time_str
260
- seconds_left -= 1
261
- time.sleep(1)
262
-
263
- if seconds_left < 0:
264
- yield "Time's up!"
265
 
266
  def reset_timer():
267
- time_str = f"Time remaining: 08:00"
268
- return time_str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
270
  # initialize prompts with empty strings
271
  initial_prompts = {'system_prompt': '', 'conv_instruction_prompt': '', 'therapy_prompt': '', 'autobio_generation_prompt': ''}
@@ -284,6 +296,9 @@ with gr.Blocks(css=css) as app:
284
  api_key_state = gr.State()
285
  prompt_visibility_state = gr.State(False)
286
 
 
 
 
287
  with gr.Row():
288
  with gr.Column(scale=1, min_width=250):
289
  gr.Markdown("## Settings")
@@ -389,7 +404,10 @@ with gr.Blocks(css=css) as app:
389
  )
390
 
391
  api_key_input = gr.Textbox(
392
- label="API Key", placeholder="Enter your API Key", type="password"
 
 
 
393
  )
394
 
395
  initialize_button = gr.Button("Initialize", variant="primary", size="large")
@@ -401,7 +419,7 @@ with gr.Blocks(css=css) as app:
401
  fn=set_initialize_button,
402
  inputs=[api_key_input, chapter_dropdown, method_dropdown, username_input,
403
  system_prompt, conv_instruction_prompt, therapy_prompt, autobio_prompt, chatbot_type_dropdown],
404
- outputs=[initialization_status, api_key_state, chatbot_type_state]
405
  )
406
 
407
  # define the function to toggle prompts visibility
@@ -418,9 +436,11 @@ with gr.Blocks(css=css) as app:
418
 
419
  with gr.Column(scale=3):
420
  with gr.Row():
421
- timer_display = gr.Markdown(value="Time remaining: 08:00", label="")
422
  start_button = gr.Button("Start Timer", elem_id="start_button")
423
- reset_button = gr.Button("Reset Timer", elem_id="reset_button")
 
 
424
 
425
  chatbot = gr.Chatbot(label="Chat here for autobiography generation", height=500)
426
 
@@ -478,18 +498,6 @@ with gr.Blocks(css=css) as app:
478
  outputs=None
479
  )
480
 
481
- start_event = start_button.click(
482
- fn=start_timer,
483
- inputs=[],
484
- outputs=timer_display
485
- )
486
-
487
- reset_button.click(
488
- fn=reset_timer,
489
- inputs=[],
490
- outputs=timer_display,
491
- cancels=[start_event]
492
- )
493
 
494
  app.queue()
495
- app.launch(share=True)
 
6
  import os
7
  import requests
8
  import time
9
+ import threading
10
+ from datetime import datetime, timedelta
11
 
12
  session = requests.Session()
13
 
 
19
  # base_url = "http://localhost:8080"
20
 
21
  timeout = 60
22
+ concurrency_count=10
23
 
24
  # mapping between display names and internal chatbot_type values
25
  display_to_value = {
 
253
  chatbot_type = display_to_value.get(chatbot_display_name, 'enhanced')
254
  return chatbot_type
255
 
256
+ # Function to start the periodic toggle
257
  def start_timer():
258
+ target_timestamp = datetime.now() + timedelta(seconds=8 * 60)
259
+ return True, target_timestamp
 
 
 
 
 
 
 
 
 
260
 
261
  def reset_timer():
262
+ is_running = False
263
+ return is_running, "Timer remaining: 8:00"
264
+
265
+
266
+ # Async function to manage periodic updates, running every second
267
+ def periodic_call(is_running, target_timestamp):
268
+ if is_running:
269
+ prefix = 'Time remaining:'
270
+ time_difference = target_timestamp - datetime.now()
271
+ second_left = int(round(time_difference.total_seconds()))
272
+ if second_left <= 0:
273
+ second_left = 0
274
+ minutes, seconds = divmod(second_left, 60)
275
+ new_remain_min = f'{minutes:02}'
276
+ new_remain_second = f'{seconds:02}'
277
+ new_info = f'{prefix} {new_remain_min}:{new_remain_second}'
278
+ return new_info
279
+ else:
280
+ return 'Time remaining: 8:00'
281
 
282
  # initialize prompts with empty strings
283
  initial_prompts = {'system_prompt': '', 'conv_instruction_prompt': '', 'therapy_prompt': '', 'autobio_generation_prompt': ''}
 
296
  api_key_state = gr.State()
297
  prompt_visibility_state = gr.State(False)
298
 
299
+ is_running = gr.State()
300
+ target_timestamp = gr.State()
301
+
302
  with gr.Row():
303
  with gr.Column(scale=1, min_width=250):
304
  gr.Markdown("## Settings")
 
404
  )
405
 
406
  api_key_input = gr.Textbox(
407
+ label="OpenAI API Key",
408
+ placeholder="Enter your openai api key",
409
+ value="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",
410
+ type="password"
411
  )
412
 
413
  initialize_button = gr.Button("Initialize", variant="primary", size="large")
 
419
  fn=set_initialize_button,
420
  inputs=[api_key_input, chapter_dropdown, method_dropdown, username_input,
421
  system_prompt, conv_instruction_prompt, therapy_prompt, autobio_prompt, chatbot_type_dropdown],
422
+ outputs=[initialization_status, api_key_state, chatbot_type_state],
423
  )
424
 
425
  # define the function to toggle prompts visibility
 
436
 
437
  with gr.Column(scale=3):
438
  with gr.Row():
439
+ timer_display = gr.Textbox(value="Time remaining: 08:00", label="")
440
  start_button = gr.Button("Start Timer", elem_id="start_button")
441
+
442
+ start_button.click(start_timer, outputs=[is_running, target_timestamp]).then(
443
+ periodic_call, inputs=[is_running, target_timestamp], outputs=timer_display, every=1)
444
 
445
  chatbot = gr.Chatbot(label="Chat here for autobiography generation", height=500)
446
 
 
498
  outputs=None
499
  )
500
 
 
 
 
 
 
 
 
 
 
 
 
 
501
 
502
  app.queue()
503
+ app.launch(share=True, max_threads=10)