VisionTexts / app.py
ProfessorLeVesseur's picture
Update app.py
bbec170 verified
raw
history blame
4.27 kB
import streamlit as st
import requests
# Streamlit page setup
st.set_page_config(
page_title="MTSS Image Accessibility Alt Text Generator",
layout="centered",
initial_sidebar_state="auto"
)
# Add the image with a specified width
image_width = 300 # Set the desired width in pixels
st.image('MTSS.ai_Logo.png', width=image_width)
st.header('VisionTexts™ | Accessibility')
st.subheader('Image Alt Text Creator')
# Initialize the API key from Streamlit secrets
api_key = st.secrets["huggingface_api_key"]
# File uploader
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
if uploaded_file:
# Display the uploaded image with specified width
image_width = 200 # Set the desired width in pixels
with st.expander("Image", expanded=True):
st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
# Toggle for showing additional details input
show_details = st.checkbox("Add details about the image.", value=False)
if show_details:
# Text input for additional details about the image
additional_details = st.text_area(
"Include any specific information that is important to include in the alt text or reflect why the image is being used:",
)
# Toggle for modifying the prompt for complex images
complex_image = st.checkbox("Is this a complex image?", value=False)
if complex_image:
# Caption explaining the impact of the complex image toggle
st.caption(
"By selecting this option, the app will create a detailed description that may exceed the typical 125-character limit for alt text."
)
# Button to trigger the analysis
analyze_button = st.button("Analyze the Image")
# Check if an image has been uploaded and if the analyze button has been pressed
if uploaded_file is not None and analyze_button:
with st.spinner("Analyzing the image ..."):
# Read the image bytes
image_bytes = uploaded_file.read()
# Decide on the model to use
model_id = "Salesforce/blip-image-captioning-base" # You can choose another model if desired
# Prepare headers and endpoint
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/octet-stream"
}
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
# Prepare the parameters
parameters = {
# "max_length": 50, # Adjust as needed
# "num_return_sequences": 1,
}
# Include additional details in the prompt if provided
if show_details and additional_details:
prompt_text = f"{additional_details}"
parameters["inputs"] = prompt_text
# Make the request to the Hugging Face API
try:
# Send the request with the image bytes
response = requests.post(
api_url,
headers=headers,
data=image_bytes,
params=parameters,
timeout=60 # Optional: increase timeout if needed
)
# Check for errors
response.raise_for_status()
# Parse the response
completion = response.json()
# Extract the generated description
if isinstance(completion, list) and "generated_text" in completion[0]:
assistant_response = completion[0]["generated_text"]
# Adjust the description based on complexity
if not complex_image and len(assistant_response) > 125:
assistant_response = assistant_response[:125] + "..."
# Display the response
st.markdown(assistant_response)
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
else:
st.error("Unexpected response format from the API.")
except requests.exceptions.HTTPError as http_err:
st.error(f"HTTP error occurred: {http_err}")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
# Warning for user action required
if not uploaded_file and analyze_button:
st.warning("Please upload an image.")