Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,44 +4,55 @@ from __future__ import annotations
|
|
4 |
|
5 |
import gradio as gr
|
6 |
import torch
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
from preprocessor import Preprocessor
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
)
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
import gradio as gr
|
6 |
import torch
|
7 |
+
import uvicorn
|
8 |
+
from fastapi import FastAPI, File, UploadFile
|
9 |
+
from fastapi.responses import HTMLResponse
|
10 |
+
from io import BytesIO
|
11 |
+
import numpy as np
|
12 |
+
import PIL.Image
|
13 |
from preprocessor import Preprocessor
|
14 |
|
15 |
+
app = FastAPI()
|
16 |
+
preprocessor = Preprocessor()
|
17 |
+
|
18 |
+
def apply_preprocessor(input_image, preprocessor_name):
|
19 |
+
preprocessor.load(preprocessor_name)
|
20 |
+
processed_image = preprocessor(input_image)
|
21 |
+
return processed_image
|
22 |
+
|
23 |
+
@app.post("/preprocess/")
|
24 |
+
async def preprocess_image(file: UploadFile = File(...), preprocessor_name: str = 'Canny'):
|
25 |
+
contents = await file.read()
|
26 |
+
image = PIL.Image.open(BytesIO(contents))
|
27 |
+
processed_image = apply_preprocessor(image, preprocessor_name)
|
28 |
+
processed_image_pil = PIL.Image.fromarray(processed_image)
|
29 |
+
buffered = BytesIO()
|
30 |
+
processed_image_pil.save(buffered, format="JPEG")
|
31 |
+
return {"image": buffered.getvalue()}
|
32 |
+
|
33 |
+
@app.get("/")
|
34 |
+
async def main():
|
35 |
+
content = """
|
36 |
+
<body>
|
37 |
+
<form action="/preprocess/" enctype="multipart/form-data" method="post">
|
38 |
+
<input name="file" type="file">
|
39 |
+
<select name="preprocessor_name">
|
40 |
+
<option value="Canny">Canny</option>
|
41 |
+
<option value="Midas">Midas</option>
|
42 |
+
<option value="MLSD">MLSD</option>
|
43 |
+
<!-- Add more options as needed -->
|
44 |
+
</select>
|
45 |
+
<input type="submit">
|
46 |
+
</form>
|
47 |
+
</body>
|
48 |
+
"""
|
49 |
+
return HTMLResponse(content)
|
50 |
+
|
51 |
+
def apply_gradio_preprocessor(input_image, preprocessor_name):
|
52 |
+
preprocessor.load(preprocessor_name)
|
53 |
+
processed_image = preprocessor(input_image)
|
54 |
+
return processed_image
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
gr.Interface(apply_gradio_preprocessor, ["image", "text"], "image")\
|
58 |
+
.launch()
|