Spaces:
Runtime error
Runtime error
Commit
·
2abb453
1
Parent(s):
96c4785
fix
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import threading
|
|
| 11 |
import logging
|
| 12 |
from queue import Queue
|
| 13 |
import json
|
|
|
|
| 14 |
|
| 15 |
lock = threading.Lock()
|
| 16 |
app = FastAPI()
|
|
@@ -455,10 +456,10 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
|
|
| 455 |
state = "generate"
|
| 456 |
|
| 457 |
script = script_template.format(user_id=user_id, state=state)
|
| 458 |
-
if user_id in queue.queue:
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
|
| 463 |
loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
|
| 464 |
if state == "generate":
|
|
@@ -467,7 +468,10 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
|
|
| 467 |
if queue_len + reply_count >= MAX_REPLIES_PER_DAY:
|
| 468 |
error_message = "Today's maximum number of replies has been reached. Please try again tomorrow."
|
| 469 |
return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text="Generate",loading_text=loading_text,script=script)
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
| 471 |
new_state = "continue"
|
| 472 |
new_button_text = "Continue"
|
| 473 |
script = f"""
|
|
@@ -495,8 +499,11 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
|
|
| 495 |
# 判断当前是否轮到该用户,如果没轮到则一直等待到轮到为止
|
| 496 |
print(queue.queue[0], [user_id,topic])
|
| 497 |
while queue.queue[0] != user_id:
|
| 498 |
-
|
| 499 |
-
if
|
|
|
|
|
|
|
|
|
|
| 500 |
return Template(html_template).render(
|
| 501 |
idea="",
|
| 502 |
error="Request was cancelled.",
|
|
@@ -505,7 +512,7 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
|
|
| 505 |
loading_text=loading_text,
|
| 506 |
script=script
|
| 507 |
)
|
| 508 |
-
time.sleep(
|
| 509 |
continue
|
| 510 |
|
| 511 |
try:
|
|
@@ -545,4 +552,6 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
|
|
| 545 |
except Exception as e:
|
| 546 |
error_message = str(e)
|
| 547 |
queue.get()
|
| 548 |
-
return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text="Generate",loading_text=loading_text,script=script)
|
|
|
|
|
|
|
|
|
| 11 |
import logging
|
| 12 |
from queue import Queue
|
| 13 |
import json
|
| 14 |
+
from collections import Counter
|
| 15 |
|
| 16 |
lock = threading.Lock()
|
| 17 |
app = FastAPI()
|
|
|
|
| 456 |
state = "generate"
|
| 457 |
|
| 458 |
script = script_template.format(user_id=user_id, state=state)
|
| 459 |
+
# if user_id in queue.queue:
|
| 460 |
+
# # 如果用户在队列中,移除用户
|
| 461 |
+
# queue.queue.remove(user_id)
|
| 462 |
+
# state = "generate"
|
| 463 |
|
| 464 |
loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
|
| 465 |
if state == "generate":
|
|
|
|
| 468 |
if queue_len + reply_count >= MAX_REPLIES_PER_DAY:
|
| 469 |
error_message = "Today's maximum number of replies has been reached. Please try again tomorrow."
|
| 470 |
return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text="Generate",loading_text=loading_text,script=script)
|
| 471 |
+
if user_id in queue.queue:
|
| 472 |
+
error_message = "You already have a request in the queue. Submitting a new request will cancel the previous request. Please confirm if you need to submit a new request. If so, click continue. There are currently {} requests being processed."
|
| 473 |
+
else:
|
| 474 |
+
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)
|
| 475 |
new_state = "continue"
|
| 476 |
new_button_text = "Continue"
|
| 477 |
script = f"""
|
|
|
|
| 499 |
# 判断当前是否轮到该用户,如果没轮到则一直等待到轮到为止
|
| 500 |
print(queue.queue[0], [user_id,topic])
|
| 501 |
while queue.queue[0] != user_id:
|
| 502 |
+
counts = Counter(queue.queue)
|
| 503 |
+
if counts[user_id] > 1:
|
| 504 |
+
while counts[user_id] > 1:
|
| 505 |
+
queue.queue.remove(user_id)
|
| 506 |
+
counts[user_id] -= 1
|
| 507 |
return Template(html_template).render(
|
| 508 |
idea="",
|
| 509 |
error="Request was cancelled.",
|
|
|
|
| 512 |
loading_text=loading_text,
|
| 513 |
script=script
|
| 514 |
)
|
| 515 |
+
time.sleep(2)
|
| 516 |
continue
|
| 517 |
|
| 518 |
try:
|
|
|
|
| 552 |
except Exception as e:
|
| 553 |
error_message = str(e)
|
| 554 |
queue.get()
|
| 555 |
+
return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text="Generate",loading_text=loading_text,script=script)
|
| 556 |
+
|
| 557 |
+
|