Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
3 |
+
import os
|
4 |
+
import tensorflow as tf
|
5 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
6 |
+
|
7 |
+
device='cpu'
|
8 |
+
|
9 |
+
model_id = "nttdataspain/vit-gpt2-stablediffusion2-lora"
|
10 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_id)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
12 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_id)
|
13 |
+
|
14 |
+
def predict(image):
|
15 |
+
img = image.convert('RGB')
|
16 |
+
model.eval()
|
17 |
+
pixel_values = feature_extractor(images=[img], return_tensors="pt").pixel_values
|
18 |
+
with torch.no_grad():
|
19 |
+
output_ids = model.generate(pixel_values, max_length=16, num_beams=4, return_dict_in_generate=True).sequences
|
20 |
+
|
21 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
22 |
+
preds = [pred.strip() for pred in preds]
|
23 |
+
return preds[0]
|
24 |
+
# def get_image(img):
|
25 |
+
# image=pipe(img)
|
26 |
+
# return image[0]['generated_text']
|
27 |
+
image=gr.Interface(predict,title='Image to text',inputs= gr.Image(label="Upload any Image", type = 'pil'),outputs='text').launch(share=True)
|