kwabs22 commited on
Commit
6af00ba
1 Parent(s): 74d7e67

Debugging subprocess to get the space to not need restart after a day

Browse files
Files changed (1) hide show
  1. app.py +42 -25
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- import random
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
- while True:
21
- # Read one character at a time
22
- char = process.stdout.read(1)
23
- if char == '' and process.poll() is not None:
24
- break
25
- if char != '':
26
- token_buffer += char
27
- if char == ' ' or char == '\n': # Token delimiters
28
- elapsed_time = time.time() - start_time # Calculate elapsed time
29
- alltokens += token_buffer
30
- yield f"{alltokens} \n\n [Inference time: {elapsed_time:.2f} seconds]"
31
- token_buffer = '' # Reset token buffer
 
 
 
 
 
 
 
32
 
33
- # Yield the last token if there is any
34
- if token_buffer:
35
- elapsed_time = time.time() - start_time # Calculate elapsed time
36
- alltokens += token_buffer
37
- yield f"{alltokens} \n\n [Inference time: {elapsed_time:.2f} seconds]"
38
-
39
- # Wait for the subprocess to finish if it hasn't already
40
- process.wait()
 
 
 
 
 
 
 
 
41
 
42
- # Check for any errors
43
- if process.returncode != 0:
44
- error_message = process.stderr.read()
45
- print(f"Error: {error_message}")
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):