Update app.py
Browse files
app.py
CHANGED
|
@@ -57,28 +57,28 @@ def analyze_coordination_needs(user_message, department, user_name, image=None):
|
|
| 57 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 58 |
|
| 59 |
prompt = f"""
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
try:
|
| 84 |
response = requests.post(
|
|
@@ -123,25 +123,25 @@ def analyze_coordination_needs(user_message, department, user_name, image=None):
|
|
| 123 |
else:
|
| 124 |
# Text-only analysis
|
| 125 |
prompt = f"""
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
|
| 146 |
try:
|
| 147 |
response = requests.post(
|
|
@@ -176,48 +176,50 @@ def send_department_email(sender_dept, recipient_dept, subject, message, user_na
|
|
| 176 |
return f"β No email configured for {departments[recipient_dept]['name']}"
|
| 177 |
|
| 178 |
# Create professional email content
|
|
|
|
|
|
|
| 179 |
html_content = f"""
|
| 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 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
|
| 222 |
# Send email
|
| 223 |
params = {
|
|
@@ -251,67 +253,26 @@ def send_email_report(conversation_data):
|
|
| 251 |
# Format email content
|
| 252 |
subject = f"Coordination Report - {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
| 253 |
|
| 254 |
-
# Create
|
| 255 |
-
|
| 256 |
-
<html>
|
| 257 |
-
<head>
|
| 258 |
-
<style>
|
| 259 |
-
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 260 |
-
.header {{ background-color: #1f2937; color: white; padding: 25px; border-radius: 8px; margin-bottom: 20px; }}
|
| 261 |
-
.stats {{ display: flex; justify-content: space-between; margin: 20px 0; }}
|
| 262 |
-
.stat-card {{ background-color: #f8fafc; padding: 15px; border-radius: 8px; text-align: center; flex: 1; margin: 0 10px; }}
|
| 263 |
-
.exchange {{ border: 1px solid #e0e0e0; margin: 15px 0; padding: 15px; border-radius: 8px; background-color: #fafafa; }}
|
| 264 |
-
.user {{ color: #2563eb; font-weight: 600; }}
|
| 265 |
-
.ai {{ color: #059669; }}
|
| 266 |
-
.timestamp {{ color: #6b7280; font-size: 0.8em; font-style: italic; }}
|
| 267 |
-
.department-badge {{ background-color: #3b82f6; color: white; padding: 2px 8px; border-radius: 12px; font-size: 0.8em; }}
|
| 268 |
-
.user-badge {{ background-color: #10b981; color: white; padding: 2px 8px; border-radius: 12px; font-size: 0.8em; margin-left: 5px; }}
|
| 269 |
-
.image-indicator {{ background-color: #f59e0b; color: white; padding: 2px 6px; border-radius: 10px; font-size: 0.7em; margin-left: 5px; }}
|
| 270 |
-
</style>
|
| 271 |
-
</head>
|
| 272 |
-
<body>
|
| 273 |
-
<div class="header">
|
| 274 |
-
<h1 style="margin: 0;">π€ AI Coordination System Report</h1>
|
| 275 |
-
<p style="margin: 5px 0; opacity: 0.9;">Comprehensive inter-department coordination summary</p>
|
| 276 |
-
<p style="margin: 5px 0;"><strong>Generated:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 277 |
-
</div>
|
| 278 |
-
|
| 279 |
-
<div class="stats">
|
| 280 |
-
<div class="stat-card">
|
| 281 |
-
<h3 style="margin: 0; color: #3b82f6;">{len(conversation_data)}</h3>
|
| 282 |
-
<p style="margin: 5px 0;">Total Interactions</p>
|
| 283 |
-
</div>
|
| 284 |
-
<div class="stat-card">
|
| 285 |
-
<h3 style="margin: 0; color: #10b981;">{len(dept_activity)}</h3>
|
| 286 |
-
<p style="margin: 5px 0;">Active Departments</p>
|
| 287 |
-
</div>
|
| 288 |
-
<div class="stat-card">
|
| 289 |
-
<h3 style="margin: 0; color: #f59e0b;">{sum(1 for entry in conversation_history if entry.get('email_sent'))}</h3>
|
| 290 |
-
<p style="margin: 5px 0;">Emails Sent</p>
|
| 291 |
-
</div>
|
| 292 |
-
</div>
|
| 293 |
-
"""
|
| 294 |
-
|
| 295 |
-
# Add department activity breakdown
|
| 296 |
-
html_content += "<h3>π Department Activity</h3>"
|
| 297 |
for dept, count in dept_activity.items():
|
| 298 |
-
|
| 299 |
|
| 300 |
-
#
|
| 301 |
-
|
| 302 |
for user, count in user_activity.items():
|
| 303 |
if user != 'Unknown':
|
| 304 |
-
|
| 305 |
|
| 306 |
-
#
|
| 307 |
-
|
| 308 |
for i, entry in enumerate(conversation_data[-10:], 1):
|
| 309 |
dept_badge = f"<span class='department-badge'>{entry.get('department', 'General').title()}</span>" if entry.get('department') else ""
|
| 310 |
user_badge = f"<span class='user-badge'>{entry.get('user_name', 'User')}</span>" if entry.get('user_name') else ""
|
| 311 |
image_indicator = " <span class='image-indicator'>π·</span>" if entry.get('image_uploaded') else ""
|
| 312 |
email_indicator = " π§" if entry.get('email_sent') else ""
|
| 313 |
|
| 314 |
-
|
| 315 |
<div class="exchange">
|
| 316 |
<div style="display: flex; justify-content: between; align-items: center; margin-bottom: 10px;">
|
| 317 |
<h4 style="margin: 0; color: #374151;">Activity #{i}</h4>
|
|
@@ -327,13 +288,61 @@ def send_email_report(conversation_data):
|
|
| 327 |
</div>
|
| 328 |
"""
|
| 329 |
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
# Send email
|
| 339 |
params = {
|
|
@@ -404,7 +413,7 @@ Message:
|
|
| 404 |
---
|
| 405 |
AI Coordination Suggestions:
|
| 406 |
{ai_analysis}
|
| 407 |
-
|
| 408 |
email_status = send_department_email(department, recipient_dept, subject, email_message, name, urgency, image)
|
| 409 |
response_parts.append(f"\n**π§ Email Status:** {email_status}")
|
| 410 |
|
|
@@ -508,7 +517,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
|
| 508 |
message_input = gr.Textbox(
|
| 509 |
label="Coordination Message",
|
| 510 |
lines=4,
|
| 511 |
-
placeholder="Describe what you need from other departments...
|
| 512 |
max_lines=6,
|
| 513 |
info="Be specific about what coordination you need"
|
| 514 |
)
|
|
|
|
| 57 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 58 |
|
| 59 |
prompt = f"""
|
| 60 |
+
As an AI coordination agent, analyze this message from {user_name} in the {department} department.
|
| 61 |
+
Also analyze the attached image and provide comprehensive coordination suggestions.
|
| 62 |
+
|
| 63 |
+
DEPARTMENT: {department}
|
| 64 |
+
REQUESTOR: {user_name}
|
| 65 |
+
MESSAGE: {user_message}
|
| 66 |
+
|
| 67 |
+
Analyze both the text message and the image to understand:
|
| 68 |
+
1. Which other departments need to be involved?
|
| 69 |
+
2. What specific actions should be taken based on both text and visual information?
|
| 70 |
+
3. What information needs to be shared between departments?
|
| 71 |
+
4. What are the priority levels?
|
| 72 |
+
5. What follow-up is needed?
|
| 73 |
+
6. Any insights from the image that affect coordination?
|
| 74 |
+
|
| 75 |
+
Provide a structured response with:
|
| 76 |
+
- Coordination needed with: [departments]
|
| 77 |
+
- Image analysis insights: [key points from image]
|
| 78 |
+
- Recommended actions: [bullet points]
|
| 79 |
+
- Priority: [High/Medium/Low]
|
| 80 |
+
- Next steps: [suggestions]
|
| 81 |
+
"""
|
| 82 |
|
| 83 |
try:
|
| 84 |
response = requests.post(
|
|
|
|
| 123 |
else:
|
| 124 |
# Text-only analysis
|
| 125 |
prompt = f"""
|
| 126 |
+
As an AI coordination agent, analyze this message from {user_name} in the {department} department and suggest appropriate actions:
|
| 127 |
+
|
| 128 |
+
DEPARTMENT: {department}
|
| 129 |
+
REQUESTOR: {user_name}
|
| 130 |
+
MESSAGE: {user_message}
|
| 131 |
+
|
| 132 |
+
Consider:
|
| 133 |
+
1. Which other departments need to be involved?
|
| 134 |
+
2. What specific actions should be taken?
|
| 135 |
+
3. What information needs to be shared?
|
| 136 |
+
4. What are the priority levels?
|
| 137 |
+
5. What follow-up is needed?
|
| 138 |
+
|
| 139 |
+
Provide a structured response with:
|
| 140 |
+
- Coordination needed with: [departments]
|
| 141 |
+
- Recommended actions: [bullet points]
|
| 142 |
+
- Priority: [High/Medium/Low]
|
| 143 |
+
- Next steps: [suggestions]
|
| 144 |
+
"""
|
| 145 |
|
| 146 |
try:
|
| 147 |
response = requests.post(
|
|
|
|
| 176 |
return f"β No email configured for {departments[recipient_dept]['name']}"
|
| 177 |
|
| 178 |
# Create professional email content
|
| 179 |
+
image_note = "<p style='margin: 5px 0;'><strong>π Attachment:</strong> Image included with request</p>" if image else ""
|
| 180 |
+
|
| 181 |
html_content = f"""
|
| 182 |
+
<html>
|
| 183 |
+
<head>
|
| 184 |
+
<style>
|
| 185 |
+
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 186 |
+
.header {{ background-color: #f4f4f4; padding: 20px; border-radius: 8px; margin-bottom: 20px; }}
|
| 187 |
+
.urgency-high {{ border-left: 5px solid #dc2626; background-color: #fef2f2; }}
|
| 188 |
+
.urgency-medium {{ border-left: 5px solid #d97706; background-color: #fffbeb; }}
|
| 189 |
+
.urgency-normal {{ border-left: 5px solid #2563eb; background-color: #eff6ff; }}
|
| 190 |
+
.message-content {{ padding: 20px; background-color: white; border-radius: 8px; margin: 15px 0; }}
|
| 191 |
+
.user-info {{ background-color: #f8fafc; padding: 10px 15px; border-radius: 6px; margin: 10px 0; }}
|
| 192 |
+
</style>
|
| 193 |
+
</head>
|
| 194 |
+
<body>
|
| 195 |
+
<div class="header">
|
| 196 |
+
<h1 style="margin: 0; color: #1f2937;">π’ Inter-Department Communication</h1>
|
| 197 |
+
<p style="margin: 5px 0;"><strong>From:</strong> {departments[sender_dept]['name']}</p>
|
| 198 |
+
<p style="margin: 5px 0;"><strong>Requested by:</strong> {user_name}</p>
|
| 199 |
+
<p style="margin: 5px 0;"><strong>To:</strong> {departments[recipient_dept]['name']}</p>
|
| 200 |
+
<p style="margin: 5px 0;"><strong>Date:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 201 |
+
<p style="margin: 5px 0;"><strong>Urgency:</strong> {urgency}</p>
|
| 202 |
+
{image_note}
|
| 203 |
+
</div>
|
| 204 |
+
|
| 205 |
+
<div class="user-info">
|
| 206 |
+
<strong>π€ Requestor Details:</strong><br>
|
| 207 |
+
Name: {user_name}<br>
|
| 208 |
+
Department: {departments[sender_dept]['name']}
|
| 209 |
+
</div>
|
| 210 |
+
|
| 211 |
+
<div class="message-content urgency-{urgency.lower() if urgency.lower() in ['high', 'medium'] else 'normal'}">
|
| 212 |
+
<h3 style="margin-top: 0;">{subject}</h3>
|
| 213 |
+
<div style="white-space: pre-line;">{message}</div>
|
| 214 |
+
</div>
|
| 215 |
+
|
| 216 |
+
<div style="color: #6b7280; font-size: 0.9em; margin-top: 20px;">
|
| 217 |
+
<p>This is an automated message from the AI Coordination System.</p>
|
| 218 |
+
{"<p><em>Note: An image was included with this request. Please check the original system for image details.</em></p>" if image else ""}
|
| 219 |
+
</div>
|
| 220 |
+
</body>
|
| 221 |
+
</html>
|
| 222 |
+
"""
|
| 223 |
|
| 224 |
# Send email
|
| 225 |
params = {
|
|
|
|
| 253 |
# Format email content
|
| 254 |
subject = f"Coordination Report - {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
| 255 |
|
| 256 |
+
# Create department activity HTML
|
| 257 |
+
dept_activity_html = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
for dept, count in dept_activity.items():
|
| 259 |
+
dept_activity_html += f"<p><strong>{dept.title()}:</strong> {count} interactions</p>"
|
| 260 |
|
| 261 |
+
# Create user activity HTML
|
| 262 |
+
user_activity_html = ""
|
| 263 |
for user, count in user_activity.items():
|
| 264 |
if user != 'Unknown':
|
| 265 |
+
user_activity_html += f"<p><strong>{user}:</strong> {count} requests</p>"
|
| 266 |
|
| 267 |
+
# Create recent activities HTML
|
| 268 |
+
recent_activities_html = ""
|
| 269 |
for i, entry in enumerate(conversation_data[-10:], 1):
|
| 270 |
dept_badge = f"<span class='department-badge'>{entry.get('department', 'General').title()}</span>" if entry.get('department') else ""
|
| 271 |
user_badge = f"<span class='user-badge'>{entry.get('user_name', 'User')}</span>" if entry.get('user_name') else ""
|
| 272 |
image_indicator = " <span class='image-indicator'>π·</span>" if entry.get('image_uploaded') else ""
|
| 273 |
email_indicator = " π§" if entry.get('email_sent') else ""
|
| 274 |
|
| 275 |
+
recent_activities_html += f"""
|
| 276 |
<div class="exchange">
|
| 277 |
<div style="display: flex; justify-content: between; align-items: center; margin-bottom: 10px;">
|
| 278 |
<h4 style="margin: 0; color: #374151;">Activity #{i}</h4>
|
|
|
|
| 288 |
</div>
|
| 289 |
"""
|
| 290 |
|
| 291 |
+
# Create comprehensive HTML report
|
| 292 |
+
html_content = f"""
|
| 293 |
+
<html>
|
| 294 |
+
<head>
|
| 295 |
+
<style>
|
| 296 |
+
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 297 |
+
.header {{ background-color: #1f2937; color: white; padding: 25px; border-radius: 8px; margin-bottom: 20px; }}
|
| 298 |
+
.stats {{ display: flex; justify-content: space-between; margin: 20px 0; }}
|
| 299 |
+
.stat-card {{ background-color: #f8fafc; padding: 15px; border-radius: 8px; text-align: center; flex: 1; margin: 0 10px; }}
|
| 300 |
+
.exchange {{ border: 1px solid #e0e0e0; margin: 15px 0; padding: 15px; border-radius: 8px; background-color: #fafafa; }}
|
| 301 |
+
.user {{ color: #2563eb; font-weight: 600; }}
|
| 302 |
+
.ai {{ color: #059669; }}
|
| 303 |
+
.timestamp {{ color: #6b7280; font-size: 0.8em; font-style: italic; }}
|
| 304 |
+
.department-badge {{ background-color: #3b82f6; color: white; padding: 2px 8px; border-radius: 12px; font-size: 0.8em; }}
|
| 305 |
+
.user-badge {{ background-color: #10b981; color: white; padding: 2px 8px; border-radius: 12px; font-size: 0.8em; margin-left: 5px; }}
|
| 306 |
+
.image-indicator {{ background-color: #f59e0b; color: white; padding: 2px 6px; border-radius: 10px; font-size: 0.7em; margin-left: 5px; }}
|
| 307 |
+
</style>
|
| 308 |
+
</head>
|
| 309 |
+
<body>
|
| 310 |
+
<div class="header">
|
| 311 |
+
<h1 style="margin: 0;">π€ AI Coordination System Report</h1>
|
| 312 |
+
<p style="margin: 5px 0; opacity: 0.9;">Comprehensive inter-department coordination summary</p>
|
| 313 |
+
<p style="margin: 5px 0;"><strong>Generated:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 314 |
+
</div>
|
| 315 |
+
|
| 316 |
+
<div class="stats">
|
| 317 |
+
<div class="stat-card">
|
| 318 |
+
<h3 style="margin: 0; color: #3b82f6;">{len(conversation_data)}</h3>
|
| 319 |
+
<p style="margin: 5px 0;">Total Interactions</p>
|
| 320 |
+
</div>
|
| 321 |
+
<div class="stat-card">
|
| 322 |
+
<h3 style="margin: 0; color: #10b981;">{len(dept_activity)}</h3>
|
| 323 |
+
<p style="margin: 5px 0;">Active Departments</p>
|
| 324 |
+
</div>
|
| 325 |
+
<div class="stat-card">
|
| 326 |
+
<h3 style="margin: 0; color: #f59e0b;">{sum(1 for entry in conversation_history if entry.get('email_sent'))}</h3>
|
| 327 |
+
<p style="margin: 5px 0;">Emails Sent</p>
|
| 328 |
+
</div>
|
| 329 |
+
</div>
|
| 330 |
+
|
| 331 |
+
<h3>π Department Activity</h3>
|
| 332 |
+
{dept_activity_html}
|
| 333 |
+
|
| 334 |
+
<h3>π₯ User Activity</h3>
|
| 335 |
+
{user_activity_html if user_activity_html else "<p>No user activity data</p>"}
|
| 336 |
+
|
| 337 |
+
<h3>π Recent Coordination Activities</h3>
|
| 338 |
+
{recent_activities_html if recent_activities_html else "<p>No recent activities</p>"}
|
| 339 |
+
|
| 340 |
+
<div style="margin-top: 30px; padding: 15px; background-color: #f0fdf4; border-radius: 8px;">
|
| 341 |
+
<p style="margin: 0;"><strong>π€ AI Note:</strong> This report is automatically generated by the coordination system. All inter-department communications are logged and monitored for efficiency.</p>
|
| 342 |
+
</div>
|
| 343 |
+
</body>
|
| 344 |
+
</html>
|
| 345 |
+
"""
|
| 346 |
|
| 347 |
# Send email
|
| 348 |
params = {
|
|
|
|
| 413 |
---
|
| 414 |
AI Coordination Suggestions:
|
| 415 |
{ai_analysis}
|
| 416 |
+
"""
|
| 417 |
email_status = send_department_email(department, recipient_dept, subject, email_message, name, urgency, image)
|
| 418 |
response_parts.append(f"\n**π§ Email Status:** {email_status}")
|
| 419 |
|
|
|
|
| 517 |
message_input = gr.Textbox(
|
| 518 |
label="Coordination Message",
|
| 519 |
lines=4,
|
| 520 |
+
placeholder="Describe what you need from other departments...",
|
| 521 |
max_lines=6,
|
| 522 |
info="Be specific about what coordination you need"
|
| 523 |
)
|