Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
-
import base64
|
3 |
-
import json
|
4 |
import requests
|
5 |
|
6 |
-
# Function to read the image file
|
7 |
-
def get_image_bytes(image_file):
|
8 |
-
return image_file.read()
|
9 |
-
|
10 |
# Streamlit page setup
|
11 |
st.set_page_config(
|
12 |
page_title="MTSS Image Accessibility Alt Text Generator",
|
@@ -39,7 +33,7 @@ show_details = st.checkbox("Add details about the image.", value=False)
|
|
39 |
if show_details:
|
40 |
# Text input for additional details about the image
|
41 |
additional_details = st.text_area(
|
42 |
-
"
|
43 |
)
|
44 |
|
45 |
# Toggle for modifying the prompt for complex images
|
@@ -48,101 +42,74 @@ complex_image = st.checkbox("Is this a complex image?", value=False)
|
|
48 |
if complex_image:
|
49 |
# Caption explaining the impact of the complex image toggle
|
50 |
st.caption(
|
51 |
-
"By
|
52 |
-
"Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box."
|
53 |
)
|
54 |
|
55 |
# Button to trigger the analysis
|
56 |
analyze_button = st.button("Analyze the Image")
|
57 |
|
58 |
-
# Optimized prompt for complex images
|
59 |
-
complex_image_prompt_text = (
|
60 |
-
"As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
|
61 |
-
"Provide a brief description using not more than 500 characters that conveys the essential information in eight or fewer clear and concise sentences. "
|
62 |
-
"Skip phrases like 'image of' or 'picture of.' "
|
63 |
-
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
64 |
-
)
|
65 |
-
|
66 |
# Check if an image has been uploaded and if the analyze button has been pressed
|
67 |
if uploaded_file is not None and analyze_button:
|
68 |
|
69 |
with st.spinner("Analyzing the image ..."):
|
70 |
# Read the image bytes
|
71 |
-
image_bytes =
|
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 |
-
#
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
# Make the request to the Hugging Face API
|
119 |
-
try:
|
120 |
-
# Send the request with the image file in the 'files' parameter
|
121 |
-
response = requests.post(
|
122 |
-
api_url,
|
123 |
-
headers=headers,
|
124 |
-
data={"data": json.dumps(payload)},
|
125 |
-
files={"file": ("image", image_bytes, content_type)},
|
126 |
-
timeout=60 # Optional: increase timeout if needed
|
127 |
-
)
|
128 |
-
|
129 |
-
# Check for errors
|
130 |
-
response.raise_for_status()
|
131 |
-
|
132 |
-
# Parse the response
|
133 |
-
completion = response.json()
|
134 |
-
|
135 |
-
# Extract the assistant's response
|
136 |
-
assistant_response = completion['choices'][0]['message']['content']
|
137 |
-
|
138 |
# Display the response
|
139 |
st.markdown(assistant_response)
|
140 |
-
|
141 |
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
142 |
-
|
143 |
-
st.error(
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
146 |
else:
|
147 |
# Warning for user action required
|
148 |
if not uploaded_file and analyze_button:
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import requests
|
3 |
|
|
|
|
|
|
|
|
|
4 |
# Streamlit page setup
|
5 |
st.set_page_config(
|
6 |
page_title="MTSS Image Accessibility Alt Text Generator",
|
|
|
33 |
if show_details:
|
34 |
# Text input for additional details about the image
|
35 |
additional_details = st.text_area(
|
36 |
+
"Include any specific information that is important to include in the alt text or reflect why the image is being used:",
|
37 |
)
|
38 |
|
39 |
# Toggle for modifying the prompt for complex images
|
|
|
42 |
if complex_image:
|
43 |
# Caption explaining the impact of the complex image toggle
|
44 |
st.caption(
|
45 |
+
"By selecting this option, the app will create a detailed description that may exceed the typical 125-character limit for alt text."
|
|
|
46 |
)
|
47 |
|
48 |
# Button to trigger the analysis
|
49 |
analyze_button = st.button("Analyze the Image")
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
# Check if an image has been uploaded and if the analyze button has been pressed
|
52 |
if uploaded_file is not None and analyze_button:
|
53 |
|
54 |
with st.spinner("Analyzing the image ..."):
|
55 |
# Read the image bytes
|
56 |
+
image_bytes = uploaded_file.read()
|
57 |
+
|
58 |
+
# Decide on the model to use
|
59 |
+
model_id = "Salesforce/blip-image-captioning-base" # You can choose another model if desired
|
60 |
+
|
61 |
+
# Prepare headers and endpoint
|
62 |
+
headers = {
|
63 |
+
"Authorization": f"Bearer {api_key}",
|
64 |
+
"Content-Type": "application/octet-stream"
|
65 |
+
}
|
66 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
67 |
+
|
68 |
+
# Prepare the parameters
|
69 |
+
parameters = {
|
70 |
+
# "max_length": 50, # Adjust as needed
|
71 |
+
# "num_return_sequences": 1,
|
72 |
+
}
|
73 |
+
|
74 |
+
# Include additional details in the prompt if provided
|
75 |
+
if show_details and additional_details:
|
76 |
+
prompt_text = f"{additional_details}"
|
77 |
+
parameters["inputs"] = prompt_text
|
78 |
+
|
79 |
+
# Make the request to the Hugging Face API
|
80 |
+
try:
|
81 |
+
# Send the request with the image bytes
|
82 |
+
response = requests.post(
|
83 |
+
api_url,
|
84 |
+
headers=headers,
|
85 |
+
data=image_bytes,
|
86 |
+
params=parameters,
|
87 |
+
timeout=60 # Optional: increase timeout if needed
|
88 |
+
)
|
89 |
+
|
90 |
+
# Check for errors
|
91 |
+
response.raise_for_status()
|
92 |
+
|
93 |
+
# Parse the response
|
94 |
+
completion = response.json()
|
95 |
+
|
96 |
+
# Extract the generated description
|
97 |
+
if isinstance(completion, list) and "generated_text" in completion[0]:
|
98 |
+
assistant_response = completion[0]["generated_text"]
|
99 |
+
# Adjust the description based on complexity
|
100 |
+
if not complex_image and len(assistant_response) > 125:
|
101 |
+
assistant_response = assistant_response[:125] + "..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
# Display the response
|
103 |
st.markdown(assistant_response)
|
|
|
104 |
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
105 |
+
else:
|
106 |
+
st.error("Unexpected response format from the API.")
|
107 |
+
|
108 |
+
except requests.exceptions.HTTPError as http_err:
|
109 |
+
st.error(f"HTTP error occurred: {http_err}")
|
110 |
+
except Exception as e:
|
111 |
+
st.error(f"An error occurred: {e}")
|
112 |
+
|
113 |
else:
|
114 |
# Warning for user action required
|
115 |
if not uploaded_file and analyze_button:
|