Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,19 @@
|
|
1 |
-
import
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
-
|
5 |
-
import
|
6 |
-
from transformers import BartTokenizer, BartForConditionalGeneration
|
7 |
-
|
8 |
-
# Load pre-trained BART model and tokenizer
|
9 |
-
tokenizer_2 = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
|
10 |
-
model_2 = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
|
11 |
|
12 |
-
#
|
13 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
14 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
15 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
16 |
|
|
|
|
|
|
|
|
|
|
|
17 |
def generate_captions(image):
|
18 |
image = Image.open(image).convert("RGB")
|
19 |
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
|
@@ -22,6 +22,7 @@ def generate_captions(image):
|
|
22 |
generated_caption = sentence.replace(text_to_remove, "")
|
23 |
return generated_caption
|
24 |
|
|
|
25 |
def generate_paragraph(caption):
|
26 |
# Tokenize the caption
|
27 |
inputs = tokenizer_2([caption], max_length=1024, truncation=True, padding="longest", return_tensors="pt")
|
@@ -31,10 +32,8 @@ def generate_paragraph(caption):
|
|
31 |
|
32 |
# Decode the generated output
|
33 |
generated_text = tokenizer_2.decode(output[0], skip_special_tokens=True)
|
34 |
-
|
35 |
return generated_text
|
36 |
|
37 |
-
|
38 |
# create the Streamlit app
|
39 |
def app():
|
40 |
st.title('Image from your Side, Detailed description from my site')
|
|
|
1 |
+
import torch
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
+
import streamlit as st
|
5 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel, BartTokenizer, BartForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# pre-trained model to arrive at context
|
8 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
9 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
10 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
11 |
|
12 |
+
# pre-trained to arrive at description
|
13 |
+
tokenizer_2 = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
|
14 |
+
model_2 = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
|
15 |
+
|
16 |
+
# function to generate context
|
17 |
def generate_captions(image):
|
18 |
image = Image.open(image).convert("RGB")
|
19 |
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
|
|
|
22 |
generated_caption = sentence.replace(text_to_remove, "")
|
23 |
return generated_caption
|
24 |
|
25 |
+
# function to generate description
|
26 |
def generate_paragraph(caption):
|
27 |
# Tokenize the caption
|
28 |
inputs = tokenizer_2([caption], max_length=1024, truncation=True, padding="longest", return_tensors="pt")
|
|
|
32 |
|
33 |
# Decode the generated output
|
34 |
generated_text = tokenizer_2.decode(output[0], skip_special_tokens=True)
|
|
|
35 |
return generated_text
|
36 |
|
|
|
37 |
# create the Streamlit app
|
38 |
def app():
|
39 |
st.title('Image from your Side, Detailed description from my site')
|