oscarwang2 commited on
Commit
9c660b1
1 Parent(s): 09550d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -8,29 +8,37 @@ import logging
8
  import json
9
  import threading
10
  import psutil
 
11
 
12
  # Configure logging
13
  logging.basicConfig(level=logging.INFO)
14
  logger = logging.getLogger(__name__)
15
 
 
 
 
16
  connected_cpus = {}
17
 
18
- # Function to donate CPU (Workaround)
19
- def donate_cpu_handler(data):
 
 
20
  host = data['host']
21
  cpu_count = data['cpu_count']
22
  connected_cpus[host] = {"cpu_count": cpu_count, "usage": 0.0}
23
  logger.info(f"CPU donated by {host} with {cpu_count} CPUs.")
24
- return json.dumps({"status": "success", "message": f"CPU donated by {host}"})
25
 
26
- # Function to update CPU usage (Workaround)
27
- def update_cpu_usage_handler(data):
 
 
28
  host = data['host']
29
  usage = data['usage']
30
  if host in connected_cpus:
31
  connected_cpus[host]['usage'] = usage
32
  logger.info(f"Updated CPU usage for {host}: {usage}%")
33
- return json.dumps({"status": "success"})
34
 
35
  # Function to run the provided Python script using MPI
36
  def run_script(script_name, folder_path):
@@ -103,6 +111,8 @@ def gradio_interface():
103
 
104
  iface.launch()
105
 
106
- # Launch the Gradio interface
107
  if __name__ == "__main__":
108
  gradio_interface()
 
 
 
8
  import json
9
  import threading
10
  import psutil
11
+ from flask import Flask, request, jsonify
12
 
13
  # Configure logging
14
  logging.basicConfig(level=logging.INFO)
15
  logger = logging.getLogger(__name__)
16
 
17
+ # Initialize Flask app
18
+ app = Flask(__name__)
19
+
20
  connected_cpus = {}
21
 
22
+ # Endpoint to donate CPU resources
23
+ @app.route('/donate_cpu', methods=['POST'])
24
+ def donate_cpu_handler():
25
+ data = request.get_json()
26
  host = data['host']
27
  cpu_count = data['cpu_count']
28
  connected_cpus[host] = {"cpu_count": cpu_count, "usage": 0.0}
29
  logger.info(f"CPU donated by {host} with {cpu_count} CPUs.")
30
+ return jsonify({"status": "success", "message": f"CPU donated by {host}"})
31
 
32
+ # Endpoint to update CPU usage
33
+ @app.route('/update_cpu_usage', methods=['POST'])
34
+ def update_cpu_usage_handler():
35
+ data = request.get_json()
36
  host = data['host']
37
  usage = data['usage']
38
  if host in connected_cpus:
39
  connected_cpus[host]['usage'] = usage
40
  logger.info(f"Updated CPU usage for {host}: {usage}%")
41
+ return jsonify({"status": "success"})
42
 
43
  # Function to run the provided Python script using MPI
44
  def run_script(script_name, folder_path):
 
111
 
112
  iface.launch()
113
 
114
+ # Launch the Gradio interface using Flask's run method
115
  if __name__ == "__main__":
116
  gradio_interface()
117
+ # Uncomment the line below if using Flask's run method for local testing
118
+ # app.run(host='0.0.0.0', port=7860)