Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,12 @@
|
|
1 |
-
import openai
|
2 |
-
openai.api_key = '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'
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel, GPT2Tokenizer, GPT2LMHeadModel
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Directory path to the saved model on Google Drive
|
8 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
@@ -18,23 +22,18 @@ def generate_captions(image):
|
|
18 |
return generated_caption
|
19 |
|
20 |
def generate_paragraph(caption):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
stop=None, # Specify an optional stop sequence
|
30 |
-
temperature=0.7 # Adjust the temperature for randomness (between 0 and 1)
|
31 |
-
)
|
32 |
-
|
33 |
-
# Extract the generated text from the API response
|
34 |
-
generated_text = response.choices[0].text.strip()
|
35 |
|
36 |
return generated_text
|
37 |
|
|
|
38 |
# create the Streamlit app
|
39 |
def app():
|
40 |
st.title('Image from your Side, Detailed description from my site')
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
from PIL import Image
|
3 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel, GPT2Tokenizer, GPT2LMHeadModel
|
4 |
+
import torch
|
5 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
6 |
+
|
7 |
+
# Load pre-trained BART model and tokenizer
|
8 |
+
tokenizer_2 = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
|
9 |
+
model_2 = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
|
10 |
|
11 |
# Directory path to the saved model on Google Drive
|
12 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
|
|
22 |
return generated_caption
|
23 |
|
24 |
def generate_paragraph(caption):
|
25 |
+
# Tokenize the caption
|
26 |
+
inputs = tokenizer_2([caption], max_length=1024, truncation=True, padding="longest", return_tensors="pt")
|
27 |
+
|
28 |
+
# Generate text
|
29 |
+
output = model_2.generate(inputs.input_ids, attention_mask=inputs.attention_mask, max_length=200, num_beams=4, length_penalty=2.0, early_stopping=True)
|
30 |
+
|
31 |
+
# Decode the generated output
|
32 |
+
generated_text = tokenizer_2.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
return generated_text
|
35 |
|
36 |
+
|
37 |
# create the Streamlit app
|
38 |
def app():
|
39 |
st.title('Image from your Side, Detailed description from my site')
|