gojiteji commited on
Commit
e461401
·
1 Parent(s): 0330c8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -5,6 +5,9 @@ import jax
5
  import jax.numpy as jnp
6
  import gradio as gr
7
 
 
 
 
8
  from pathlib import Path
9
  from PIL import Image
10
  import numpy as np
@@ -61,12 +64,41 @@ pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
61
 
62
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  def text_to_image_and_image_to_text(text=None,image=None):
66
  txt=None
67
  img=None
68
  if image != None:
69
- txt=text
70
  if text !=None:
71
  images = sd2_inference(pipeline, [text], params, seed = 42, num_inference_steps = 5 )
72
  img = images[0]
 
5
  import jax.numpy as jnp
6
  import gradio as gr
7
 
8
+ from PIL import Image
9
+ from transformers import ViTFeatureExtractor, AutoTokenizer, FlaxVisionEncoderDecoderModel
10
+
11
  from pathlib import Path
12
  from PIL import Image
13
  import numpy as np
 
64
 
65
 
66
 
67
+ loc = "ydshieh/vit-gpt2-coco-en"
68
+
69
+ feature_extractor = ViTFeatureExtractor.from_pretrained(loc)
70
+ tokenizer = AutoTokenizer.from_pretrained(loc)
71
+ model = FlaxVisionEncoderDecoderModel.from_pretrained(loc)
72
+
73
+ gen_kwargs = {"max_length": 16, "num_beams": 4}
74
+
75
+
76
+ # This takes sometime when compiling the first time, but the subsequent inference will be much faster
77
+
78
+ def generate(pixel_values):
79
+ output_ids = model.generate(pixel_values, **gen_kwargs).sequences
80
+ return output_ids
81
+
82
+
83
+ def predict(image):
84
+
85
+ pixel_values = feature_extractor(images=image, return_tensors="np").pixel_values
86
+ output_ids = generate(pixel_values)
87
+ preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
88
+ preds = [pred.strip() for pred in preds]
89
+
90
+ return preds
91
+
92
+ def image2text(image):
93
+ preds = predict(images[0])
94
+ return (preds[0])
95
+
96
 
97
  def text_to_image_and_image_to_text(text=None,image=None):
98
  txt=None
99
  img=None
100
  if image != None:
101
+ txt=image2text(image)
102
  if text !=None:
103
  images = sd2_inference(pipeline, [text], params, seed = 42, num_inference_steps = 5 )
104
  img = images[0]