-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (171 loc) · 6.95 KB
/
main.py
File metadata and controls
214 lines (171 loc) · 6.95 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import streamlit as st
import requests
from PIL import Image
from io import BytesIO
from model_ai import generate_prompt
def is_valid_image_url(url):
"""Check if URL contains a valid image."""
try:
response = requests.get(url, timeout=5)
if response.status_code != 200:
return False
content_type = response.headers.get('content-type', '')
if not content_type.startswith('image/'):
return False
Image.open(BytesIO(response.content))
return True
except Exception:
return False
def load_image_from_url(url):
try:
if not is_valid_image_url(url):
return None
response = requests.get(url)
img = Image.open(BytesIO(response.content))
return img
except Exception:
return None
def main():
st.set_page_config(
page_title="VisAI - DELIVERY PACKAGE DAMAGE DETECTION", layout="wide")
st.markdown("""
<style>
.stButton > button {
border-radius: 6px;
font-weight: 500;
}
.stTextArea > div > div > textarea {
border-radius: 6px;
}
.css-1629p8f h1 {
font-weight: 600;
}
</style>
""", unsafe_allow_html=True)
st.title(":mag_right: VisAI - DELIVERY PACKAGE DAMAGE DETECTION")
with st.sidebar:
st.header("Instructions")
st.markdown(
"""
### How to Use:
**1. Input Images**
- Enter a list of image URLs
- Separate URLs with commas
**2. Analysis**
- Click "Analyze Responsibility"
- Wait for processing
**3. Results**
- Review image validation
- Check responsibility assessment
- Read detailed analysis
⚠️ **Note:** Ensure all URLs are accessible and contain valid images
"""
)
if 'urls_input' not in st.session_state:
st.session_state['urls_input'] = ""
if 'analysis_results' not in st.session_state:
st.session_state['analysis_results'] = None
st.markdown("### 📸 Input Image URLs")
urls_input = st.text_area(
"List of Image URLs:",
value=st.session_state['urls_input'],
placeholder="Paste URLs separated by commas",
height=150,
help="Each URL should point to a valid image file",
)
st.session_state['urls_input'] = urls_input
button_cols = st.columns([5, 3, 5])
with button_cols[0]:
analyze_button = st.button(
"🔍 Analyze Responsibility", type="primary", use_container_width=True)
with button_cols[1]:
clear_button = st.button("❌Clear", use_container_width=True)
if clear_button:
st.session_state['urls_input'] = ""
st.session_state['analysis_results'] = None
if analyze_button:
if urls_input:
urls = [url.strip()
for url in urls_input.split(',') if url.strip()]
if urls:
with st.expander("📊 Analysis Details", expanded=True):
st.write(f"**Total images to analyze:** {len(urls)}")
valid_urls = []
invalid_urls = []
progress_bar = st.progress(0)
status_text = st.empty()
for idx, url in enumerate(urls):
status_text.text(
f"Processing image {idx + 1}/{len(urls)}...")
progress_bar.progress((idx + 1) / len(urls))
image = load_image_from_url(url)
if image:
valid_urls.append(url)
else:
invalid_urls.append(url)
status_text.empty()
progress_bar.empty()
# Display valid images in a grid
if valid_urls:
st.markdown("### 🖼️ Valid Images")
cols = 3
for i in range(0, len(valid_urls), cols):
columns = st.columns(cols)
for col_index, url in enumerate(valid_urls[i:i+cols]):
with columns[col_index]:
st.image(url, use_container_width=True)
st.caption(f"Image {i + col_index + 1}")
# Show invalid URLs in an expander
if invalid_urls:
with st.expander("⚠️ Invalid URLs", expanded=False):
st.error(
"The following URLs were invalid or inaccessible:")
for url in invalid_urls:
st.code(url, language="text")
# Process valid images and store results in session state
if valid_urls:
with st.spinner("Analyzing images..."):
result = generate_prompt(valid_urls)
# Store results in session state for potential future use or display.
st.session_state['analysis_results'] = result
# Display analysis results only after processing is complete.
display_analysis_results(valid_urls, invalid_urls)
else:
st.error(
"No valid images found for analysis. Please check your URLs and try again.")
else:
st.error(
"No valid image URLs provided. Please enter at least one URL.")
else:
st.error("Please provide image URLs to analyze.")
def display_analysis_results(valid_urls, invalid_urls):
"""Display the analysis results."""
result = st.session_state['analysis_results']
if result is not None:
st.markdown("### 📋 VisAI Analysis Results")
# Create a card-like container for results
results_container = st.container()
with results_container:
col1, col2 = st.columns([1, 2])
with col1:
st.markdown("**This product is damaged by:**")
case = result.get('case', 'No response')
if case == "Vendors":
st.error(case)
elif case == "Carriers":
st.warning(case)
else:
st.info(case)
with col2:
st.markdown("**Analysis:**")
st.write(result.get('reason', 'No response'))
# Add metrics
st.markdown("---")
metric_cols = st.columns(3)
temp = result.get('total_token', 0)
cal = temp*1.25/1000000
metric_cols[0].metric("Valid Images", len(valid_urls))
metric_cols[1].metric("Total Tokens", temp)
metric_cols[2].metric("Total Cost", f"${cal:.6f}")
if __name__ == "__main__":
main()