File size: 2,163 Bytes
f76d77f
 
ea6a4e1
 
f76d77f
 
 
 
 
047420b
f76d77f
 
047420b
 
f76d77f
047420b
f76d77f
 
 
047420b
f76d77f
 
047420b
 
f76d77f
 
047420b
f76d77f
 
047420b
f76d77f
047420b
 
f76d77f
047420b
 
f76d77f
047420b
 
 
 
 
 
 
 
f76d77f
047420b
 
 
f76d77f
 
 
047420b
f76d77f
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
import streamlit as st
import base64
import openai

# Function to encode the image to base64
def encode_image(image_file):
    return base64.b64encode(image_file.getvalue()).decode("utf-8")

# Streamlit page setup
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
st.title("MTSS Snapshot: Accessibility Image Textifier: `Alt Text`")

# Retrieve the OpenAI API key from Streamlit secrets and set it
openai.api_key = st.secrets["openai_api_key"]

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

if uploaded_file:
    with st.expander("Image", expanded=True):
        st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)

# Toggle for additional details input
show_details = st.checkbox("Add details about the image")

if show_details:
    additional_details = st.text_area("Add any additional details or context about the image here:")

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

if uploaded_file and openai.api_key and analyze_button:
    with st.spinner("Analysing the image..."):
        base64_image = encode_image(uploaded_file)
        prompt_text = "You are a highly knowledgeable accessibility specialist. [Your detailed prompt here]"

        if show_details and additional_details:
            prompt_text += f"\n\nAdditional Context Provided by the User:\n{additional_details}"

        # Define the messages payload
        messages = [{
            "role": "user",
            "content": [{"type": "text", "text": prompt_text}, {"type": "image_url", "image_url": f"data:image/jpeg;base64,{base64_image}"}]
        }]

        try:
            # Make the request to OpenAI and handle streaming if required
            response = openai.ChatCompletion.create(model="gpt-4-vision-preview", messages=messages, max_tokens=1200)
            st.write(response.choices[0].message.content)
        except Exception as e:
            st.error(f"An error occurred: {e}")
else:
    if not uploaded_file:
        st.warning("Please upload an image.")