D0k-tor commited on
Commit
355d287
·
1 Parent(s): 3eb855f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -1
app.py CHANGED
@@ -1,5 +1,8 @@
1
  import gradio as gr
2
  import streamlit as st
 
 
 
3
 
4
  # def greet(name):
5
  # return "Hello " + name + "!!"
@@ -7,6 +10,23 @@ import streamlit as st
7
  # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
8
  # iface.launch()
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  st.title("Image to Text using Lora")
11
 
12
  inputs = gr.inputs.Image(label="Upload any Image", type = 'pil', optional=True)
@@ -15,7 +35,7 @@ description = "NTT Data Bilbao team"
15
  title = "Image to Text using Lora"
16
 
17
  interface = gr.Interface(
18
- # fn=predict,
19
  description=description,
20
  inputs = inputs,
21
  theme="grass",
 
1
  import gradio as gr
2
  import streamlit as st
3
+ import torch
4
+ import re
5
+ from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
6
 
7
  # def greet(name):
8
  # return "Hello " + name + "!!"
 
10
  # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
11
  # iface.launch()
12
 
13
+ device='cpu'
14
+ encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
15
+ decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
16
+ model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
17
+ feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
18
+ tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
19
+ model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
20
+
21
+
22
+ def predict(image,max_length=64, num_beams=4):
23
+ image = image.convert('RGB')
24
+ image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
25
+ clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
26
+ caption_ids = model.generate(image, max_length = max_length)[0]
27
+ caption_text = clean_text(tokenizer.decode(caption_ids))
28
+ return caption_text
29
+
30
  st.title("Image to Text using Lora")
31
 
32
  inputs = gr.inputs.Image(label="Upload any Image", type = 'pil', optional=True)
 
35
  title = "Image to Text using Lora"
36
 
37
  interface = gr.Interface(
38
+ fn=predict,
39
  description=description,
40
  inputs = inputs,
41
  theme="grass",