nej-dot commited on
Commit
d93e95e
·
verified ·
1 Parent(s): aef3692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -39
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
- DESCRIPTION = "# ControlNet v1.1"
11
-
12
- if not torch.cuda.is_available():
13
- DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
14
-
15
- with gr.Blocks(css="style.css") as demo:
16
- gr.Markdown(DESCRIPTION)
17
- gr.DuplicateButton(
18
- value="Duplicate Space for private use",
19
- elem_id="duplicate-button",
20
- visible=False, # Hiding the duplicate button
21
- )
22
-
23
- # Instantiate the Preprocessor class
24
- preprocessor = Preprocessor()
25
-
26
- # Function to apply preprocessor to input image
27
- def apply_preprocessor(input_image, preprocessor_name):
28
- # Load the desired preprocessor
29
- preprocessor.load(preprocessor_name)
30
- # Apply the preprocessor to the input image
31
- processed_image = preprocessor(input_image)
32
- # Convert processed image from numpy array to PIL image
33
- processed_image_pil = PIL.Image.fromarray(processed_image)
34
- return processed_image_pil
35
-
36
- # Gradio interface
37
- image = gr.inputs.Image()
38
- preprocessor_name = gr.inputs.Dropdown(choices=["Canny", "Midas", "MLSD", "Openpose", "PidiNet", "NormalBae", "Lineart", "LineartAnime", "ContentShuffle", "DPT", "UPerNet"], label="Preprocessor")
39
- run_button = gr.Button("Start")
40
- output_image = gr.outputs.Image(label="Output")
41
-
42
- # Define processing function for Gradio
43
- def process_image(input_image, preprocessor_name):
44
- return apply_preprocessor(input_image, preprocessor_name)
45
-
46
- # Link inputs and outputs to function using Gradio's Interface class
47
- gr.Interface(fn=process_image, inputs=[image, preprocessor_name], outputs=output_image, title="Preprocessor GUI").launch()
 
 
 
 
 
 
 
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()