Upload 2 files
Browse files- app.py +58 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from fairseq.checkpoint_utils import load_model_ensemble_and_task_from_hf_hub
|
| 8 |
+
from fairseq.models.text_to_speech.hub_interface import TTSHubInterface
|
| 9 |
+
|
| 10 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 11 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 13 |
+
|
| 14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
model.to(device)
|
| 16 |
+
|
| 17 |
+
models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
|
| 18 |
+
"facebook/fastspeech2-en-ljspeech",
|
| 19 |
+
arg_overrides={"vocoder": "hifigan", "fp16": False}
|
| 20 |
+
)
|
| 21 |
+
model1 = models[0]
|
| 22 |
+
TTSHubInterface.update_cfg_with_data_cfg(cfg, task.data_cfg)
|
| 23 |
+
generator = task.build_generator(models, cfg)
|
| 24 |
+
|
| 25 |
+
max_length = 16
|
| 26 |
+
num_beams = 4
|
| 27 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def predict_step(image_paths):
|
| 31 |
+
images = []
|
| 32 |
+
text = ""
|
| 33 |
+
|
| 34 |
+
for image_path in image_paths:
|
| 35 |
+
i_image = Image.fromarray(image_path)
|
| 36 |
+
if i_image.mode != "RGB":
|
| 37 |
+
i_image = i_image.convert(mode="RGB")
|
| 38 |
+
print(image_path)
|
| 39 |
+
|
| 40 |
+
images.append(i_image)
|
| 41 |
+
print(images)
|
| 42 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
| 43 |
+
pixel_values = pixel_values.to(device)
|
| 44 |
+
|
| 45 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
| 46 |
+
|
| 47 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 48 |
+
preds = [pred.strip() for pred in preds]
|
| 49 |
+
preds = ' '.join(str(e) for e in preds)
|
| 50 |
+
text = text + preds
|
| 51 |
+
sample = TTSHubInterface.get_model_input(task, text)
|
| 52 |
+
wav, rate = TTSHubInterface.get_prediction(task, model1, generator, sample)
|
| 53 |
+
return wav#, rate, text
|
| 54 |
+
#return ipd.Audio(wav, rate=rate)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
interface = gr.Interface(predict_step, gr.Image(), "audio")
|
| 58 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
fairseq
|
| 3 |
+
gradio
|
| 4 |
+
transformers
|
| 5 |
+
Pillow
|
| 6 |
+
g2p-en
|