fountai commited on
Commit
9121ab1
·
1 Parent(s): aae6316
Files changed (2) hide show
  1. app.py +26 -34
  2. requirements.txt +2 -1
app.py CHANGED
@@ -12,8 +12,8 @@ import os
12
  import requests
13
  from modules.audio import convert, get_audio_duration
14
  from modules.r2 import upload_to_s3
 
15
  import threading
16
- import queue
17
 
18
  vpv_webhook = os.environ.get("VPV_WEBHOOK")
19
 
@@ -73,43 +73,35 @@ class ProcessRequest(BaseModel):
73
  crossfade: float = 0.1
74
 
75
 
76
- q = queue.Queue()
77
-
78
- def process_queue(q):
79
- while True:
80
- try:
81
- key, censor, offset, text, format, speed, crossfade, id, receiver, webhook = q.get(timeout=5)
82
-
83
- # Processamento do item
84
- audio = generate_audio(key, text, censor, offset, speed=speed, crossfade=crossfade)
85
- convertedAudioPath = convert(audio, format)
86
- duration = get_audio_duration(convertedAudioPath)
87
- audioUrl = upload_to_s3(convertedAudioPath, f"{id}", format)
88
 
89
- os.remove(audio)
90
- os.remove(convertedAudioPath)
 
 
91
 
92
- payload = {
93
- "id": id,
94
- "duration": duration,
95
- "receiver": receiver,
96
- "url": audioUrl
97
- }
98
 
99
- requests.post(webhook, json=payload)
 
 
 
 
 
100
 
101
- except queue.Empty:
102
- print("Queue timeout: no items to process.")
103
- break
104
- except Exception as e:
105
- print(f"Error processing item: {e}")
106
- else:
107
- # Confirma que a tarefa foi concluída com sucesso
108
- q.task_done()
109
-
110
- worker_thread = threading.Thread(target=process_queue, args=(q,))
111
- worker_thread.start()
112
-
113
  @app.post("/process")
114
  def process_audio(payload: ProcessRequest):
115
  key = payload.key
 
12
  import requests
13
  from modules.audio import convert, get_audio_duration
14
  from modules.r2 import upload_to_s3
15
+ from wakaq import Queue, Worker
16
  import threading
 
17
 
18
  vpv_webhook = os.environ.get("VPV_WEBHOOK")
19
 
 
73
  crossfade: float = 0.1
74
 
75
 
76
+ def process_queue(item):
77
+ key, censor, offset, text, format, speed, crossfade, id, receiver, webhook = item
 
 
 
 
 
 
 
 
 
 
78
 
79
+ audio = generate_audio(key, text, censor, offset, speed=speed, crossfade=crossfade)
80
+ convertedAudioPath = convert(audio, format)
81
+ duration = get_audio_duration(convertedAudioPath)
82
+ audioUrl = upload_to_s3(convertedAudioPath, f"{id}", format)
83
 
84
+ os.remove(audio)
85
+ os.remove(convertedAudioPath)
 
 
 
 
86
 
87
+ payload = {
88
+ "id": id,
89
+ "duration": duration,
90
+ "receiver": receiver,
91
+ "url": audioUrl
92
+ }
93
 
94
+ requests.post(webhook, json=payload)
95
+
96
+ queue = Queue()
97
+ worker = Worker(queue, process_queue, concurrency=8)
98
+ def start_worker():
99
+ worker.run()
100
+
101
+ thread = threading.Thread(target=start_worker)
102
+ thread.daemon = True
103
+ thread.start()
104
+
 
105
  @app.post("/process")
106
  def process_audio(payload: ProcessRequest):
107
  key = payload.key
requirements.txt CHANGED
@@ -15,4 +15,5 @@ peft
15
  spaces
16
  torch
17
  transformers
18
- xformers
 
 
15
  spaces
16
  torch
17
  transformers
18
+ xformers
19
+ wakaq