LucaVivona commited on
Commit
dfab724
β€’
1 Parent(s): f1637d7

backend refactors

Browse files
backend/app.py CHANGED
@@ -2,6 +2,11 @@ from flask import Flask, jsonify, request
2
  from flask_cors import CORS
3
  import socket
4
  import argparse
 
 
 
 
 
5
 
6
  app = Flask(__name__)
7
  CORS(app)
@@ -15,13 +20,53 @@ def portConnection(port : int):
15
  return False
16
 
17
 
18
- global visable, watcher, dog
 
19
  visable = []
 
20
 
21
  @app.route("/")
22
  def Home():
23
  return jsonify({"message" :"everything is up amd running... πŸš€",})
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  @app.route("/api/append/port" , methods=["POST"])
26
  def append_port():
27
  current = request.json
@@ -37,6 +82,15 @@ def remove_port():
37
  "ports" : current['port']})
38
 
39
 
 
 
 
 
 
 
 
 
 
40
  @app.route("/api/open/ports", methods=["GET"])
41
  def open_ports():
42
  return jsonify(visable)
@@ -47,4 +101,6 @@ if __name__ == "__main__":
47
  parser = argparse.ArgumentParser()
48
  parser.add_argument("-p", "--port", help="location of flask api port on local host", default=5000)
49
  args = parser.parse_args()
50
- app.run(host="0.0.0.0", port=args.port, debug=True)
 
 
 
2
  from flask_cors import CORS
3
  import socket
4
  import argparse
5
+ import src
6
+ import importlib
7
+ import asyncio
8
+ from multiprocessing import Process
9
+ import gradio as gr
10
 
11
  app = Flask(__name__)
12
  CORS(app)
 
20
  return False
21
 
22
 
23
+ global visable
24
+ global process_map
25
  visable = []
26
+ process_map = {}
27
 
28
  @app.route("/")
29
  def Home():
30
  return jsonify({"message" :"everything is up amd running... πŸš€",})
31
 
32
+ # def IS_STREAMABLE():
33
+ # pass
34
+
35
+ # async def subprocess(metadata):
36
+ # # fetch the module
37
+
38
+ # try :
39
+ # DONT_LISTEN_TO = ['GradioModule', 'register']
40
+ # mode = importlib.import_module(metadata)
41
+ # fn, names = [], []
42
+ # for fn_key in dir(mode):
43
+ # # print(fn_key, type(getattr(mode, fn_key)))
44
+ # attr = getattr(mode, fn_key)
45
+ # # p = print(dir(attr)) if fn_key == "Hello_World" else None
46
+ # if callable(attr) and not fn_key.startswith("__") and "__decorator__" in dir(attr) and attr.__decorator__ == "__gradio__":
47
+ # fn.append((getattr(mode, fn_key)))
48
+ # names.append(getattr(mode, fn_key)().__name__)
49
+ # print(fn, names)
50
+ # except Exception as e:
51
+ # print(e)
52
+ # raise e
53
+
54
+
55
+ # asyncio.create_task(src.resources.module.tabularGradio(fn, names, listen=2000, metadata=metadata, name=metadata.split('.')[-1]))
56
+ # return
57
+
58
+ # @app.route("/api/append/module" , methods=["POST"])
59
+ # def append_module():
60
+
61
+ # current = request.json
62
+ # # task_cb = Process(target=subprocess, args=(current['module'],))
63
+ # asyncio.run(subprocess(current['module']))
64
+ # # run module until
65
+ # return jsonify({"executed" : True,
66
+ # "process" : "ended" })
67
+
68
+
69
+
70
  @app.route("/api/append/port" , methods=["POST"])
71
  def append_port():
72
  current = request.json
 
82
  "ports" : current['port']})
83
 
84
 
85
+ # @app.route("/api/remove/module" , methods=["POST"])
86
+ # def remove_module():
87
+ # current = request.json
88
+
89
+ # visable.remove(current)
90
+ # process_map[current["kwargs"]["metadata"]].terminate()
91
+ # return jsonify({"executed" : True,
92
+ # "ports" : current['port']})
93
+
94
  @app.route("/api/open/ports", methods=["GET"])
95
  def open_ports():
96
  return jsonify(visable)
 
101
  parser = argparse.ArgumentParser()
102
  parser.add_argument("-p", "--port", help="location of flask api port on local host", default=5000)
103
  args = parser.parse_args()
104
+ app.run(host="0.0.0.0", port=args.port, debug=True)
105
+
106
+
backend/src/__init__.py CHANGED
@@ -1,4 +1,3 @@
1
- import examples
2
- import resource
3
- import src.demoF
4
- import src.demoC
 
1
+ from .example import *
2
+ import src.resources
3
+ from src.demo import *
 
backend/src/demo/demo.py CHANGED
@@ -2,7 +2,7 @@ import argparse
2
  import sys
3
 
4
  sys.path.insert(0, "../")
5
- from resources import GradioModule, register, InterLauncher, tabularGradio
6
  from example.examples import Pictionary, FSD, stock_forecast
7
 
8
  import gradio as gr
 
2
  import sys
3
 
4
  sys.path.insert(0, "../")
5
+ from src.resources import GradioModule, register, InterLauncher, tabularGradio
6
  from example.examples import Pictionary, FSD, stock_forecast
7
 
8
  import gradio as gr
backend/src/demo/module.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.insert(0, "../")
3
+ from src.resources import GradioModule, register
4
+
5
+ @register(["text"], ["text"], examples=[["Luca Vivona"]])
6
+ def Hello_World(name):
7
+ return f"πŸ‘‹ Hello {name}, and welcome to Gradio Flow πŸ€—"
8
+
9
+ @register(["number", "number"], ["number"], examples=[[1,1]])
10
+ def add(x, y):
11
+ return x + y
backend/src/example/__init__.py CHANGED
@@ -1 +1 @@
1
- from .examples import *
 
1
+ from .examples import *
backend/src/example/examples.py CHANGED
@@ -7,9 +7,8 @@ import torch
7
  from torch import nn
8
  import numpy as np
9
  import PIL
10
-
11
  sys.path.insert(0, "../resources")
12
- from resources.module import GradioModule, register
13
 
14
 
15
  @GradioModule
 
7
  from torch import nn
8
  import numpy as np
9
  import PIL
 
10
  sys.path.insert(0, "../resources")
11
+ from src.resources.module import GradioModule, register
12
 
13
 
14
  @GradioModule
backend/src/resources/module.py CHANGED
@@ -337,7 +337,6 @@ def GradioModule(cls):
337
  except Exception:
338
  print(f"**{bcolor.BOLD}{bcolor.FAIL}CONNECTION ERROR{bcolor.ENDC}** πŸ›The api either lost connection or was turned off...πŸ›")
339
  return
340
-
341
  return Decorator
342
 
343
  # console colour changer
 
337
  except Exception:
338
  print(f"**{bcolor.BOLD}{bcolor.FAIL}CONNECTION ERROR{bcolor.ENDC}** πŸ›The api either lost connection or was turned off...πŸ›")
339
  return
 
340
  return Decorator
341
 
342
  # console colour changer
backend/test/app_test.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import requests
2
+ DOCKER_LOCAL_HOST = '0.0.0.0'
3
+ import gradio as gr
4
+ import asyncio
5
+ if __name__ == "__main__":
6
+ gr.close_all()
7
+ requests.post(f"http://{DOCKER_LOCAL_HOST}:{ 2000 }/api/append/module", json={"module" : "src.demo.module"})