Spaces:
Runtime error
Runtime error
Soha Sarwar
commited on
Commit
·
aa76222
1
Parent(s):
f7e4587
"Added app code"
Browse files- Makefile +0 -0
- app.py +69 -0
- requirements.txt +7 -0
Makefile
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import re
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
| 7 |
+
import os
|
| 8 |
+
import tensorflow as tf
|
| 9 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 10 |
+
|
| 11 |
+
device='cpu'
|
| 12 |
+
|
| 13 |
+
model_id = "nttdataspain/vit-gpt2-stablediffusion2-lora"
|
| 14 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_id)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 16 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_id)
|
| 17 |
+
|
| 18 |
+
# Predict function
|
| 19 |
+
def predict(image):
|
| 20 |
+
img = image.convert('RGB')
|
| 21 |
+
model.eval()
|
| 22 |
+
pixel_values = feature_extractor(images=[img], return_tensors="pt").pixel_values
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
output_ids = model.generate(pixel_values, max_length=16, num_beams=4, return_dict_in_generate=True).sequences
|
| 25 |
+
|
| 26 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 27 |
+
preds = [pred.strip() for pred in preds]
|
| 28 |
+
return preds[0]
|
| 29 |
+
|
| 30 |
+
input = gr.inputs.Image(label="Upload any Image", type = 'pil', optional=True)
|
| 31 |
+
output = gr.outputs.Textbox(type="text",label="Captions")
|
| 32 |
+
examples_folder = os.path.join(os.path.dirname(__file__), "examples")
|
| 33 |
+
examples = [os.path.join(examples_folder, file) for file in os.listdir(examples_folder)]
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
|
| 37 |
+
gr.HTML(
|
| 38 |
+
"""
|
| 39 |
+
<div style="text-align: center; max-width: 1200px; margin: 20px auto;">
|
| 40 |
+
<h2 style="font-weight: 900; font-size: 3rem; margin: 0rem">
|
| 41 |
+
📸 Image-to-Text with Awais Nayyar 📝
|
| 42 |
+
</h2>
|
| 43 |
+
<br>
|
| 44 |
+
</div>
|
| 45 |
+
""")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
# img = gr.inputs.Image(label="Upload any Image", type = 'pil', optional=True)
|
| 50 |
+
img = gr.Image(label="Upload any Image", type = 'pil', optional=True)
|
| 51 |
+
|
| 52 |
+
# img = gr.inputs.Image(type="pil", label="Upload any Image", optional=True)
|
| 53 |
+
|
| 54 |
+
button = gr.Button(value="Convert")
|
| 55 |
+
with gr.Column(scale=1):
|
| 56 |
+
# out = gr.outputs.Textbox(type="text",label="Captions")
|
| 57 |
+
out = gr.Label(type="text", label="Captions")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
button.click(predict, inputs=[img], outputs=[out])
|
| 61 |
+
|
| 62 |
+
gr.Examples(
|
| 63 |
+
examples=examples,
|
| 64 |
+
inputs=img,
|
| 65 |
+
outputs=out,
|
| 66 |
+
fn=predict,
|
| 67 |
+
cache_examples=True,
|
| 68 |
+
)
|
| 69 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
streamlit
|
| 3 |
+
transformers
|
| 4 |
+
pillow
|
| 5 |
+
requests
|
| 6 |
+
torch
|
| 7 |
+
tensorflow
|