Rafuwhwhs commited on
Commit
25479bb
·
verified ·
1 Parent(s): c41f38f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import tempfile
4
+ from modelscope.outputs import OutputKeys
5
+ from modelscope.pipelines import pipeline
6
+ from modelscope.utils.constant import Tasks
7
+ 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
+ # Load the model into memory to make running multiple predictions efficien
16
+ img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
17
+
18
+
19
+ def load_image_from_url(url):
20
+ response = requests.get(url)
21
+ img = Image.open(BytesIO(response.content))
22
+ return img
23
+
24
+ def inference(img, img_url=None):
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
+ upload_url = "https://api.postimages.org/upload"
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
+ interface = gr.Interface(
43
+ inference,
44
+ inputs=[
45
+ gr.inputs.Image(type="pil", label="Input Image"),
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)