Image_Describer / app.py
kusumakar's picture
Update app.py
f9bfa88
raw
history blame
2.61 kB
import openai
import os
import streamlit as st
# Access repository secret
api_key = os.environ.get('OPENAI_API_KEY')
# Check if the secret is available
if api_key:
st.write("API Key:", api_key)
else:
st.write("API Key not found.")
import numpy as np
from PIL import Image
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel, GPT2Tokenizer, GPT2LMHeadModel
# Directory path to the saved model on Google Drive
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
def generate_captions(image):
image = Image.open(image).convert("RGB")
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
sentence = generated_caption
text_to_remove = "<|endoftext|>"
generated_caption = sentence.replace(text_to_remove, "")
return generated_caption
def generate_paragraph(caption):
prompt = "Generate a paragraph based on the following caption: " + caption
# Make the API call to GPT-3
response = openai.Completion.create(
engine='text-davinci-003', # Specify the GPT-3 model
prompt=prompt,
max_tokens=200, # Adjust the desired length of the generated text
n = 1, # Set the number of completions to generate
stop=None, # Specify an optional stop sequence
temperature=0.7 # Adjust the temperature for randomness (between 0 and 1)
)
# Extract the generated text from the API response
generated_text = response.choices[0].text.strip()
return generated_text
# create the Streamlit app
def app():
st.title('Image from your Side, Detailed description from my site')
st.write('Upload an image to see what we have in store.')
# create file uploader
uploaded_file = st.file_uploader("Got You Covered, Upload your wish!, magic on the Way! ", type=["jpg", "jpeg", "png"])
# check if file has been uploaded
if uploaded_file is not None:
# load the image
image = Image.open(uploaded_file).convert("RGB")
# Image Captions
string = generate_captions(uploaded_file)
st.image(image, caption='The Uploaded File')
st.write("First is first captions for your Photo : ", string)
generated_paragraph = generate_paragraph(string)
st.write(generated_paragraph)
# run the app
if __name__ == '__main__':
app()