Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
-
|
4 |
|
5 |
# Function to encode the image to base64
|
6 |
def encode_image(image_file):
|
@@ -10,21 +10,19 @@ def encode_image(image_file):
|
|
10 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
|
11 |
st.title("MTSS Snapshot: Accessibility Image Textifier: `Alt Text`")
|
12 |
|
13 |
-
# Retrieve the OpenAI API
|
14 |
openai.api_key = st.secrets["openai_api_key"]
|
15 |
|
16 |
-
#
|
17 |
-
# openai.api_key = api_key
|
18 |
-
|
19 |
-
# File uploader for images
|
20 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
21 |
|
22 |
if uploaded_file:
|
23 |
-
|
|
|
24 |
st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)
|
25 |
|
26 |
-
# Toggle for additional details input
|
27 |
-
show_details = st.
|
28 |
|
29 |
if show_details:
|
30 |
# Text input for additional details about the image, shown only if toggle is True
|
@@ -34,13 +32,12 @@ if show_details:
|
|
34 |
)
|
35 |
|
36 |
# Button to trigger the analysis
|
37 |
-
analyze_button = st.button("Analyse the
|
38 |
|
39 |
# Check if an image has been uploaded, if the API key is available, and if the button has been pressed
|
40 |
-
|
41 |
-
if uploaded_file is not None and analyze_button:
|
42 |
|
43 |
-
with st.spinner("
|
44 |
# Encode the image
|
45 |
base64_image = encode_image(uploaded_file)
|
46 |
|
@@ -49,13 +46,15 @@ if uploaded_file is not None and analyze_button:
|
|
49 |
"You are a highly knowledgeable accessibility expert. "
|
50 |
"Your task is to examine the following image in detail. "
|
51 |
"Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
|
52 |
-
"Highlight key elements and their significance, and present your analysis in clear, well-structured format. "
|
53 |
-
"Create a detailed image caption explaining in 150 words or less.
|
54 |
)
|
55 |
-
|
56 |
if show_details and additional_details:
|
57 |
-
prompt_text +=
|
58 |
-
|
|
|
|
|
59 |
# Create the payload for the completion request
|
60 |
messages = [
|
61 |
{
|
@@ -69,7 +68,7 @@ if uploaded_file is not None and analyze_button:
|
|
69 |
],
|
70 |
}
|
71 |
]
|
72 |
-
|
73 |
# Make the request to the OpenAI API
|
74 |
try:
|
75 |
# Without Stream
|
@@ -101,4 +100,4 @@ else:
|
|
101 |
if not uploaded_file and analyze_button:
|
102 |
st.warning("Please upload an image.")
|
103 |
if not api_key:
|
104 |
-
st.warning("Please enter your OpenAI API key.")
|
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
+
from openai
|
4 |
|
5 |
# Function to encode the image to base64
|
6 |
def encode_image(image_file):
|
|
|
10 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
|
11 |
st.title("MTSS Snapshot: Accessibility Image Textifier: `Alt Text`")
|
12 |
|
13 |
+
# Retrieve the OpenAI API Key from secrets
|
14 |
openai.api_key = st.secrets["openai_api_key"]
|
15 |
|
16 |
+
# File uploader allows user to add their own image
|
|
|
|
|
|
|
17 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
18 |
|
19 |
if uploaded_file:
|
20 |
+
# Display the uploaded image
|
21 |
+
with st.expander("Image", expanded = True):
|
22 |
st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)
|
23 |
|
24 |
+
# Toggle for showing additional details input
|
25 |
+
show_details = st.toggle("Add details about the image", value=False)
|
26 |
|
27 |
if show_details:
|
28 |
# Text input for additional details about the image, shown only if toggle is True
|
|
|
32 |
)
|
33 |
|
34 |
# Button to trigger the analysis
|
35 |
+
analyze_button = st.button("Analyse the Image", type="secondary")
|
36 |
|
37 |
# Check if an image has been uploaded, if the API key is available, and if the button has been pressed
|
38 |
+
if uploaded_file is not None and api_key and analyze_button:
|
|
|
39 |
|
40 |
+
with st.spinner("Analyzing the image ..."):
|
41 |
# Encode the image
|
42 |
base64_image = encode_image(uploaded_file)
|
43 |
|
|
|
46 |
"You are a highly knowledgeable accessibility expert. "
|
47 |
"Your task is to examine the following image in detail. "
|
48 |
"Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
|
49 |
+
"Highlight key elements and their significance, and present your analysis in clear, well-structured markdown format. "
|
50 |
+
"Create a detailed image caption in explaining in 150 words or less."
|
51 |
)
|
52 |
+
|
53 |
if show_details and additional_details:
|
54 |
+
prompt_text += (
|
55 |
+
f"\n\nAdditional Context Provided by the User:\n{additional_details}"
|
56 |
+
)
|
57 |
+
|
58 |
# Create the payload for the completion request
|
59 |
messages = [
|
60 |
{
|
|
|
68 |
],
|
69 |
}
|
70 |
]
|
71 |
+
|
72 |
# Make the request to the OpenAI API
|
73 |
try:
|
74 |
# Without Stream
|
|
|
100 |
if not uploaded_file and analyze_button:
|
101 |
st.warning("Please upload an image.")
|
102 |
if not api_key:
|
103 |
+
st.warning("Please enter your OpenAI API key.")")
|