Update app.py
Browse files
app.py
CHANGED
@@ -8,48 +8,27 @@ import PIL
|
|
8 |
from pathlib import Path
|
9 |
import gradio as gr
|
10 |
import numpy as np
|
11 |
-
import requests
|
12 |
-
from io import BytesIO
|
13 |
-
from PIL import Image
|
14 |
|
15 |
-
|
16 |
img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
|
17 |
|
18 |
|
19 |
-
def
|
20 |
-
|
21 |
-
img = Image.open(BytesIO(response.content))
|
22 |
-
return img
|
23 |
|
24 |
-
|
25 |
-
if img_url:
|
26 |
-
img = load_image_from_url(img_url)
|
27 |
-
img = np.array(img)
|
28 |
-
output = img_colorization(img[..., ::-1])
|
29 |
result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
|
|
|
30 |
temp_dir = tempfile.mkdtemp()
|
31 |
out_path = os.path.join(temp_dir, 'old-to-color.png')
|
32 |
cv2.imwrite(out_path, result)
|
33 |
-
|
34 |
-
files = {'file': open(out_path, 'rb')}
|
35 |
-
response = requests.post(upload_url, files=files)
|
36 |
-
files.close()
|
37 |
-
image_url = response.json()['url'] # رابط الصورة المحملة
|
38 |
|
39 |
-
return Path(out_path), image_url
|
40 |
|
41 |
title = "Color Restorization Model"
|
42 |
-
|
43 |
inference,
|
44 |
-
inputs=
|
45 |
-
|
46 |
-
gr.inputs.Textbox(placeholder="Enter Image URL (optional)", label="Image URL (optional)")
|
47 |
-
],
|
48 |
-
outputs=[
|
49 |
-
gr.outputs.Image(type="pil", label="Output Image"),
|
50 |
-
gr.outputs.Textbox(label="Download Link")
|
51 |
-
],
|
52 |
title=title
|
53 |
-
)
|
54 |
-
|
55 |
-
interface.launch(enable_queue=True)
|
|
|
8 |
from pathlib import Path
|
9 |
import gradio as gr
|
10 |
import numpy as np
|
|
|
|
|
|
|
11 |
|
12 |
+
"""Load the model into memory to make running multiple predictions efficient"""
|
13 |
img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
|
14 |
|
15 |
|
16 |
+
def inference(img):
|
17 |
+
image = cv2.imread(str(img))
|
|
|
|
|
18 |
|
19 |
+
output = img_colorization(image[..., ::-1])
|
|
|
|
|
|
|
|
|
20 |
result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
|
21 |
+
|
22 |
temp_dir = tempfile.mkdtemp()
|
23 |
out_path = os.path.join(temp_dir, 'old-to-color.png')
|
24 |
cv2.imwrite(out_path, result)
|
25 |
+
return Path(out_path)
|
|
|
|
|
|
|
|
|
26 |
|
|
|
27 |
|
28 |
title = "Color Restorization Model"
|
29 |
+
gr.Interface(
|
30 |
inference,
|
31 |
+
[gr.inputs.Image(type="filepath", label="Input")],
|
32 |
+
gr.outputs.Image(type="pil", label="Output"),
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
title=title
|
34 |
+
).launch(enable_queue=True)
|
|
|
|