WaterKnight commited on
Commit
9ba5c76
1 Parent(s): 058960f

Code improvements.

Browse files
Files changed (1) hide show
  1. app.py +35 -21
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  from io import BytesIO
3
  import requests
 
4
 
5
  # Interface utilities
6
  import gradio as gr
@@ -40,6 +41,8 @@ photo_ids = pd.read_csv("unsplash-dataset/photo_ids.csv")
40
  photo_ids = list(photo_ids["photo_id"])
41
 
42
  def image_from_text(text_input):
 
 
43
  ## Inference
44
  with torch.no_grad():
45
  inputs = tokenizer([text_input], padding=True, return_tensors="pt")
@@ -53,6 +56,9 @@ def image_from_text(text_input):
53
  photo_id = photo_ids[idx]
54
  photo_data = photos[photos["photo_id"] == photo_id].iloc[0]
55
 
 
 
 
56
  # Downlaod image
57
  response = requests.get(photo_data["photo_image_url"] + "?w=640")
58
  pil_image = Image.open(BytesIO(response.content)).convert("RGB")
@@ -60,32 +66,40 @@ def image_from_text(text_input):
60
  # Convert RGB to BGR
61
  open_cv_image = open_cv_image[:, :, ::-1].copy()
62
 
 
 
63
  return open_cv_image
64
 
65
  def inference(content, style):
 
 
 
66
  result = stylepro_artistic.style_transfer(
67
  images=[{
68
- "content": image_from_text(content),
69
  "styles": [cv2.imread(style.name)]
70
  }])
 
 
71
  return Image.fromarray(np.uint8(result[0]["data"])[:,:,::-1]).convert("RGB")
72
-
73
- title = "Neural Style Transfer"
74
- description = "Gradio demo for Neural Style Transfer. To use it, simply enter the text for image content and upload style image. Read more at the links below."
75
- article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2003.07694'target='_blank'>Parameter-Free Style Projection for Arbitrary Style Transfer</a> | <a href='https://github.com/PaddlePaddle/PaddleHub' target='_blank'>Github Repo</a></br><a href='https://arxiv.org/abs/2103.00020'target='_blank'>Clip paper</a> | <a href='https://huggingface.co/transformers/model_doc/clip.html' target='_blank'>Hugging Face Clip Implementation</a></p>"
76
- examples=[
77
- ["a cute kangaroo", "styles/starry.jpeg"],
78
- ["man holding beer", "styles/mona1.jpeg"],
79
- ]
80
- interface = gr.Interface(inference,
81
- inputs=[
82
- gr.inputs.Textbox(lines=1, placeholder="Describe the content of the image", default="a cute kangaroo", label="Describe the image to which the style will be applied"),
83
- gr.inputs.Image(type="file", label="Style to be applied"),
84
- ],
85
- outputs=gr.outputs.Image(type="pil"),
86
- enable_queue=True,
87
- title=title,
88
- description=description,
89
- article=article,
90
- examples=examples)
91
- interface.launch()
 
 
1
  import os
2
  from io import BytesIO
3
  import requests
4
+ from datetime import datetime
5
 
6
  # Interface utilities
7
  import gradio as gr
 
41
  photo_ids = list(photo_ids["photo_id"])
42
 
43
  def image_from_text(text_input):
44
+ start=datetime.now()
45
+
46
  ## Inference
47
  with torch.no_grad():
48
  inputs = tokenizer([text_input], padding=True, return_tensors="pt")
 
56
  photo_id = photo_ids[idx]
57
  photo_data = photos[photos["photo_id"] == photo_id].iloc[0]
58
 
59
+ print(f"Time spent at CLIP: {datetime.now()-start}")
60
+
61
+ start=datetime.now()
62
  # Downlaod image
63
  response = requests.get(photo_data["photo_image_url"] + "?w=640")
64
  pil_image = Image.open(BytesIO(response.content)).convert("RGB")
 
66
  # Convert RGB to BGR
67
  open_cv_image = open_cv_image[:, :, ::-1].copy()
68
 
69
+ print(f"Time spent at Image request: {datetime.now()-start}")
70
+
71
  return open_cv_image
72
 
73
  def inference(content, style):
74
+ content_image = image_from_text(content)
75
+ start=datetime.now()
76
+
77
  result = stylepro_artistic.style_transfer(
78
  images=[{
79
+ "content": content_image,
80
  "styles": [cv2.imread(style.name)]
81
  }])
82
+
83
+ print(f"Time spent at Style Transfer: {datetime.now()-start}")
84
  return Image.fromarray(np.uint8(result[0]["data"])[:,:,::-1]).convert("RGB")
85
+
86
+ if __name__ == "__main__":
87
+ title = "Neural Style Transfer"
88
+ description = "Gradio demo for Neural Style Transfer. To use it, simply enter the text for image content and upload style image. Read more at the links below."
89
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2003.07694'target='_blank'>Parameter-Free Style Projection for Arbitrary Style Transfer</a> | <a href='https://github.com/PaddlePaddle/PaddleHub' target='_blank'>Github Repo</a></br><a href='https://arxiv.org/abs/2103.00020'target='_blank'>Clip paper</a> | <a href='https://huggingface.co/transformers/model_doc/clip.html' target='_blank'>Hugging Face Clip Implementation</a></p>"
90
+ examples=[
91
+ ["a cute kangaroo", "styles/starry.jpeg"],
92
+ ["man holding beer", "styles/mona1.jpeg"],
93
+ ]
94
+ interface = gr.Interface(inference,
95
+ inputs=[
96
+ gr.inputs.Textbox(lines=1, placeholder="Describe the content of the image", default="a cute kangaroo", label="Describe the image to which the style will be applied"),
97
+ gr.inputs.Image(type="file", label="Style to be applied"),
98
+ ],
99
+ outputs=gr.outputs.Image(type="pil"),
100
+ enable_queue=True,
101
+ title=title,
102
+ description=description,
103
+ article=article,
104
+ examples=examples)
105
+ interface.launch()