Spaces:
Sleeping
Sleeping
Create App2.py
Browse files
App2.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import base64
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Function to encode the image to base64
|
| 6 |
+
def encode_image(image_file):
|
| 7 |
+
return base64.b64encode(image_file.getvalue()).decode("utf-8")
|
| 8 |
+
|
| 9 |
+
# Streamlit page setup
|
| 10 |
+
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="auto")
|
| 11 |
+
|
| 12 |
+
# Add the image with a specified width
|
| 13 |
+
image_width = 300 # Set the desired width in pixels
|
| 14 |
+
st.image('MTSS.ai_Logo.png', width=image_width)
|
| 15 |
+
|
| 16 |
+
st.header('VisionTexts™ | Accessibility')
|
| 17 |
+
st.subheader('Image Alt Text Creator')
|
| 18 |
+
|
| 19 |
+
# Initialize the Hugging Face InferenceClient with the API key from Streamlit secrets
|
| 20 |
+
client = InferenceClient(api_key=st.secrets["huggingface_api_key"])
|
| 21 |
+
|
| 22 |
+
# File uploader
|
| 23 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 24 |
+
|
| 25 |
+
if uploaded_file:
|
| 26 |
+
# Display the uploaded image with specified width
|
| 27 |
+
image_width = 200 # Set the desired width in pixels
|
| 28 |
+
with st.expander("Image", expanded=True):
|
| 29 |
+
st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
|
| 30 |
+
|
| 31 |
+
# Toggle for showing additional details input
|
| 32 |
+
show_details = st.checkbox("Add details about the image.", value=False)
|
| 33 |
+
|
| 34 |
+
if show_details:
|
| 35 |
+
# Text input for additional details about the image
|
| 36 |
+
additional_details = st.text_area(
|
| 37 |
+
"The details could include specific information that is important to include in the alt text or reflect why the image is being used:",
|
| 38 |
+
disabled=not show_details
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Toggle for modifying the prompt for complex images
|
| 42 |
+
complex_image = st.checkbox("Is this a complex image?", value=False)
|
| 43 |
+
|
| 44 |
+
if complex_image:
|
| 45 |
+
# Caption explaining the impact of the complex image toggle
|
| 46 |
+
st.caption(
|
| 47 |
+
"By clicking this toggle, it will instruct the app to create a description that exceeds the 125-character limit. "
|
| 48 |
+
"Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Button to trigger the analysis
|
| 52 |
+
analyze_button = st.button("Analyze the Image", type="secondary")
|
| 53 |
+
|
| 54 |
+
# Optimized prompt for complex images
|
| 55 |
+
complex_image_prompt_text = (
|
| 56 |
+
"As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
|
| 57 |
+
"Provide a brief description using not more than 500 characters that conveys the essential information in eight or fewer clear and concise sentences. "
|
| 58 |
+
"Skip phrases like 'image of' or 'picture of.' "
|
| 59 |
+
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Check if an image has been uploaded and if the analyze button has been pressed
|
| 63 |
+
if uploaded_file is not None and analyze_button:
|
| 64 |
+
|
| 65 |
+
with st.spinner("Analyzing the image ..."):
|
| 66 |
+
# Encode the image
|
| 67 |
+
base64_image = encode_image(uploaded_file)
|
| 68 |
+
|
| 69 |
+
# Determine which prompt to use based on the complexity of the image
|
| 70 |
+
if complex_image:
|
| 71 |
+
prompt_text = complex_image_prompt_text
|
| 72 |
+
else:
|
| 73 |
+
prompt_text = (
|
| 74 |
+
"As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
|
| 75 |
+
"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. "
|
| 76 |
+
"Skip phrases like 'image of' or 'picture of.' "
|
| 77 |
+
"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."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if show_details and additional_details:
|
| 81 |
+
prompt_text += (
|
| 82 |
+
f"\n\nInclude the additional context provided by the user in your description:\n{additional_details}"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Create the payload for the completion request
|
| 86 |
+
messages = [
|
| 87 |
+
{
|
| 88 |
+
"role": "user",
|
| 89 |
+
"content": [
|
| 90 |
+
{"type": "text", "text": prompt_text},
|
| 91 |
+
{
|
| 92 |
+
"type": "image",
|
| 93 |
+
"image": {
|
| 94 |
+
# Provide the image bytes directly
|
| 95 |
+
"bytes": base64.b64decode(base64_image)
|
| 96 |
+
},
|
| 97 |
+
},
|
| 98 |
+
],
|
| 99 |
+
}
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
# Make the request to the Hugging Face API
|
| 103 |
+
try:
|
| 104 |
+
# Send the request to the model
|
| 105 |
+
completion = client.chat_completions(
|
| 106 |
+
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
| 107 |
+
messages=messages,
|
| 108 |
+
max_new_tokens=1200
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Extract the assistant's response
|
| 112 |
+
assistant_response = completion.get("choices")[0]["message"]["content"]
|
| 113 |
+
|
| 114 |
+
# Display the response
|
| 115 |
+
st.markdown(assistant_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 |
+
# Warning for user action required
|
| 122 |
+
if not uploaded_file and analyze_button:
|
| 123 |
+
st.warning("Please upload an image.")
|