__init__.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from ..errors import MissingRequirementsError
  2. try:
  3. from .server.app import app
  4. from .server.website import Website
  5. from .server.backend import Backend_Api
  6. except ImportError:
  7. raise MissingRequirementsError('Install "flask" package for the gui')
  8. def run_gui(host: str = '0.0.0.0', port: int = 8080, debug: bool = False) -> None:
  9. if debug:
  10. import g4f
  11. g4f.debug.logging = True
  12. config = {
  13. 'host' : host,
  14. 'port' : port,
  15. 'debug': debug
  16. }
  17. site = Website(app)
  18. for route in site.routes:
  19. app.add_url_rule(
  20. route,
  21. view_func = site.routes[route]['function'],
  22. methods = site.routes[route]['methods'],
  23. )
  24. backend_api = Backend_Api(app)
  25. for route in backend_api.routes:
  26. app.add_url_rule(
  27. route,
  28. view_func = backend_api.routes[route]['function'],
  29. methods = backend_api.routes[route]['methods'],
  30. )
  31. print(f"Running on port {config['port']}")
  32. app.run(**config)
  33. print(f"Closing port {config['port']}")