Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,18 @@
|
|
1 |
import numpy as np
|
2 |
from PIL import Image
|
3 |
import streamlit as st
|
4 |
-
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
5 |
|
6 |
# Directory path to the saved model on Google Drive
|
7 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
8 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
9 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
def generate_captions(image):
|
12 |
image = Image.open(image).convert("RGB")
|
13 |
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
|
@@ -16,6 +21,18 @@ def generate_captions(image):
|
|
16 |
generated_caption = sentence.replace(text_to_remove, "")
|
17 |
return generated_caption
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# create the Streamlit app
|
20 |
def app():
|
21 |
st.title('Image from your Side, Trending Hashtags from our Side')
|
@@ -36,6 +53,9 @@ def app():
|
|
36 |
st.image(image, caption='The Uploaded File')
|
37 |
st.write("First is first captions for your Photo : ", string)
|
38 |
|
|
|
|
|
|
|
39 |
# run the app
|
40 |
if __name__ == '__main__':
|
41 |
app()
|
|
|
1 |
import numpy as np
|
2 |
from PIL import Image
|
3 |
import streamlit as st
|
4 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel, GPT2Tokenizer, GPT2LMHeadModel
|
5 |
|
6 |
# Directory path to the saved model on Google Drive
|
7 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
8 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
9 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
10 |
|
11 |
+
# Load the pre-trained model and tokenizer
|
12 |
+
model_name = "gpt2"
|
13 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
14 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
15 |
+
|
16 |
def generate_captions(image):
|
17 |
image = Image.open(image).convert("RGB")
|
18 |
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
|
|
|
21 |
generated_caption = sentence.replace(text_to_remove, "")
|
22 |
return generated_caption
|
23 |
|
24 |
+
# Define the Streamlit app
|
25 |
+
def generate_paragraph(prompt):
|
26 |
+
# Tokenize the prompt
|
27 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
28 |
+
|
29 |
+
# Generate the paragraph
|
30 |
+
output = model.generate(input_ids, max_length=200, num_return_sequences=1, early_stopping=True)
|
31 |
+
|
32 |
+
# Decode the generated output into text
|
33 |
+
paragraph = tokenizer.decode(output[0], skip_special_tokens=True)
|
34 |
+
return paragraph
|
35 |
+
|
36 |
# create the Streamlit app
|
37 |
def app():
|
38 |
st.title('Image from your Side, Trending Hashtags from our Side')
|
|
|
53 |
st.image(image, caption='The Uploaded File')
|
54 |
st.write("First is first captions for your Photo : ", string)
|
55 |
|
56 |
+
generated_paragraph = generate_paragraph(string)
|
57 |
+
|
58 |
+
st.write(generated_paragraph)
|
59 |
# run the app
|
60 |
if __name__ == '__main__':
|
61 |
app()
|