Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,52 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import base64
|
| 4 |
import os
|
| 5 |
+
from langchain.llms import OpenAI
|
| 6 |
+
from langchain.prompts import ChatPromptTemplate
|
| 7 |
|
| 8 |
+
# Set up environment variable for OpenAI API key
|
| 9 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("k1")
|
| 10 |
|
| 11 |
+
# Initialize OpenAI with LangChain
|
| 12 |
+
llm = OpenAI(api_key=os.environ["OPENAI_API_KEY"], model="gpt-3.5-turbo", temperature=0)
|
| 13 |
|
| 14 |
+
# Prompt template for image captioning
|
| 15 |
+
caption_template = """
|
| 16 |
+
You are an expert image captioner. Given an image in base64 format, provide a descriptive caption for the image.
|
| 17 |
+
|
| 18 |
+
Image (base64): {image_base64}
|
| 19 |
+
|
| 20 |
+
Caption:
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
prompt = ChatPromptTemplate.from_messages([("system", caption_template)])
|
| 24 |
+
|
| 25 |
+
def generate_caption(image):
|
| 26 |
+
# Convert the image to base64
|
| 27 |
+
buffered = BytesIO()
|
| 28 |
+
image.save(buffered, format="JPEG")
|
| 29 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 30 |
+
|
| 31 |
+
# Create the prompt
|
| 32 |
+
formatted_prompt = prompt.format(image_base64=img_base64)
|
| 33 |
+
|
| 34 |
+
# Generate the caption using OpenAI
|
| 35 |
+
response = llm(formatted_prompt)
|
| 36 |
+
|
| 37 |
+
return response.choices[0].text.strip()
|
| 38 |
+
|
| 39 |
+
# Streamlit UI
|
| 40 |
+
st.title("Image Captioning Application :robot_face:")
|
| 41 |
+
|
| 42 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 43 |
+
|
| 44 |
+
if uploaded_file is not None:
|
| 45 |
+
image = Image.open(uploaded_file)
|
| 46 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 47 |
+
|
| 48 |
+
if st.button("Generate Caption"):
|
| 49 |
+
with st.spinner("Generating caption..."):
|
| 50 |
+
caption = generate_caption(image)
|
| 51 |
+
st.write("Caption: ")
|
| 52 |
+
st.write(caption)
|