jianghuyihei commited on
Commit
dc15d27
1 Parent(s): ced8510
Files changed (2) hide show
  1. __pycache__/app.cpython-310.pyc +0 -0
  2. app.py +12 -13
__pycache__/app.cpython-310.pyc CHANGED
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
 
app.py CHANGED
@@ -11,22 +11,17 @@ import threading
11
  import logging
12
  from queue import Queue
13
  import json
14
- import uuid
15
  import yaml
16
  import os
17
 
18
- # with open('/Users/jianghuyihei/code/MultiResearchAgent/config.yaml', 'r') as file:
19
- # config = yaml.safe_load(file)
20
- # for key, value in config.items():
21
- # os.environ[key] = str(value)
22
-
23
  lock = threading.Lock()
24
  app = FastAPI()
25
 
 
26
  # 每日最大回复次数
27
  MAX_REPLIES_PER_DAY = 500
28
  # 当日回复次数计数器
29
- reply_count = 189
30
  # 启动时设置计数器重置
31
  last_reset_time = datetime.now()
32
 
@@ -355,7 +350,7 @@ html_template = """
355
  {{ script}}
356
  </script>
357
  <script>
358
- const socket = new WebSocket('wss://damo-nlp-sg-coi-agent.hf.space/ws');
359
 
360
  socket.addEventListener('open', function (event) {
361
  const userId = document.getElementById("user_id").value;
@@ -420,6 +415,7 @@ queue = Queue()
420
 
421
  @app.websocket("/ws")
422
  async def websocket_endpoint(websocket: WebSocket):
 
423
  await websocket.accept()
424
  print("WebSocket connection established.")
425
  try:
@@ -428,14 +424,15 @@ async def websocket_endpoint(websocket: WebSocket):
428
  message = json.loads(data)
429
  user_id = message.get("user_id")
430
  action = message.get("action")
431
- print(action)
432
  if action == "disconnect":
433
- for item in list(queue.queue):
 
 
434
  if item == user_id:
435
  queue.queue.remove(item)
 
436
  break
437
- print(f"User {user_id} disconnected.")
438
-
439
  except WebSocketDisconnect:
440
  print("WebSocket connection closed.")
441
 
@@ -450,6 +447,7 @@ def form_get(request: Request):
450
  @app.post("/", response_class=HTMLResponse)
451
  def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...), state: str = Form(...)):
452
  global reply_count
 
453
  start_time = time.time()
454
  client_ip = request.client.host
455
  if user_id == "":
@@ -500,7 +498,7 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
500
  print(queue.queue[0], [user_id,topic])
501
  while queue.queue[0] != user_id:
502
  # 检查用户是否还在队列中
503
- if not any(user_id == item[0] for item in queue.queue):
504
  return Template(html_template).render(
505
  idea="",
506
  error="Request was cancelled.",
@@ -514,6 +512,7 @@ def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...),
514
 
515
  try:
516
  with lock:
 
517
  logging.info(f"Processing request for topic: {topic}")
518
  start_time = time.time()
519
  error_message = None
 
11
  import logging
12
  from queue import Queue
13
  import json
 
14
  import yaml
15
  import os
16
 
 
 
 
 
 
17
  lock = threading.Lock()
18
  app = FastAPI()
19
 
20
+ current_user = None
21
  # 每日最大回复次数
22
  MAX_REPLIES_PER_DAY = 500
23
  # 当日回复次数计数器
24
+ reply_count = 0
25
  # 启动时设置计数器重置
26
  last_reset_time = datetime.now()
27
 
 
350
  {{ script}}
351
  </script>
352
  <script>
353
+ const socket = new WebSocket("ws://localhost:7860/ws");
354
 
355
  socket.addEventListener('open', function (event) {
356
  const userId = document.getElementById("user_id").value;
 
415
 
416
  @app.websocket("/ws")
417
  async def websocket_endpoint(websocket: WebSocket):
418
+ global current_user
419
  await websocket.accept()
420
  print("WebSocket connection established.")
421
  try:
 
424
  message = json.loads(data)
425
  user_id = message.get("user_id")
426
  action = message.get("action")
427
+ print(user_id, action)
428
  if action == "disconnect":
429
+ if user_id == current_user:
430
+ continue
431
+ for item in queue.queue:
432
  if item == user_id:
433
  queue.queue.remove(item)
434
+ print(f"User {user_id} disconnected.")
435
  break
 
 
436
  except WebSocketDisconnect:
437
  print("WebSocket connection closed.")
438
 
 
447
  @app.post("/", response_class=HTMLResponse)
448
  def form_post(request: Request,topic: str = Form(...), user_id: str = Form(...), state: str = Form(...)):
449
  global reply_count
450
+ global current_user
451
  start_time = time.time()
452
  client_ip = request.client.host
453
  if user_id == "":
 
498
  print(queue.queue[0], [user_id,topic])
499
  while queue.queue[0] != user_id:
500
  # 检查用户是否还在队列中
501
+ if not any(user_id == item for item in queue.queue):
502
  return Template(html_template).render(
503
  idea="",
504
  error="Request was cancelled.",
 
512
 
513
  try:
514
  with lock:
515
+ current_user = user_id
516
  logging.info(f"Processing request for topic: {topic}")
517
  start_time = time.time()
518
  error_message = None