Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,43 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
headers = {"Authorization": f"Bearer {st.secrets['huggingface_api_key']}"}
|
7 |
-
|
8 |
-
# Function to query the model
|
9 |
-
def query_image(image_data, prompt_text):
|
10 |
-
# Prepare the payload
|
11 |
-
payload = {
|
12 |
-
"inputs": {
|
13 |
-
"image": image_data,
|
14 |
-
"text": prompt_text
|
15 |
-
}
|
16 |
-
}
|
17 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
-
return response.json()
|
19 |
|
20 |
# Streamlit page setup
|
21 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="auto")
|
22 |
|
23 |
# Add the image with a specified width
|
24 |
-
image_width = 300 #
|
25 |
st.image('MTSS.ai_Logo.png', width=image_width)
|
26 |
|
27 |
st.header('VisionTexts™ | Accessibility')
|
28 |
st.subheader('Image Alt Text Creator')
|
29 |
|
30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
32 |
|
33 |
if uploaded_file:
|
34 |
# Display the uploaded image
|
35 |
-
|
|
|
36 |
with st.expander("Image", expanded=True):
|
37 |
-
st.image(
|
|
|
|
|
38 |
|
39 |
# Toggle for showing additional details input
|
40 |
show_details = st.checkbox("Add details about the image.", value=False)
|
@@ -42,70 +45,77 @@ show_details = st.checkbox("Add details about the image.", value=False)
|
|
42 |
if show_details:
|
43 |
# Text input for additional details about the image
|
44 |
additional_details = st.text_area(
|
45 |
-
"
|
46 |
)
|
|
|
|
|
47 |
|
48 |
# Toggle for modifying the prompt for complex images
|
49 |
complex_image = st.checkbox("Is this a complex image?", value=False)
|
50 |
|
51 |
-
if complex_image:
|
52 |
-
st.caption(
|
53 |
-
"By selecting this, the app will create a description exceeding the 125-character limit. "
|
54 |
-
"Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box."
|
55 |
-
)
|
56 |
-
|
57 |
# Button to trigger the analysis
|
58 |
-
analyze_button = st.button("Analyze the Image")
|
59 |
|
60 |
# Optimized prompt for complex images
|
61 |
complex_image_prompt_text = (
|
62 |
-
"As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
|
63 |
-
"Provide a brief description using not more than 500 characters that
|
64 |
"Skip phrases like 'image of' or 'picture of.' "
|
65 |
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
66 |
)
|
67 |
|
68 |
# Check if an image has been uploaded and if the button has been pressed
|
69 |
if uploaded_file is not None and analyze_button:
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
|
75 |
# Determine which prompt to use based on the complexity of the image
|
76 |
if complex_image:
|
77 |
prompt_text = complex_image_prompt_text
|
78 |
else:
|
79 |
prompt_text = (
|
80 |
-
"As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
|
81 |
-
"Provide a brief description using not more than 125 characters that
|
82 |
"Skip phrases like 'image of' or 'picture of.' "
|
83 |
-
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points and newlines, focusing on creating a seamless narrative
|
84 |
)
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
#
|
92 |
try:
|
93 |
-
response
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
106 |
except Exception as e:
|
107 |
st.error(f"An error occurred: {e}")
|
108 |
else:
|
109 |
-
|
110 |
-
if not uploaded_file and analyze_button:
|
111 |
-
st.warning("Please upload an image.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Streamlit page setup
|
8 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="auto")
|
9 |
|
10 |
# Add the image with a specified width
|
11 |
+
image_width = 300 # Set the desired width in pixels
|
12 |
st.image('MTSS.ai_Logo.png', width=image_width)
|
13 |
|
14 |
st.header('VisionTexts™ | Accessibility')
|
15 |
st.subheader('Image Alt Text Creator')
|
16 |
|
17 |
+
# Retrieve the Hugging Face API Key from secrets
|
18 |
+
huggingface_api_key = st.secrets["huggingface_api_key"]
|
19 |
+
|
20 |
+
# Initialize the image captioning pipeline
|
21 |
+
image_captioner = pipeline(
|
22 |
+
"image-to-text",
|
23 |
+
model="Salesforce/blip-image-captioning-large",
|
24 |
+
use_auth_token=huggingface_api_key
|
25 |
+
)
|
26 |
+
|
27 |
+
# Initialize the language model client
|
28 |
+
client = InferenceClient(token=huggingface_api_key)
|
29 |
+
|
30 |
+
# File uploader allows user to add their own image
|
31 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
32 |
|
33 |
if uploaded_file:
|
34 |
# Display the uploaded image
|
35 |
+
image = Image.open(uploaded_file)
|
36 |
+
image_width = 200 # Set the desired width in pixels
|
37 |
with st.expander("Image", expanded=True):
|
38 |
+
st.image(image, caption=uploaded_file.name, width=image_width, use_column_width=False)
|
39 |
+
else:
|
40 |
+
st.warning("Please upload an image.")
|
41 |
|
42 |
# Toggle for showing additional details input
|
43 |
show_details = st.checkbox("Add details about the image.", value=False)
|
|
|
45 |
if show_details:
|
46 |
# Text input for additional details about the image
|
47 |
additional_details = st.text_area(
|
48 |
+
"The details could include specific information that is important to include in the alt text or reflect why the image is being used:"
|
49 |
)
|
50 |
+
else:
|
51 |
+
additional_details = ""
|
52 |
|
53 |
# Toggle for modifying the prompt for complex images
|
54 |
complex_image = st.checkbox("Is this a complex image?", value=False)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Button to trigger the analysis
|
57 |
+
analyze_button = st.button("Analyze the Image", type="secondary")
|
58 |
|
59 |
# Optimized prompt for complex images
|
60 |
complex_image_prompt_text = (
|
61 |
+
"As an expert in image accessibility and alternative text, thoroughly describe the image caption provided. "
|
62 |
+
"Provide a brief description using not more than 500 characters that conveys the essential information in eight or fewer clear and concise sentences. "
|
63 |
"Skip phrases like 'image of' or 'picture of.' "
|
64 |
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
65 |
)
|
66 |
|
67 |
# Check if an image has been uploaded and if the button has been pressed
|
68 |
if uploaded_file is not None and analyze_button:
|
69 |
+
with st.spinner("Analyzing the image..."):
|
70 |
+
# Get the caption from the image using the image captioning model
|
71 |
+
caption_response = image_captioner(image)
|
72 |
+
image_caption = caption_response[0]['generated_text']
|
73 |
|
74 |
# Determine which prompt to use based on the complexity of the image
|
75 |
if complex_image:
|
76 |
prompt_text = complex_image_prompt_text
|
77 |
else:
|
78 |
prompt_text = (
|
79 |
+
"As an expert in image accessibility and alternative text, succinctly describe the image caption provided in less than 125 characters. "
|
80 |
+
"Provide a brief description using not more than 125 characters that conveys the essential information in three or fewer clear and concise sentences for use as alt text. "
|
81 |
"Skip phrases like 'image of' or 'picture of.' "
|
82 |
+
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points and newlines, focusing on creating a seamless narrative for accessibility purposes."
|
83 |
)
|
84 |
|
85 |
+
# Include additional details if provided
|
86 |
+
if additional_details:
|
87 |
+
prompt_text += f"\n\nInclude the additional context provided by the user in your description:\n{additional_details}"
|
88 |
+
|
89 |
+
# Create the prompt for the language model
|
90 |
+
full_prompt = f"{prompt_text}\n\nImage Caption: {image_caption}"
|
91 |
+
|
92 |
+
# Prepare messages for chat interface
|
93 |
+
messages = [
|
94 |
+
{"role": "user", "content": full_prompt}
|
95 |
+
]
|
96 |
|
97 |
+
# Use the language model to generate the alt text description
|
98 |
try:
|
99 |
+
# Stream the response from the language model
|
100 |
+
stream = client.chat(
|
101 |
+
model="meta-llama/Llama-2-7b-chat-hf",
|
102 |
+
messages=messages,
|
103 |
+
stream=True
|
104 |
+
)
|
105 |
+
|
106 |
+
# Stream the response
|
107 |
+
full_response = ""
|
108 |
+
message_placeholder = st.empty()
|
109 |
+
for chunk in stream:
|
110 |
+
if 'generated_text' in chunk:
|
111 |
+
content = chunk['generated_text']
|
112 |
+
full_response += content
|
113 |
+
message_placeholder.markdown(full_response + "▌")
|
114 |
+
# Final update after stream ends
|
115 |
+
message_placeholder.markdown(full_response)
|
116 |
+
|
117 |
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
118 |
except Exception as e:
|
119 |
st.error(f"An error occurred: {e}")
|
120 |
else:
|
121 |
+
st.write("Please upload an image and click 'Analyze the Image' to generate alt text.")
|
|
|
|