ProfessorLeVesseur commited on
Commit
047420b
·
verified ·
1 Parent(s): 8d3d28b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -82
app.py CHANGED
@@ -1,111 +1,54 @@
1
  import streamlit as st
2
  import base64
3
- # from openai import OpenAI
4
  import openai
5
 
6
-
7
  # Function to encode the image to base64
8
  def encode_image(image_file):
9
  return base64.b64encode(image_file.getvalue()).decode("utf-8")
10
 
11
- st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
12
  # Streamlit page setup
 
13
  st.title("MTSS Snapshot: Accessibility Image Textifier: `Alt Text`")
14
 
15
- # Retrieve the OpenAI API key from Streamlit secrets
16
- openai_api_key = st.secrets["openai_api_key"]
17
-
18
- # Initialize the OpenAI client with the API key
19
- # client = OpenAI(api_key=openai_api_key)
20
- client = openai.api_key(api_key=openai_api_key)
21
- # openai.api_key = openai_api_key
22
 
23
- # File uploader allows user to add their own image
24
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
25
 
26
  if uploaded_file:
27
- # Display the uploaded image
28
- with st.expander("Image", expanded = True):
29
  st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)
30
 
31
- # Toggle for showing additional details input
32
- show_details = st.toggle("Add details about the image", value=False)
33
 
34
  if show_details:
35
- # Text input for additional details about the image, shown only if toggle is True
36
- additional_details = st.text_area(
37
- "Add any additional details or context about the image here:",
38
- disabled=not show_details
39
- )
40
 
41
  # Button to trigger the analysis
42
- analyze_button = st.button("Analyse the Scientific Image", type="secondary")
43
 
44
- # Check if an image has been uploaded, if the API key is available, and if the button has been pressed
45
- if uploaded_file is not None and api_key and analyze_button:
46
-
47
- with st.spinner("Analysing the image ..."):
48
- # Encode the image
49
  base64_image = encode_image(uploaded_file)
50
-
51
- # Optimized prompt for additional clarity and detail
52
- prompt_text = (
53
- "You are a highly knowledgeable accessibility specialist. "
54
- "Your task is to examine the following image in detail. "
55
- "Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
56
- "Highlight key elements and their significance, and present your analysis in clear, well-structured format. "
57
- "Assume the reader has a basic understanding of scientific concepts."
58
- "Create a detailed image caption using 150 words or less."
59
- )
60
-
61
  if show_details and additional_details:
62
- prompt_text += (
63
- f"\n\nAdditional Context Provided by the User:\n{additional_details}"
64
- )
65
-
66
- # Create the payload for the completion request
67
- messages = [
68
- {
69
- "role": "user",
70
- "content": [
71
- {"type": "text", "text": prompt_text},
72
- {
73
- "type": "image_url",
74
- "image_url": f"data:image/jpeg;base64,{base64_image}",
75
- },
76
- ],
77
- }
78
- ]
79
-
80
- # Make the request to the OpenAI API
81
  try:
82
- # Without Stream
83
-
84
- # response = client.chat.completions.create(
85
- # model="gpt-4-vision-preview", messages=messages, max_tokens=500, stream=False
86
- # )
87
-
88
- # Stream the response
89
- full_response = ""
90
- message_placeholder = st.empty()
91
- for completion in client.chat.completions.create(
92
- model="gpt-4-vision-preview", messages=messages,
93
- max_tokens=1200, stream=True
94
- ):
95
- # Check if there is content to display
96
- if completion.choices[0].delta.content is not None:
97
- full_response += completion.choices[0].delta.content
98
- message_placeholder.markdown(full_response + "▌")
99
- # Final update to placeholder after the stream ends
100
- message_placeholder.markdown(full_response)
101
-
102
- # Display the response in the app
103
- # st.write(response.choices[0].message.content)
104
  except Exception as e:
105
  st.error(f"An error occurred: {e}")
106
  else:
107
- # Warnings for user action required
108
- if not uploaded_file and analyze_button:
109
  st.warning("Please upload an image.")
110
- if not api_key:
111
- st.warning("Please enter your OpenAI API key.")
 
1
  import streamlit as st
2
  import base64
 
3
  import openai
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="collapsed")
11
  st.title("MTSS Snapshot: Accessibility Image Textifier: `Alt Text`")
12
 
13
+ # Retrieve the OpenAI API key from Streamlit secrets and set it
14
+ openai.api_key = st.secrets["openai_api_key"]
 
 
 
 
 
15
 
16
+ # File uploader for images
17
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
18
 
19
  if uploaded_file:
20
+ with st.expander("Image", expanded=True):
 
21
  st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)
22
 
23
+ # Toggle for additional details input
24
+ show_details = st.checkbox("Add details about the image")
25
 
26
  if show_details:
27
+ additional_details = st.text_area("Add any additional details or context about the image here:")
 
 
 
 
28
 
29
  # Button to trigger the analysis
30
+ analyze_button = st.button("Analyse the Scientific Image")
31
 
32
+ if uploaded_file and openai.api_key and analyze_button:
33
+ with st.spinner("Analysing the image..."):
 
 
 
34
  base64_image = encode_image(uploaded_file)
35
+ prompt_text = "You are a highly knowledgeable accessibility specialist. [Your detailed prompt here]"
36
+
 
 
 
 
 
 
 
 
 
37
  if show_details and additional_details:
38
+ prompt_text += f"\n\nAdditional Context Provided by the User:\n{additional_details}"
39
+
40
+ # Define the messages payload
41
+ messages = [{
42
+ "role": "user",
43
+ "content": [{"type": "text", "text": prompt_text}, {"type": "image_url", "image_url": f"data:image/jpeg;base64,{base64_image}"}]
44
+ }]
45
+
 
 
 
 
 
 
 
 
 
 
 
46
  try:
47
+ # Make the request to OpenAI and handle streaming if required
48
+ response = openai.ChatCompletion.create(model="gpt-4-vision-preview", messages=messages, max_tokens=1200)
49
+ st.write(response.choices[0].message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  except Exception as e:
51
  st.error(f"An error occurred: {e}")
52
  else:
53
+ if not uploaded_file:
 
54
  st.warning("Please upload an image.")