jianghuyihei commited on
Commit
a28c21c
1 Parent(s): bda1c4c
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -360,8 +360,8 @@ def fix_markdown(text):
360
  async def add_user_id_and_state_cookie(request: Request, call_next):
361
  user_id = request.cookies.get("user_id")
362
  state = request.cookies.get("state")
 
363
  response = await call_next(request)
364
-
365
  if not user_id:
366
  user_id = str(uuid.uuid4())
367
  state = "generate"
@@ -370,6 +370,7 @@ async def add_user_id_and_state_cookie(request: Request, call_next):
370
  elif not state:
371
  state = "generate"
372
  response.set_cookie(key="state", value=state)
 
373
  return response
374
 
375
  @app.get("/user_id")
@@ -382,7 +383,7 @@ async def get_user_id(request: Request):
382
 
383
  @app.get("/", response_class=HTMLResponse)
384
  def form_get():
385
- return Template(html_template).render(idea= "This is a example of the idea geneartion", error=None, reply_count=reply_count,button_text="Generate",loading_text="Generating content, Usually takes 2-3 minutes, please wait...")
386
 
387
  @app.post("/", response_class=HTMLResponse)
388
  def form_post(request: Request,response: Response,topic: str = Form(...)):
@@ -397,27 +398,28 @@ def form_post(request: Request,response: Response,topic: str = Form(...)):
397
  response.set_cookie(key="state", value=state)
398
 
399
  print(user_id,state)
400
- loading_text = "Generating content, Usually takes 2-3 minutes, please wait..."
401
  if state == "generate":
402
  if not queue.empty():
403
  queue_len = queue.qsize()
404
  if queue_len + reply_count >= MAX_REPLIES_PER_DAY:
405
  error_message = "Today's maximum number of replies has been reached. Please try again tomorrow."
406
  return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text="Generate",loading_text=loading_text)
407
- error_message = "There are currently {} requests being processed. If you want to queue, please rewrite your topic and click the Continue button and you will enter the queue.".format(queue_len)
408
  new_state = "continue"
409
  new_button_text = "Continue"
410
  response.set_cookie(key="state", value=new_state)
411
- return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text=new_button_text,loading_text=f"Generating content, Usually takes {queue_len*2}-{queue_len*3} minutes, please wait...")
412
 
413
- queue.put(user_id)
414
  new_state = "generate"
415
  new_button_text = "Generate"
416
  response.set_cookie(key="state", value=new_state)
417
  queue_len = queue.qsize()
418
 
419
  # 判断当前是否轮到该用户,如果没轮到则一直等待到轮到为止
420
- while queue.queue[0] != user_id:
 
421
  continue
422
 
423
  with lock:
 
360
  async def add_user_id_and_state_cookie(request: Request, call_next):
361
  user_id = request.cookies.get("user_id")
362
  state = request.cookies.get("state")
363
+ print(f"Before call_next: user_id={user_id}, state={state}")
364
  response = await call_next(request)
 
365
  if not user_id:
366
  user_id = str(uuid.uuid4())
367
  state = "generate"
 
370
  elif not state:
371
  state = "generate"
372
  response.set_cookie(key="state", value=state)
373
+ print(f"After call_next: user_id={user_id}, state={state}")
374
  return response
375
 
376
  @app.get("/user_id")
 
383
 
384
  @app.get("/", response_class=HTMLResponse)
385
  def form_get():
386
+ return Template(html_template).render(idea= "This is a example of the idea geneartion", error=None, reply_count=reply_count,button_text="Generate",loading_text="Generating content, Usually takes 3-4 minutes, please wait...")
387
 
388
  @app.post("/", response_class=HTMLResponse)
389
  def form_post(request: Request,response: Response,topic: str = Form(...)):
 
398
  response.set_cookie(key="state", value=state)
399
 
400
  print(user_id,state)
401
+ loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
402
  if state == "generate":
403
  if not queue.empty():
404
  queue_len = queue.qsize()
405
  if queue_len + reply_count >= MAX_REPLIES_PER_DAY:
406
  error_message = "Today's maximum number of replies has been reached. Please try again tomorrow."
407
  return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text="Generate",loading_text=loading_text)
408
+ error_message = "There are currently {} requests being processed. If you want to queue, please write your original topic and click the Continue button and you will enter the queue.".format(queue_len)
409
  new_state = "continue"
410
  new_button_text = "Continue"
411
  response.set_cookie(key="state", value=new_state)
412
+ return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text=new_button_text,loading_text=f"Generating content, Usually takes {(queue_len+1)*3}-{(queue_len+1)*4} minutes, please wait...")
413
 
414
+ queue.put([user_id,topic])
415
  new_state = "generate"
416
  new_button_text = "Generate"
417
  response.set_cookie(key="state", value=new_state)
418
  queue_len = queue.qsize()
419
 
420
  # 判断当前是否轮到该用户,如果没轮到则一直等待到轮到为止
421
+ while queue.queue[0] != [user_id,topic]:
422
+ time.sleep(10)
423
  continue
424
 
425
  with lock: