Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,9 +23,10 @@ openai.api_key = st.secrets["openai_api_key"]
|
|
23 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
24 |
|
25 |
if uploaded_file:
|
26 |
-
# Display the uploaded image
|
27 |
-
|
28 |
-
|
|
|
29 |
|
30 |
# Toggle for showing additional details input
|
31 |
show_details = st.toggle("Add details about the image", value=False)
|
@@ -40,79 +41,6 @@ if show_details:
|
|
40 |
# Button to trigger the analysis
|
41 |
analyze_button = st.button("Analyze the Image", type="secondary")
|
42 |
|
43 |
-
# # Check if an image has been uploaded, if the API key is available, and if the button has been pressed
|
44 |
-
# if uploaded_file is not None and analyze_button:
|
45 |
-
|
46 |
-
# with st.spinner("Analyzing the image ..."):
|
47 |
-
# # Encode the image
|
48 |
-
# base64_image = encode_image(uploaded_file)
|
49 |
-
|
50 |
-
# # Optimized prompt for additional clarity and detail
|
51 |
-
# prompt_text = (
|
52 |
-
# "You are a highly knowledgeable accessibility expert. "
|
53 |
-
# "Your task is to examine the following image in detail. "
|
54 |
-
# "Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
|
55 |
-
# "Highlight key elements and their significance, and present your analysis in clear, well-structured paragraph format. "
|
56 |
-
# "Create a detailed image caption in explaining in 150 words or less."
|
57 |
-
# )
|
58 |
-
|
59 |
-
# if show_details and additional_details:
|
60 |
-
# prompt_text += (
|
61 |
-
# f"\n\nAdditional Context Provided by the User:\n{additional_details}"
|
62 |
-
# )
|
63 |
-
|
64 |
-
# # Create the payload for the completion request
|
65 |
-
# messages = [
|
66 |
-
# {
|
67 |
-
# "role": "user",
|
68 |
-
# "content": [
|
69 |
-
# {"type": "text", "text": prompt_text},
|
70 |
-
# {
|
71 |
-
# "type": "image_url",
|
72 |
-
# "image_url": f"data:image/jpeg;base64,{base64_image}",
|
73 |
-
# },
|
74 |
-
# ],
|
75 |
-
# }
|
76 |
-
# ]
|
77 |
-
|
78 |
-
# # Make the request to the OpenAI API
|
79 |
-
# try:
|
80 |
-
# # Without Stream
|
81 |
-
|
82 |
-
# # response = openai.chat.completions.create(
|
83 |
-
# # model="gpt-4-vision-preview", messages=messages, max_tokens=500, stream=False
|
84 |
-
# # )
|
85 |
-
|
86 |
-
# # Stream the response
|
87 |
-
# full_response = ""
|
88 |
-
# message_placeholder = st.empty()
|
89 |
-
# for completion in openai.chat.completions.create(
|
90 |
-
# model="gpt-4-vision-preview", messages=messages,
|
91 |
-
# max_tokens=150, stream=True
|
92 |
-
# ):
|
93 |
-
# # Check if there is content to display
|
94 |
-
# if completion.choices[0].delta.content is not None:
|
95 |
-
# full_response += completion.choices[0].delta.content
|
96 |
-
# message_placeholder.markdown(full_response + "▌")
|
97 |
-
# # Final update to placeholder after the stream ends
|
98 |
-
# message_placeholder.markdown(full_response)
|
99 |
-
|
100 |
-
# # Display the response in the app
|
101 |
-
# # st.write(response.choices[0].message.content)
|
102 |
-
# except Exception as e:
|
103 |
-
# st.error(f"An error occurred: {e}")
|
104 |
-
# else:
|
105 |
-
# # Warnings for user action required
|
106 |
-
# if not uploaded_file and analyze_button:
|
107 |
-
# st.warning("Please upload an image.")
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
# Check if an image has been uploaded, if the API key is available, and if the button has been pressed
|
117 |
if uploaded_file is not None and analyze_button:
|
118 |
|
@@ -126,7 +54,7 @@ if uploaded_file is not None and analyze_button:
|
|
126 |
"Your task is to examine the following image in detail. "
|
127 |
"Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
|
128 |
"Highlight key elements and their significance, and present your analysis in clear, well-structured paragraph format. "
|
129 |
-
"Create a detailed image caption
|
130 |
)
|
131 |
|
132 |
if show_details and additional_details:
|
@@ -163,6 +91,13 @@ if uploaded_file is not None and analyze_button:
|
|
163 |
model="gpt-4-vision-preview", messages=messages,
|
164 |
max_tokens=150, stream=True
|
165 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
# Check if there is content to display
|
167 |
if completion.choices[0].delta.content is not None:
|
168 |
full_response += completion.choices[0].delta.content
|
|
|
23 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
24 |
|
25 |
if uploaded_file:
|
26 |
+
# Display the uploaded image with specified width
|
27 |
+
image_width = 300 # Set the desired width in pixels
|
28 |
+
with st.expander("Image", expanded=True):
|
29 |
+
st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
|
30 |
|
31 |
# Toggle for showing additional details input
|
32 |
show_details = st.toggle("Add details about the image", value=False)
|
|
|
41 |
# Button to trigger the analysis
|
42 |
analyze_button = st.button("Analyze the 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 analyze_button:
|
46 |
|
|
|
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 paragraph format. "
|
57 |
+
"Create a detailed image caption explaining in 150 words or less, using no bullet points, just one paragraph."
|
58 |
)
|
59 |
|
60 |
if show_details and additional_details:
|
|
|
91 |
model="gpt-4-vision-preview", messages=messages,
|
92 |
max_tokens=150, stream=True
|
93 |
):
|
94 |
+
# # Check if there is content to display
|
95 |
+
# if completion.choices[0].delta.content is not None:
|
96 |
+
# full_response += completion.choices[0].delta.content
|
97 |
+
# message_placeholder.markdown(full_response + "▌")
|
98 |
+
# # Final update to placeholder after the stream ends
|
99 |
+
# message_placeholder.markdown(full_response) # stream text
|
100 |
+
|
101 |
# Check if there is content to display
|
102 |
if completion.choices[0].delta.content is not None:
|
103 |
full_response += completion.choices[0].delta.content
|