Update app.py
Browse files
app.py
CHANGED
|
@@ -47,54 +47,128 @@ def update_report_email(new_email):
|
|
| 47 |
else:
|
| 48 |
return "β Please enter a valid email address"
|
| 49 |
|
| 50 |
-
def analyze_coordination_needs(user_message, department):
|
| 51 |
-
"""Use LLM to analyze coordination needs and suggest actions"""
|
| 52 |
-
prompt = f"""
|
| 53 |
-
As an AI coordination agent, analyze this message from {department} department and suggest appropriate actions:
|
| 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 |
-
"model": "google/gemma-3-27b-it:free",
|
| 81 |
-
"messages": [{"role": "user", "content": prompt}],
|
| 82 |
-
"temperature": 0.3
|
| 83 |
-
},
|
| 84 |
-
timeout=30
|
| 85 |
-
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
def send_department_email(sender_dept, recipient_dept, subject, message, urgency="Normal"):
|
| 97 |
-
"""Send email between departments"""
|
| 98 |
try:
|
| 99 |
# Determine recipient email
|
| 100 |
recipient_email = departments[recipient_dept]["email"]
|
|
@@ -112,15 +186,24 @@ def send_department_email(sender_dept, recipient_dept, subject, message, urgency
|
|
| 112 |
.urgency-medium {{ border-left: 5px solid #d97706; background-color: #fffbeb; }}
|
| 113 |
.urgency-normal {{ border-left: 5px solid #2563eb; background-color: #eff6ff; }}
|
| 114 |
.message-content {{ padding: 20px; background-color: white; border-radius: 8px; margin: 15px 0; }}
|
|
|
|
| 115 |
</style>
|
| 116 |
</head>
|
| 117 |
<body>
|
| 118 |
<div class="header">
|
| 119 |
<h1 style="margin: 0; color: #1f2937;">π’ Inter-Department Communication</h1>
|
| 120 |
<p style="margin: 5px 0;"><strong>From:</strong> {departments[sender_dept]['name']}</p>
|
|
|
|
| 121 |
<p style="margin: 5px 0;"><strong>To:</strong> {departments[recipient_dept]['name']}</p>
|
| 122 |
<p style="margin: 5px 0;"><strong>Date:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 123 |
<p style="margin: 5px 0;"><strong>Urgency:</strong> {urgency}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
</div>
|
| 125 |
|
| 126 |
<div class="message-content {f'urgency-{urgency.lower()}' if urgency.lower() in ['high', 'medium'] else 'urgency-normal'}">
|
|
@@ -130,6 +213,7 @@ def send_department_email(sender_dept, recipient_dept, subject, message, urgency
|
|
| 130 |
|
| 131 |
<div style="color: #6b7280; font-size: 0.9em; margin-top: 20px;">
|
| 132 |
<p>This is an automated message from the AI Coordination System.</p>
|
|
|
|
| 133 |
</div>
|
| 134 |
</body>
|
| 135 |
</html>
|
|
@@ -157,9 +241,12 @@ def send_email_report(conversation_data):
|
|
| 157 |
try:
|
| 158 |
# Calculate department activity
|
| 159 |
dept_activity = {}
|
|
|
|
| 160 |
for entry in conversation_data:
|
| 161 |
dept = entry.get('department', 'Unknown')
|
|
|
|
| 162 |
dept_activity[dept] = dept_activity.get(dept, 0) + 1
|
|
|
|
| 163 |
|
| 164 |
# Format email content
|
| 165 |
subject = f"Coordination Report - {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
|
@@ -178,6 +265,8 @@ def send_email_report(conversation_data):
|
|
| 178 |
.ai {{ color: #059669; }}
|
| 179 |
.timestamp {{ color: #6b7280; font-size: 0.8em; font-style: italic; }}
|
| 180 |
.department-badge {{ background-color: #3b82f6; color: white; padding: 2px 8px; border-radius: 12px; font-size: 0.8em; }}
|
|
|
|
|
|
|
| 181 |
</style>
|
| 182 |
</head>
|
| 183 |
<body>
|
|
@@ -197,7 +286,7 @@ def send_email_report(conversation_data):
|
|
| 197 |
<p style="margin: 5px 0;">Active Departments</p>
|
| 198 |
</div>
|
| 199 |
<div class="stat-card">
|
| 200 |
-
<h3 style="margin: 0; color: #f59e0b;">{sum(1 for entry in
|
| 201 |
<p style="margin: 5px 0;">Emails Sent</p>
|
| 202 |
</div>
|
| 203 |
</div>
|
|
@@ -208,19 +297,31 @@ def send_email_report(conversation_data):
|
|
| 208 |
for dept, count in dept_activity.items():
|
| 209 |
html_content += f"<p><strong>{dept.title()}:</strong> {count} interactions</p>"
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
# Add recent exchanges
|
| 212 |
html_content += "<h3>π Recent Coordination Activities</h3>"
|
| 213 |
-
for i, entry in enumerate(conversation_data[-10:], 1):
|
| 214 |
dept_badge = f"<span class='department-badge'>{entry.get('department', 'General').title()}</span>" if entry.get('department') else ""
|
|
|
|
|
|
|
| 215 |
email_indicator = " π§" if entry.get('email_sent') else ""
|
| 216 |
|
| 217 |
html_content += f"""
|
| 218 |
<div class="exchange">
|
| 219 |
-
<div style="display: flex; justify-content: between; align-items: center;">
|
| 220 |
<h4 style="margin: 0; color: #374151;">Activity #{i}</h4>
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
</div>
|
| 223 |
-
<p class="user"><strong>
|
| 224 |
<p class="ai"><strong>π€ AI Coordination:</strong> {entry['ai_response']}</p>
|
| 225 |
<p class="timestamp">{entry['timestamp']}</p>
|
| 226 |
</div>
|
|
@@ -248,14 +349,16 @@ def send_email_report(conversation_data):
|
|
| 248 |
except Exception as e:
|
| 249 |
return f"Failed to send report: {str(e)}"
|
| 250 |
|
| 251 |
-
def add_to_conversation_history(user_input, department, ai_response, email_sent=False):
|
| 252 |
"""Add current exchange to conversation history"""
|
| 253 |
entry = {
|
| 254 |
'timestamp': datetime.datetime.now().isoformat(),
|
| 255 |
'user_input': user_input,
|
| 256 |
'department': department,
|
|
|
|
| 257 |
'ai_response': ai_response,
|
| 258 |
-
'email_sent': email_sent
|
|
|
|
| 259 |
}
|
| 260 |
conversation_history.append(entry)
|
| 261 |
|
|
@@ -263,32 +366,46 @@ def add_to_conversation_history(user_input, department, ai_response, email_sent=
|
|
| 263 |
if len(conversation_history) > 50:
|
| 264 |
conversation_history.pop(0)
|
| 265 |
|
| 266 |
-
def process_coordination_request(
|
| 267 |
-
"""Process coordination requests between departments"""
|
| 268 |
if not message.strip():
|
| 269 |
return "Please enter a coordination message."
|
| 270 |
|
| 271 |
if not department:
|
| 272 |
return "Please select your department."
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
# Prepare response
|
| 278 |
response_parts = []
|
| 279 |
-
response_parts.append(f"**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
# Send email if requested
|
| 282 |
email_status = ""
|
| 283 |
if send_email and recipient_dept:
|
| 284 |
-
subject = f"Coordination Request from {departments[department]['name']}"
|
| 285 |
-
email_message = f"""{
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
---
|
| 288 |
AI Coordination Suggestions:
|
| 289 |
{ai_analysis}
|
| 290 |
"""
|
| 291 |
-
email_status = send_department_email(department, recipient_dept, subject, email_message, urgency)
|
| 292 |
response_parts.append(f"\n**π§ Email Status:** {email_status}")
|
| 293 |
|
| 294 |
full_response = "\n".join(response_parts)
|
|
@@ -297,8 +414,10 @@ AI Coordination Suggestions:
|
|
| 297 |
add_to_conversation_history(
|
| 298 |
user_input=message,
|
| 299 |
department=department,
|
|
|
|
| 300 |
ai_response=full_response,
|
| 301 |
-
email_sent=bool(send_email and recipient_dept)
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
return full_response
|
|
@@ -310,15 +429,23 @@ def get_conversation_stats():
|
|
| 310 |
|
| 311 |
total_exchanges = len(conversation_history)
|
| 312 |
dept_activity = {}
|
|
|
|
| 313 |
emails_sent = sum(1 for entry in conversation_history if entry.get('email_sent'))
|
|
|
|
| 314 |
|
| 315 |
for entry in conversation_history:
|
| 316 |
dept = entry.get('department', 'Unknown')
|
|
|
|
| 317 |
dept_activity[dept] = dept_activity.get(dept, 0) + 1
|
|
|
|
|
|
|
| 318 |
|
| 319 |
-
stats_text = f"Total Activities: {total_exchanges} | Emails Sent: {emails_sent}\n"
|
| 320 |
stats_text += "Department Activity: " + ", ".join([f"{k}: {v}" for k, v in dept_activity.items()])
|
| 321 |
|
|
|
|
|
|
|
|
|
|
| 322 |
return stats_text
|
| 323 |
|
| 324 |
def clear_conversation():
|
|
@@ -331,6 +458,10 @@ def get_current_email():
|
|
| 331 |
"""Get the current report email address"""
|
| 332 |
return current_report_email if current_report_email else "Not set"
|
| 333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
# Gradio Interface
|
| 335 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
| 336 |
gr.Markdown("""
|
|
@@ -356,46 +487,67 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
|
| 356 |
update_email_btn = gr.Button("πΎ Update Email", variant="primary")
|
| 357 |
|
| 358 |
gr.Markdown("---")
|
| 359 |
-
gr.Markdown("###
|
| 360 |
-
|
| 361 |
-
department_dropdown = gr.Dropdown(
|
| 362 |
-
choices=["administration", "production", "magazine"],
|
| 363 |
-
label="Your Department",
|
| 364 |
-
info="Select which department you're representing"
|
| 365 |
-
)
|
| 366 |
-
|
| 367 |
-
recipient_dropdown = gr.Dropdown(
|
| 368 |
-
choices=["administration", "production", "magazine"],
|
| 369 |
-
label="Recipient Department (for email)",
|
| 370 |
-
info="Select which department to email"
|
| 371 |
-
)
|
| 372 |
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
with gr.Row():
|
| 394 |
-
submit_btn = gr.Button("π Submit Coordination", variant="primary", size="lg")
|
| 395 |
-
|
| 396 |
|
| 397 |
with gr.Row():
|
| 398 |
-
|
|
|
|
| 399 |
stats_btn = gr.Button("π Get Stats")
|
| 400 |
|
| 401 |
status_display = gr.Textbox(
|
|
@@ -407,22 +559,25 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
|
| 407 |
stats_display = gr.Textbox(
|
| 408 |
label="Coordination Statistics",
|
| 409 |
interactive=False,
|
| 410 |
-
max_lines=
|
| 411 |
)
|
| 412 |
|
| 413 |
with gr.Column(scale=2):
|
| 414 |
gr.Markdown("### π€ AI Coordination Response")
|
| 415 |
output = gr.Textbox(
|
| 416 |
label="Coordination Analysis & Actions",
|
| 417 |
-
lines=
|
| 418 |
-
show_copy_button=True
|
|
|
|
| 419 |
)
|
| 420 |
|
| 421 |
-
with gr.Accordion("π’ Department Information", open=
|
| 422 |
gr.Markdown("""
|
| 423 |
**Administration**: Overall management, decision making, and coordination
|
| 424 |
**Production**: Manufacturing, quality control, and production planning
|
| 425 |
**Magazine Management**: Content creation, publishing, and distribution
|
|
|
|
|
|
|
| 426 |
""")
|
| 427 |
|
| 428 |
with gr.Accordion("π Recent Coordination Activities", open=False):
|
|
@@ -447,19 +602,19 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
|
| 447 |
|
| 448 |
submit_btn.click(
|
| 449 |
fn=process_coordination_request,
|
| 450 |
-
inputs=[
|
| 451 |
outputs=output
|
| 452 |
)
|
| 453 |
|
| 454 |
report_btn.click(
|
| 455 |
-
fn=lambda
|
| 456 |
-
inputs=[
|
| 457 |
outputs=output
|
| 458 |
)
|
| 459 |
|
| 460 |
-
|
| 461 |
fn=process_coordination_request,
|
| 462 |
-
inputs=[
|
| 463 |
outputs=output
|
| 464 |
)
|
| 465 |
|
|
@@ -469,6 +624,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
|
| 469 |
outputs=status_display
|
| 470 |
)
|
| 471 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
stats_btn.click(
|
| 473 |
fn=get_conversation_stats,
|
| 474 |
inputs=[],
|
|
|
|
| 47 |
else:
|
| 48 |
return "β Please enter a valid email address"
|
| 49 |
|
| 50 |
+
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
| 51 |
+
"""Use LLM to analyze coordination needs and suggest actions - with image support"""
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
if image:
|
| 54 |
+
# Convert image to base64 for vision model
|
| 55 |
+
buffered = io.BytesIO()
|
| 56 |
+
image.save(buffered, format="PNG")
|
| 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(
|
| 85 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 86 |
+
headers={
|
| 87 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 88 |
+
"Content-Type": "application/json",
|
| 89 |
+
},
|
| 90 |
+
json={
|
| 91 |
+
"model": "google/gemini-flash-1.5:free", # Vision model
|
| 92 |
+
"messages": [
|
| 93 |
+
{
|
| 94 |
+
"role": "user",
|
| 95 |
+
"content": [
|
| 96 |
+
{
|
| 97 |
+
"type": "text",
|
| 98 |
+
"text": prompt
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"type": "image_url",
|
| 102 |
+
"image_url": {
|
| 103 |
+
"url": f"data:image/png;base64,{img_base64}"
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
]
|
| 107 |
+
}
|
| 108 |
+
],
|
| 109 |
+
"temperature": 0.3
|
| 110 |
+
},
|
| 111 |
+
timeout=60
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
if response.status_code == 200:
|
| 115 |
+
data = response.json()
|
| 116 |
+
return data["choices"][0]["message"]["content"]
|
| 117 |
+
else:
|
| 118 |
+
return "AI analysis with image temporarily unavailable."
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
return f"Image analysis error: {str(e)}"
|
| 122 |
|
| 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(
|
| 148 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 149 |
+
headers={
|
| 150 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 151 |
+
"Content-Type": "application/json",
|
| 152 |
+
},
|
| 153 |
+
json={
|
| 154 |
+
"model": "google/gemma-3-27b-it:free",
|
| 155 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 156 |
+
"temperature": 0.3
|
| 157 |
+
},
|
| 158 |
+
timeout=30
|
| 159 |
+
)
|
| 160 |
|
| 161 |
+
if response.status_code == 200:
|
| 162 |
+
data = response.json()
|
| 163 |
+
return data["choices"][0]["message"]["content"]
|
| 164 |
+
else:
|
| 165 |
+
return "AI analysis temporarily unavailable."
|
| 166 |
+
|
| 167 |
+
except Exception as e:
|
| 168 |
+
return f"Analysis error: {str(e)}"
|
| 169 |
|
| 170 |
+
def send_department_email(sender_dept, recipient_dept, subject, message, user_name, urgency="Normal", image=None):
|
| 171 |
+
"""Send email between departments with optional image attachment"""
|
| 172 |
try:
|
| 173 |
# Determine recipient email
|
| 174 |
recipient_email = departments[recipient_dept]["email"]
|
|
|
|
| 186 |
.urgency-medium {{ border-left: 5px solid #d97706; background-color: #fffbeb; }}
|
| 187 |
.urgency-normal {{ border-left: 5px solid #2563eb; background-color: #eff6ff; }}
|
| 188 |
.message-content {{ padding: 20px; background-color: white; border-radius: 8px; margin: 15px 0; }}
|
| 189 |
+
.user-info {{ background-color: #f8fafc; padding: 10px 15px; border-radius: 6px; margin: 10px 0; }}
|
| 190 |
</style>
|
| 191 |
</head>
|
| 192 |
<body>
|
| 193 |
<div class="header">
|
| 194 |
<h1 style="margin: 0; color: #1f2937;">π’ Inter-Department Communication</h1>
|
| 195 |
<p style="margin: 5px 0;"><strong>From:</strong> {departments[sender_dept]['name']}</p>
|
| 196 |
+
<p style="margin: 5px 0;"><strong>Requested by:</strong> {user_name}</p>
|
| 197 |
<p style="margin: 5px 0;"><strong>To:</strong> {departments[recipient_dept]['name']}</p>
|
| 198 |
<p style="margin: 5px 0;"><strong>Date:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 199 |
<p style="margin: 5px 0;"><strong>Urgency:</strong> {urgency}</p>
|
| 200 |
+
{"<p style='margin: 5px 0;'><strong>π Attachment:</strong> Image included with request</p>" if image else ""}
|
| 201 |
+
</div>
|
| 202 |
+
|
| 203 |
+
<div class="user-info">
|
| 204 |
+
<strong>π€ Requestor Details:</strong><br>
|
| 205 |
+
Name: {user_name}<br>
|
| 206 |
+
Department: {departments[sender_dept]['name']}
|
| 207 |
</div>
|
| 208 |
|
| 209 |
<div class="message-content {f'urgency-{urgency.lower()}' if urgency.lower() in ['high', 'medium'] else 'urgency-normal'}">
|
|
|
|
| 213 |
|
| 214 |
<div style="color: #6b7280; font-size: 0.9em; margin-top: 20px;">
|
| 215 |
<p>This is an automated message from the AI Coordination System.</p>
|
| 216 |
+
{"<p><em>Note: An image was included with this request. Please check the original system for image details.</em></p>" if image else ""}
|
| 217 |
</div>
|
| 218 |
</body>
|
| 219 |
</html>
|
|
|
|
| 241 |
try:
|
| 242 |
# Calculate department activity
|
| 243 |
dept_activity = {}
|
| 244 |
+
user_activity = {}
|
| 245 |
for entry in conversation_data:
|
| 246 |
dept = entry.get('department', 'Unknown')
|
| 247 |
+
user = entry.get('user_name', 'Unknown')
|
| 248 |
dept_activity[dept] = dept_activity.get(dept, 0) + 1
|
| 249 |
+
user_activity[user] = user_activity.get(user, 0) + 1
|
| 250 |
|
| 251 |
# Format email content
|
| 252 |
subject = f"Coordination Report - {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
|
|
|
| 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>
|
|
|
|
| 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>
|
|
|
|
| 297 |
for dept, count in dept_activity.items():
|
| 298 |
html_content += f"<p><strong>{dept.title()}:</strong> {count} interactions</p>"
|
| 299 |
|
| 300 |
+
# Add user activity
|
| 301 |
+
html_content += "<h3>π₯ User Activity</h3>"
|
| 302 |
+
for user, count in user_activity.items():
|
| 303 |
+
if user != 'Unknown':
|
| 304 |
+
html_content += f"<p><strong>{user}:</strong> {count} requests</p>"
|
| 305 |
+
|
| 306 |
# Add recent exchanges
|
| 307 |
html_content += "<h3>π Recent Coordination Activities</h3>"
|
| 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 |
html_content += f"""
|
| 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>
|
| 318 |
+
<div>
|
| 319 |
+
{dept_badge}
|
| 320 |
+
{user_badge}
|
| 321 |
+
{image_indicator}
|
| 322 |
+
</div>
|
| 323 |
</div>
|
| 324 |
+
<p class="user"><strong>π¬ Message:</strong> {entry['user_input']}{email_indicator}</p>
|
| 325 |
<p class="ai"><strong>π€ AI Coordination:</strong> {entry['ai_response']}</p>
|
| 326 |
<p class="timestamp">{entry['timestamp']}</p>
|
| 327 |
</div>
|
|
|
|
| 349 |
except Exception as e:
|
| 350 |
return f"Failed to send report: {str(e)}"
|
| 351 |
|
| 352 |
+
def add_to_conversation_history(user_input, department, user_name, ai_response, email_sent=False, image_uploaded=False):
|
| 353 |
"""Add current exchange to conversation history"""
|
| 354 |
entry = {
|
| 355 |
'timestamp': datetime.datetime.now().isoformat(),
|
| 356 |
'user_input': user_input,
|
| 357 |
'department': department,
|
| 358 |
+
'user_name': user_name,
|
| 359 |
'ai_response': ai_response,
|
| 360 |
+
'email_sent': email_sent,
|
| 361 |
+
'image_uploaded': image_uploaded
|
| 362 |
}
|
| 363 |
conversation_history.append(entry)
|
| 364 |
|
|
|
|
| 366 |
if len(conversation_history) > 50:
|
| 367 |
conversation_history.pop(0)
|
| 368 |
|
| 369 |
+
def process_coordination_request(name, department, message, image, recipient_dept, send_email, urgency):
|
| 370 |
+
"""Process coordination requests between departments with image support"""
|
| 371 |
if not message.strip():
|
| 372 |
return "Please enter a coordination message."
|
| 373 |
|
| 374 |
if not department:
|
| 375 |
return "Please select your department."
|
| 376 |
|
| 377 |
+
if not name.strip():
|
| 378 |
+
return "Please enter your name."
|
| 379 |
+
|
| 380 |
+
# Get AI analysis and suggestions (with image if provided)
|
| 381 |
+
ai_analysis = analyze_coordination_needs(message, department, name, image)
|
| 382 |
|
| 383 |
# Prepare response
|
| 384 |
response_parts = []
|
| 385 |
+
response_parts.append(f"**π€ Request from:** {name} | **π’ Department:** {departments[department]['name']}")
|
| 386 |
+
|
| 387 |
+
if image:
|
| 388 |
+
response_parts.append("**π· Image attached and analyzed**")
|
| 389 |
+
|
| 390 |
+
response_parts.append(f"\n**π€ AI Coordination Analysis:**\n{ai_analysis}")
|
| 391 |
|
| 392 |
# Send email if requested
|
| 393 |
email_status = ""
|
| 394 |
if send_email and recipient_dept:
|
| 395 |
+
subject = f"Coordination Request from {departments[department]['name']} - {name}"
|
| 396 |
+
email_message = f"""Requestor: {name}
|
| 397 |
+
Department: {departments[department]['name']}
|
| 398 |
+
|
| 399 |
+
Message:
|
| 400 |
+
{message}
|
| 401 |
+
|
| 402 |
+
{"\nNote: An image was included with this request. Please check the system for visual details." if image else ""}
|
| 403 |
+
|
| 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 |
|
| 411 |
full_response = "\n".join(response_parts)
|
|
|
|
| 414 |
add_to_conversation_history(
|
| 415 |
user_input=message,
|
| 416 |
department=department,
|
| 417 |
+
user_name=name,
|
| 418 |
ai_response=full_response,
|
| 419 |
+
email_sent=bool(send_email and recipient_dept),
|
| 420 |
+
image_uploaded=image is not None
|
| 421 |
)
|
| 422 |
|
| 423 |
return full_response
|
|
|
|
| 429 |
|
| 430 |
total_exchanges = len(conversation_history)
|
| 431 |
dept_activity = {}
|
| 432 |
+
user_activity = {}
|
| 433 |
emails_sent = sum(1 for entry in conversation_history if entry.get('email_sent'))
|
| 434 |
+
images_uploaded = sum(1 for entry in conversation_history if entry.get('image_uploaded'))
|
| 435 |
|
| 436 |
for entry in conversation_history:
|
| 437 |
dept = entry.get('department', 'Unknown')
|
| 438 |
+
user = entry.get('user_name', 'Unknown')
|
| 439 |
dept_activity[dept] = dept_activity.get(dept, 0) + 1
|
| 440 |
+
if user != 'Unknown':
|
| 441 |
+
user_activity[user] = user_activity.get(user, 0) + 1
|
| 442 |
|
| 443 |
+
stats_text = f"Total Activities: {total_exchanges} | Emails Sent: {emails_sent} | Images: {images_uploaded}\n"
|
| 444 |
stats_text += "Department Activity: " + ", ".join([f"{k}: {v}" for k, v in dept_activity.items()])
|
| 445 |
|
| 446 |
+
if user_activity:
|
| 447 |
+
stats_text += f"\nActive Users: {', '.join(user_activity.keys())}"
|
| 448 |
+
|
| 449 |
return stats_text
|
| 450 |
|
| 451 |
def clear_conversation():
|
|
|
|
| 458 |
"""Get the current report email address"""
|
| 459 |
return current_report_email if current_report_email else "Not set"
|
| 460 |
|
| 461 |
+
def clear_form():
|
| 462 |
+
"""Clear the form fields"""
|
| 463 |
+
return ["", None, "", None, "administration", "Normal", False]
|
| 464 |
+
|
| 465 |
# Gradio Interface
|
| 466 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
| 467 |
gr.Markdown("""
|
|
|
|
| 487 |
update_email_btn = gr.Button("πΎ Update Email", variant="primary")
|
| 488 |
|
| 489 |
gr.Markdown("---")
|
| 490 |
+
gr.Markdown("### π€ Coordination Request Form")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
+
with gr.Group():
|
| 493 |
+
gr.Markdown("#### Personal Information")
|
| 494 |
+
name_input = gr.Textbox(
|
| 495 |
+
label="Your Full Name",
|
| 496 |
+
placeholder="Enter your full name",
|
| 497 |
+
info="This will be included in coordination requests"
|
| 498 |
+
)
|
| 499 |
+
|
| 500 |
+
department_dropdown = gr.Dropdown(
|
| 501 |
+
choices=["administration", "production", "magazine"],
|
| 502 |
+
label="Your Department",
|
| 503 |
+
info="Select which department you're representing"
|
| 504 |
+
)
|
| 505 |
|
| 506 |
+
with gr.Group():
|
| 507 |
+
gr.Markdown("#### Request Details")
|
| 508 |
+
message_input = gr.Textbox(
|
| 509 |
+
label="Coordination Message",
|
| 510 |
+
lines=4,
|
| 511 |
+
placeholder="Describe what you need from other departments...\nExample: 'We need production schedules for the next magazine issue by Friday.'",
|
| 512 |
+
max_lines=6,
|
| 513 |
+
info="Be specific about what coordination you need"
|
| 514 |
+
)
|
| 515 |
+
|
| 516 |
+
image_input = gr.Image(
|
| 517 |
+
label="π· Attach Image (Optional)",
|
| 518 |
+
type="pil",
|
| 519 |
+
height=200,
|
| 520 |
+
info="Upload diagrams, photos, or documents as images for the AI to analyze"
|
| 521 |
+
)
|
| 522 |
|
| 523 |
+
with gr.Group():
|
| 524 |
+
gr.Markdown("#### Email Options")
|
| 525 |
+
recipient_dropdown = gr.Dropdown(
|
| 526 |
+
choices=["administration", "production", "magazine"],
|
| 527 |
+
label="Recipient Department",
|
| 528 |
+
info="Select which department to email"
|
| 529 |
+
)
|
| 530 |
+
|
| 531 |
+
urgency_dropdown = gr.Dropdown(
|
| 532 |
+
choices=["Normal", "Medium", "High"],
|
| 533 |
+
value="Normal",
|
| 534 |
+
label="Urgency Level",
|
| 535 |
+
info="Set email priority"
|
| 536 |
+
)
|
| 537 |
+
|
| 538 |
+
send_email_checkbox = gr.Checkbox(
|
| 539 |
+
label="π§ Send email to recipient department",
|
| 540 |
+
value=True,
|
| 541 |
+
info="Automatically send coordination email"
|
| 542 |
+
)
|
| 543 |
|
| 544 |
with gr.Row():
|
| 545 |
+
submit_btn = gr.Button("π Submit Coordination Request", variant="primary", size="lg")
|
| 546 |
+
clear_form_btn = gr.Button("ποΈ Clear Form", variant="secondary")
|
| 547 |
|
| 548 |
with gr.Row():
|
| 549 |
+
report_btn = gr.Button("π Submit & Send Report", variant="secondary")
|
| 550 |
+
clear_btn = gr.Button("ποΈ Clear History", variant="stop")
|
| 551 |
stats_btn = gr.Button("π Get Stats")
|
| 552 |
|
| 553 |
status_display = gr.Textbox(
|
|
|
|
| 559 |
stats_display = gr.Textbox(
|
| 560 |
label="Coordination Statistics",
|
| 561 |
interactive=False,
|
| 562 |
+
max_lines=4
|
| 563 |
)
|
| 564 |
|
| 565 |
with gr.Column(scale=2):
|
| 566 |
gr.Markdown("### π€ AI Coordination Response")
|
| 567 |
output = gr.Textbox(
|
| 568 |
label="Coordination Analysis & Actions",
|
| 569 |
+
lines=18,
|
| 570 |
+
show_copy_button=True,
|
| 571 |
+
max_lines=20
|
| 572 |
)
|
| 573 |
|
| 574 |
+
with gr.Accordion("π’ Department Information", open=True):
|
| 575 |
gr.Markdown("""
|
| 576 |
**Administration**: Overall management, decision making, and coordination
|
| 577 |
**Production**: Manufacturing, quality control, and production planning
|
| 578 |
**Magazine Management**: Content creation, publishing, and distribution
|
| 579 |
+
|
| 580 |
+
*The AI will analyze your message and any attached images to provide specific coordination recommendations.*
|
| 581 |
""")
|
| 582 |
|
| 583 |
with gr.Accordion("π Recent Coordination Activities", open=False):
|
|
|
|
| 602 |
|
| 603 |
submit_btn.click(
|
| 604 |
fn=process_coordination_request,
|
| 605 |
+
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 606 |
outputs=output
|
| 607 |
)
|
| 608 |
|
| 609 |
report_btn.click(
|
| 610 |
+
fn=lambda name, dept, msg, img, rec, send, urg: process_coordination_request(name, dept, msg, img, rec, send, urg) + "\n\n---\n" + send_email_report(conversation_history),
|
| 611 |
+
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 612 |
outputs=output
|
| 613 |
)
|
| 614 |
|
| 615 |
+
message_input.submit(
|
| 616 |
fn=process_coordination_request,
|
| 617 |
+
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 618 |
outputs=output
|
| 619 |
)
|
| 620 |
|
|
|
|
| 624 |
outputs=status_display
|
| 625 |
)
|
| 626 |
|
| 627 |
+
clear_form_btn.click(
|
| 628 |
+
fn=clear_form,
|
| 629 |
+
inputs=[],
|
| 630 |
+
outputs=[name_input, image_input, message_input, recipient_dropdown, department_dropdown, urgency_dropdown, send_email_checkbox]
|
| 631 |
+
)
|
| 632 |
+
|
| 633 |
stats_btn.click(
|
| 634 |
fn=get_conversation_stats,
|
| 635 |
inputs=[],
|