File size: 4,910 Bytes
5f1f9d6
4c77641
5f1f9d6
7956ee3
 
48118a5
7956ee3
 
 
 
 
 
 
 
 
 
 
 
 
5f1f9d6
7956ee3
5f1f9d6
 
7956ee3
5f1f9d6
 
 
 
 
 
 
 
 
7956ee3
 
5f1f9d6
 
 
 
 
 
 
 
 
7956ee3
5f1f9d6
 
 
 
 
 
 
7956ee3
 
5f1f9d6
 
 
d5a2742
5f1f9d6
7956ee3
 
 
 
 
 
 
 
 
5f1f9d6
 
 
7956ee3
bbec170
 
7956ee3
 
 
 
 
 
 
 
 
 
bbec170
 
7956ee3
 
 
bbec170
7956ee3
bbec170
7956ee3
bbec170
7956ee3
 
 
 
 
bbec170
7956ee3
bbec170
7956ee3
 
 
bbec170
 
5f1f9d6
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import streamlit as st
import requests

# Hugging Face API setup
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision-Instruct"
headers = {"Authorization": f"Bearer {st.secrets['huggingface_api_key']}"}

# Function to query the model
def query_image(image_data, prompt_text):
    # Prepare the payload
    payload = {
        "inputs": {
            "image": image_data,
            "text": prompt_text
        }
    }
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

# 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  # Desired width in pixels
st.image('MTSS.ai_Logo.png', width=image_width)

st.header('VisionTexts™ | Accessibility')
st.subheader('Image Alt Text Creator')

# File uploader
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])

if uploaded_file:
    # Display the uploaded image
    image_width = 200  # 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 specific information important for 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:
    st.caption(
        "By selecting this, the app will create a description exceeding the 125-character limit. "
        "Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box."
    )

# Button to trigger the analysis
analyze_button = st.button("Analyze the Image")

# Optimized prompt for complex images
complex_image_prompt_text = (
    "As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
    "Provide a brief description using not more than 500 characters that convey the essential information conveyed by the image in eight or fewer clear and concise sentences. "
    "Skip phrases like 'image of' or 'picture of.' "
    "Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
)

# Check if an image has been uploaded and if the button has been pressed
if uploaded_file is not None and analyze_button:

    with st.spinner("Analyzing the image ..."):
        # Read the image file
        image_bytes = uploaded_file.read()

        # Determine which prompt to use based on the complexity of the image
        if complex_image:
            prompt_text = complex_image_prompt_text
        else:
            prompt_text = (
                "As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
                "Provide a brief description using not more than 125 characters that convey the essential information conveyed by the image in three or fewer clear and concise sentences for use as alt text. "
                "Skip phrases like 'image of' or 'picture of.' "
                "Your description should form a clear, well-structured, and factual paragraph that avoids bullet points and newlines, focusing on creating a seamless narrative that serves as effective alternative text for accessibility purposes."
            )

        if show_details and additional_details:
            prompt_text += (
                f"\n\nInclude the additional context provided by the user in your description:\n{additional_details}"
            )

        # Query the model
        try:
            response = query_image(image_bytes, prompt_text)

            # Extract the generated text from the response
            if isinstance(response, dict) and 'generated_text' in response:
                alt_text = response['generated_text']
            elif isinstance(response, list) and 'generated_text' in response[0]:
                alt_text = response[0]['generated_text']
            else:
                alt_text = "No description generated."

            # Display the generated alt text
            st.markdown(f"**Generated Alt Text:** {alt_text}")
            st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
        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.")