kwabs22
commited on
Commit
•
6af00ba
1
Parent(s):
74d7e67
Debugging subprocess to get the space to not need restart after a day
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import subprocess
|
4 |
import time
|
5 |
|
@@ -13,36 +13,53 @@ def generate_response(user_message): #generate_response_token_by_token
|
|
13 |
]
|
14 |
|
15 |
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
|
|
|
|
|
16 |
|
17 |
start_time = time.time()
|
18 |
alltokens = ""
|
19 |
token_buffer = ''
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
47 |
|
48 |
def custom_generate_response(cust_user_message):
|
|
|
1 |
import gradio as gr
|
2 |
+
import psutil
|
3 |
import subprocess
|
4 |
import time
|
5 |
|
|
|
13 |
]
|
14 |
|
15 |
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
|
16 |
+
process_monitor = psutil.Process(process.pid)
|
17 |
+
|
18 |
|
19 |
start_time = time.time()
|
20 |
alltokens = ""
|
21 |
token_buffer = ''
|
22 |
+
try:
|
23 |
+
while True:
|
24 |
+
# Read one character at a time
|
25 |
+
char = process.stdout.read(1)
|
26 |
+
if char == '' and process.poll() is not None:
|
27 |
+
break
|
28 |
+
if char != '':
|
29 |
+
token_buffer += char
|
30 |
+
if char == ' ' or char == '\n': # Token delimiters
|
31 |
+
elapsed_time = time.time() - start_time # Calculate elapsed time
|
32 |
+
alltokens += token_buffer
|
33 |
+
yield f"{alltokens} \n\n [Inference time: {elapsed_time:.2f} seconds]"
|
34 |
+
token_buffer = '' # Reset token buffer
|
35 |
+
# Log resource usage every minute
|
36 |
+
if time.time() - start_time > 60:
|
37 |
+
cpu_usage = process_monitor.cpu_percent()
|
38 |
+
memory_usage = process_monitor.memory_info().rss # in bytes
|
39 |
+
print(f"Subprocess CPU Usage: {cpu_usage}%, Memory Usage: {memory_usage / 1024 ** 2} MB")
|
40 |
+
start_time = time.time() # Reset the timer
|
41 |
|
42 |
+
# Yield the last token if there is any
|
43 |
+
if token_buffer:
|
44 |
+
elapsed_time = time.time() - start_time # Calculate elapsed time
|
45 |
+
alltokens += token_buffer
|
46 |
+
yield f"{alltokens} \n\n [Inference time: {elapsed_time:.2f} seconds]"
|
47 |
+
finally:
|
48 |
+
try:
|
49 |
+
# Wait for the process to complete, with a timeout
|
50 |
+
process.wait(timeout=60) # Timeout in seconds
|
51 |
+
except subprocess.TimeoutExpired:
|
52 |
+
print("Process didn't complete within the timeout. Killing it.")
|
53 |
+
process.kill()
|
54 |
+
process.wait() # Ensure proper cleanup
|
55 |
+
# Wait for the subprocess to finish if it hasn't already
|
56 |
+
process.stdout.close()
|
57 |
+
process.stderr.close()
|
58 |
|
59 |
+
# Check for any errors
|
60 |
+
if process.returncode != 0:
|
61 |
+
error_message = process.stderr.read()
|
62 |
+
print(f"Error: {error_message}")
|
63 |
|
64 |
|
65 |
def custom_generate_response(cust_user_message):
|