Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -109,32 +109,108 @@ class AIShoppingAnalyzer:
|
|
| 109 |
if specific_product:
|
| 110 |
query += f"\n5. Detailed analysis of this specific product: {specific_product}"
|
| 111 |
|
| 112 |
-
# Initialize agents
|
| 113 |
websurfer = self.create_websurfer()
|
| 114 |
assistant = self.create_assistant()
|
| 115 |
|
| 116 |
# Create team
|
| 117 |
team = self.create_team(websurfer, assistant)
|
| 118 |
-
|
| 119 |
-
# Run the analysis
|
| 120 |
-
result = []
|
| 121 |
-
async for message in team.run_stream(task=query):
|
| 122 |
-
result.append(message)
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
except Exception as e:
|
| 127 |
-
return f"Analysis error: {str(e)}"
|
| 128 |
finally:
|
| 129 |
if websurfer:
|
| 130 |
try:
|
| 131 |
await websurfer.close()
|
| 132 |
except Exception as e:
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
def create_gradio_interface() -> gr.Interface:
|
| 136 |
"""Create the Gradio interface for the AI Shopping Analyzer"""
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def validate_api_key(api_key: str) -> bool:
|
| 139 |
"""Validate the OpenAI API key format"""
|
| 140 |
return api_key.startswith("sk-") and len(api_key) > 20
|
|
|
|
| 109 |
if specific_product:
|
| 110 |
query += f"\n5. Detailed analysis of this specific product: {specific_product}"
|
| 111 |
|
| 112 |
+
# Initialize agents with automatic browser management
|
| 113 |
websurfer = self.create_websurfer()
|
| 114 |
assistant = self.create_assistant()
|
| 115 |
|
| 116 |
# Create team
|
| 117 |
team = self.create_team(websurfer, assistant)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# Modified execution to handle EOF errors
|
| 120 |
+
try:
|
| 121 |
+
result = []
|
| 122 |
+
async for message in team.run_stream(task=query):
|
| 123 |
+
if isinstance(message, str):
|
| 124 |
+
result.append(message)
|
| 125 |
+
else:
|
| 126 |
+
result.append(str(message))
|
| 127 |
+
return "\n".join(result)
|
| 128 |
+
except EOFError:
|
| 129 |
+
return "Analysis completed with some limitations. Please try again if results are incomplete."
|
| 130 |
+
except Exception as e:
|
| 131 |
+
return f"Analysis error: {str(e)}"
|
| 132 |
|
|
|
|
|
|
|
| 133 |
finally:
|
| 134 |
if websurfer:
|
| 135 |
try:
|
| 136 |
await websurfer.close()
|
| 137 |
except Exception as e:
|
| 138 |
+
print(f"Cleanup error: {str(e)}")
|
| 139 |
+
# Continue even if cleanup fails
|
| 140 |
|
| 141 |
def create_gradio_interface() -> gr.Interface:
|
| 142 |
"""Create the Gradio interface for the AI Shopping Analyzer"""
|
| 143 |
|
| 144 |
+
css = """
|
| 145 |
+
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap');
|
| 146 |
+
|
| 147 |
+
body {
|
| 148 |
+
font-family: 'Open Sans', sans-serif !important;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
.dashboard-container {
|
| 152 |
+
border: 1px solid #e0e5ff;
|
| 153 |
+
border-radius: 8px;
|
| 154 |
+
background-color: #ffffff;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
.token-header {
|
| 158 |
+
font-size: 1.25rem;
|
| 159 |
+
font-weight: 600;
|
| 160 |
+
margin-top: 1rem;
|
| 161 |
+
margin-bottom: 0.5rem;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.feature-button {
|
| 165 |
+
display: inline-block;
|
| 166 |
+
margin: 0.25rem;
|
| 167 |
+
padding: 0.5rem 1rem;
|
| 168 |
+
background-color: #f3f4f6;
|
| 169 |
+
border: 1px solid #e5e7eb;
|
| 170 |
+
border-radius: 0.375rem;
|
| 171 |
+
font-size: 0.875rem;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
.feature-button:hover {
|
| 175 |
+
background-color: #e5e7eb;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
/* Custom styling for form elements */
|
| 179 |
+
.gr-form {
|
| 180 |
+
background: transparent !important;
|
| 181 |
+
border: none !important;
|
| 182 |
+
box-shadow: none !important;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.gr-input, .gr-textarea {
|
| 186 |
+
border: 1px solid #e5e7eb !important;
|
| 187 |
+
border-radius: 6px !important;
|
| 188 |
+
padding: 8px 12px !important;
|
| 189 |
+
font-size: 14px !important;
|
| 190 |
+
transition: all 0.2s !important;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
.gr-input:focus, .gr-textarea:focus {
|
| 194 |
+
border-color: #4c4ce3 !important;
|
| 195 |
+
outline: none !important;
|
| 196 |
+
box-shadow: 0 0 0 2px rgba(76, 76, 227, 0.2) !important;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.gr-button {
|
| 200 |
+
background-color: #4c4ce3 !important;
|
| 201 |
+
color: white !important;
|
| 202 |
+
border-radius: 6px !important;
|
| 203 |
+
padding: 8px 16px !important;
|
| 204 |
+
font-size: 14px !important;
|
| 205 |
+
font-weight: 600 !important;
|
| 206 |
+
transition: all 0.2s !important;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
.gr-button:hover {
|
| 210 |
+
background-color: #3a3ab8 !important;
|
| 211 |
+
}
|
| 212 |
+
"""
|
| 213 |
+
|
| 214 |
def validate_api_key(api_key: str) -> bool:
|
| 215 |
"""Validate the OpenAI API key format"""
|
| 216 |
return api_key.startswith("sk-") and len(api_key) > 20
|