ProfessorLeVesseur commited on
Commit
7956ee3
·
verified ·
1 Parent(s): bbec170

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -62
app.py CHANGED
@@ -1,29 +1,38 @@
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",
7
- layout="centered",
8
- initial_sidebar_state="auto"
9
- )
10
 
11
  # Add the image with a specified width
12
- image_width = 300 # Set the desired width in pixels
13
  st.image('MTSS.ai_Logo.png', width=image_width)
14
 
15
  st.header('VisionTexts™ | Accessibility')
16
  st.subheader('Image Alt Text Creator')
17
 
18
- # Initialize the API key from Streamlit secrets
19
- api_key = st.secrets["huggingface_api_key"]
20
-
21
  # File uploader
22
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
23
 
24
  if uploaded_file:
25
- # Display the uploaded image with specified width
26
- image_width = 200 # Set the desired width in pixels
27
  with st.expander("Image", expanded=True):
28
  st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
29
 
@@ -33,83 +42,69 @@ show_details = st.checkbox("Add details about the image.", value=False)
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
40
  complex_image = st.checkbox("Is this a complex image?", value=False)
41
 
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:
 
1
  import streamlit as st
2
  import requests
3
 
4
+ # Hugging Face API setup
5
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision-Instruct"
6
+ headers = {"Authorization": f"Bearer {st.secrets['hf_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 # Desired width in pixels
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
  # File uploader
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_width = 200 # Desired width in pixels
36
  with st.expander("Image", expanded=True):
37
  st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
38
 
 
42
  if show_details:
43
  # Text input for additional details about the image
44
  additional_details = st.text_area(
45
+ "Include specific information important for the alt text or reflect why the image is being used:"
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 convey the essential information conveyed by the image in eight or fewer clear and concise sentences. "
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
  with st.spinner("Analyzing the image ..."):
72
+ # Read the image file
73
  image_bytes = uploaded_file.read()
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 convey the essential information conveyed by the image in three or fewer clear and concise sentences for use as alt text. "
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 that serves as effective alternative text for accessibility purposes."
84
+ )
 
 
 
 
 
85
 
 
86
  if show_details and additional_details:
87
+ prompt_text += (
88
+ f"\n\nInclude the additional context provided by the user in your description:\n{additional_details}"
89
+ )
90
 
91
+ # Query the model
92
  try:
93
+ response = query_image(image_bytes, prompt_text)
 
 
 
 
 
 
 
94
 
95
+ # Extract the generated text from the response
96
+ if isinstance(response, dict) and 'generated_text' in response:
97
+ alt_text = response['generated_text']
98
+ elif isinstance(response, list) and 'generated_text' in response[0]:
99
+ alt_text = response[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
100
  else:
101
+ alt_text = "No description generated."
102
 
103
+ # Display the generated alt text
104
+ st.markdown(f"**Generated Alt Text:** {alt_text}")
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
  # Warning for user action required
110
  if not uploaded_file and analyze_button: