alessandro trinca tornidor commited on
Commit
9e6aa30
1 Parent(s): 9190e8f

[refactor] porting changes from https://huggingface.co/spaces/aletrn/music-separation

Browse files
README.md CHANGED
@@ -4,8 +4,8 @@ emoji: 🌖
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.28.3
8
- app_file: app_gradio_fastapi/main.py
9
  pinned: false
10
  license: mit
11
  ---
 
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.32.2
8
+ app_file: app.py
9
  pinned: false
10
  license: mit
11
  ---
app_gradio_fastapi/main.py → app.py RENAMED
@@ -1,28 +1,35 @@
1
  import logging
 
2
  import gradio as gr
 
3
  from fastapi import FastAPI
4
 
5
- from app_gradio_fastapi import routes
6
- from app_gradio_fastapi.helpers.formatters import request_formatter
7
- from app_gradio_fastapi.helpers.session_logger import change_logging
8
 
9
 
10
- change_logging()
 
 
 
 
11
 
12
 
13
- logging.info("creating FastAPI app...")
14
  CUSTOM_GRADIO_PATH = "/"
15
- app = FastAPI(title="logging_app", version="1.0")
16
- app.include_router(routes.router)
17
- logging.info("FastAPI app created, creating gradio app...")
18
  io = gr.Interface(
19
- request_formatter,
20
  inputs=[
21
- gr.Textbox(lines=1, placeholder=10, label="write a number to divide 100 (0 will raise division by zero error)"),
22
  ],
23
  outputs=[
24
  gr.Textbox(lines=1, placeholder=None, label="Text Output"),
25
  ],
 
26
  )
 
27
  app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
28
  logging.info("gradio app mounted")
 
 
 
 
 
1
  import logging
2
+
3
  import gradio as gr
4
+ import uvicorn
5
  from fastapi import FastAPI
6
 
7
+ import routes
8
+ from helpers import formatters, session_logger
 
9
 
10
 
11
+ session_logger.change_logging()
12
+ app = FastAPI(title="gradio-with-fastapi", version="1.0")
13
+ logging.info("FastAPI app created, including routes...")
14
+ app.include_router(routes.router)
15
+ logging.info("routes included, creating gradio app")
16
 
17
 
 
18
  CUSTOM_GRADIO_PATH = "/"
 
 
 
19
  io = gr.Interface(
20
+ formatters.request_formatter,
21
  inputs=[
22
+ gr.Textbox(lines=1, placeholder="10", label="write a number to divide 100; 0 will raise ZeroDivisionError"),
23
  ],
24
  outputs=[
25
  gr.Textbox(lines=1, placeholder=None, label="Text Output"),
26
  ],
27
+ title="gradio with fastapi...",
28
  )
29
+ logging.info("mounting gradio app within FastAPI...")
30
  app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
31
  logging.info("gradio app mounted")
32
+
33
+
34
+ if __name__ == '__main__':
35
+ uvicorn.run(app, host="0.0.0.0", port=7860)
app_gradio_fastapi/helpers/__init__.py DELETED
File without changes
{app_gradio_fastapi → helpers}/__init__.py RENAMED
File without changes
{app_gradio_fastapi/helpers → helpers}/formatters.py RENAMED
@@ -1,7 +1,7 @@
1
  import logging
2
  from fastapi import HTTPException
3
 
4
- from app_gradio_fastapi.helpers import session_logger
5
 
6
 
7
  def divide(a: int, b: int) -> float:
 
1
  import logging
2
  from fastapi import HTTPException
3
 
4
+ from helpers import session_logger
5
 
6
 
7
  def divide(a: int, b: int) -> float:
{app_gradio_fastapi/helpers → helpers}/session_logger.py RENAMED
@@ -5,23 +5,28 @@ from typing import Callable
5
 
6
  logging_uuid = contextvars.ContextVar("uuid")
7
  formatter = '%(asctime)s | %(uuid)s [%(pathname)s:%(module)s %(lineno)d] %(levelname)s | %(message)s'
 
8
 
9
 
10
  loggingType = logging.CRITICAL | logging.ERROR | logging.WARNING | logging.INFO | logging.DEBUG
11
 
12
 
13
- def change_logging(level_log: loggingType = logging.INFO) -> None:
14
- old_factory = logging.getLogRecordFactory()
 
15
 
16
- def record_factory(*args, **kwargs):
17
- record = old_factory(*args, **kwargs)
18
- record.uuid = logging_uuid.get("uuid")
19
- if isinstance(record.msg, str):
20
- record.msg = record.msg.replace("\\", "\\\\").replace("\n", "\\n")
21
- return record
22
 
23
- logging.setLogRecordFactory(record_factory)
24
- logging.basicConfig(level=level_log, format=formatter, force=True)
 
 
 
25
 
26
 
27
  def set_uuid_logging(func: Callable) -> Callable:
 
5
 
6
  logging_uuid = contextvars.ContextVar("uuid")
7
  formatter = '%(asctime)s | %(uuid)s [%(pathname)s:%(module)s %(lineno)d] %(levelname)s | %(message)s'
8
+ formatter_without_uuid = '%(asctime)s | [%(pathname)s:%(module)s %(lineno)d] %(levelname)s | %(message)s'
9
 
10
 
11
  loggingType = logging.CRITICAL | logging.ERROR | logging.WARNING | logging.INFO | logging.DEBUG
12
 
13
 
14
+ def change_logging(level_log: loggingType = logging.INFO, formatter_log: str = formatter) -> None:
15
+ try:
16
+ old_factory = logging.getLogRecordFactory()
17
 
18
+ def record_factory(*args, **kwargs):
19
+ record = old_factory(*args, **kwargs)
20
+ record.uuid = logging_uuid.get("uuid")
21
+ if isinstance(record.msg, str):
22
+ record.msg = record.msg.replace("\\", "\\\\").replace("\n", "\\n")
23
+ return record
24
 
25
+ logging.setLogRecordFactory(record_factory)
26
+ except Exception as e:
27
+ print(f"change_logging exception:{e}, reset logging...")
28
+ formatter_log = formatter_without_uuid
29
+ logging.basicConfig(level=level_log, format=formatter_log, force=True)
30
 
31
 
32
  def set_uuid_logging(func: Callable) -> Callable:
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
  fastapi==0.110.2
2
- gradio==4.28.3
 
1
  fastapi==0.110.2
2
+ gradio==4.32.2
app_gradio_fastapi/routes.py → routes.py RENAMED
@@ -3,7 +3,7 @@ import logging
3
 
4
  from fastapi import APIRouter
5
 
6
- from app_gradio_fastapi.helpers import session_logger
7
 
8
 
9
  router = APIRouter()
@@ -14,7 +14,7 @@ router = APIRouter()
14
  def health() -> str:
15
  try:
16
  logging.info("health check")
17
- return json.dumps({"msg": "ok"})
18
  except Exception as e:
19
  logging.error(f"exception:{e}.")
20
  return json.dumps({"msg": "request failed"})
 
3
 
4
  from fastapi import APIRouter
5
 
6
+ from helpers import session_logger
7
 
8
 
9
  router = APIRouter()
 
14
  def health() -> str:
15
  try:
16
  logging.info("health check")
17
+ return json.dumps({"msg": "still alive..."})
18
  except Exception as e:
19
  logging.error(f"exception:{e}.")
20
  return json.dumps({"msg": "request failed"})