Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,9 @@ DEFAULT_REPORT_EMAIL = os.getenv("REPORT_EMAIL")
|
|
| 12 |
BREVO_API_KEY = os.getenv("BREVO_API_KEY")
|
| 13 |
BREVO_FROM_EMAIL = os.getenv("BREVO_FROM_EMAIL", "noreply@yourdomain.com")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Store conversation history and settings
|
| 16 |
conversation_history = []
|
| 17 |
current_report_email = DEFAULT_REPORT_EMAIL
|
|
@@ -20,17 +23,17 @@ current_report_email = DEFAULT_REPORT_EMAIL
|
|
| 20 |
departments = {
|
| 21 |
"administration": {
|
| 22 |
"name": "Administration",
|
| 23 |
-
"email": os.getenv("ADMIN_EMAIL", ""),
|
| 24 |
"responsibilities": "Overall management, decision making, and coordination"
|
| 25 |
},
|
| 26 |
"production": {
|
| 27 |
"name": "Production",
|
| 28 |
-
"email": os.getenv("PRODUCTION_EMAIL", ""),
|
| 29 |
"responsibilities": "Manufacturing, quality control, and production planning"
|
| 30 |
},
|
| 31 |
"magazine": {
|
| 32 |
"name": "Magazine Management",
|
| 33 |
-
"email": os.getenv("MAGAZINE_EMAIL", ""),
|
| 34 |
"responsibilities": "Content creation, publishing, and distribution"
|
| 35 |
}
|
| 36 |
}
|
|
@@ -47,8 +50,9 @@ def update_report_email(new_email):
|
|
| 47 |
def send_email_brevo_api(to_email, subject, html_content, from_name="AI Coordination System"):
|
| 48 |
"""Send email using Brevo API"""
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
response = requests.post(
|
| 54 |
"https://api.brevo.com/v3/smtp/email",
|
|
@@ -78,9 +82,184 @@ def send_email_brevo_api(to_email, subject, html_content, from_name="AI Coordina
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"β Email error: {str(e)}"
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
| 82 |
"""Use LLM to analyze coordination needs and suggest actions - with image support"""
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
if image:
|
| 85 |
# Convert image to base64 for vision model
|
| 86 |
buffered = io.BytesIO()
|
|
@@ -168,7 +347,7 @@ Monitoring or escalation steps
|
|
| 168 |
|
| 169 |
else:
|
| 170 |
# Text-only analysis
|
| 171 |
-
prompt =
|
| 172 |
You are an Enterprise Operations Coordination AI used inside a professional organization.
|
| 173 |
|
| 174 |
Your responsibility is to:
|
|
@@ -304,8 +483,12 @@ def send_department_email(sender_dept, recipient_dept, subject, message, user_na
|
|
| 304 |
|
| 305 |
def send_email_report(conversation_data):
|
| 306 |
"""Send comprehensive coordination report to administration"""
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
try:
|
| 311 |
# Calculate department activity
|
|
@@ -447,6 +630,11 @@ def process_coordination_request(name, department, message, image, recipient_dep
|
|
| 447 |
|
| 448 |
# Prepare response
|
| 449 |
response_parts = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
response_parts.append(f"**π€ Request from:** {name} | **π’ Department:** {departments[department]['name']}")
|
| 451 |
|
| 452 |
if image:
|
|
@@ -537,14 +725,54 @@ def clear_conversation():
|
|
| 537 |
|
| 538 |
def get_current_email():
|
| 539 |
"""Get the current report email address"""
|
| 540 |
-
return current_report_email if current_report_email else "
|
| 541 |
|
| 542 |
def clear_form():
|
| 543 |
"""Clear the form fields"""
|
| 544 |
return ["", None, "", None, "administration", "Normal", False]
|
| 545 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
# Gradio Interface
|
| 547 |
-
# Add this at the top of your Gradio Blocks
|
| 548 |
with gr.Blocks(
|
| 549 |
theme=gr.themes.Soft(),
|
| 550 |
title="AI Coordination System"
|
|
@@ -552,21 +780,48 @@ with gr.Blocks(
|
|
| 552 |
gr.Markdown("# π’ AI Department Coordination System")
|
| 553 |
gr.Markdown("*Streamline communication between Administration, Production, and Magazine Management*")
|
| 554 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
with gr.Row():
|
| 556 |
with gr.Column(scale=1):
|
| 557 |
-
gr.
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
|
| 571 |
gr.Markdown("---")
|
| 572 |
gr.Markdown("### π New Coordination Request")
|
|
@@ -580,7 +835,8 @@ with gr.Blocks(
|
|
| 580 |
|
| 581 |
department_dropdown = gr.Dropdown(
|
| 582 |
choices=["administration", "production", "magazine"],
|
| 583 |
-
label="Your Department"
|
|
|
|
| 584 |
)
|
| 585 |
|
| 586 |
with gr.Group():
|
|
@@ -632,24 +888,38 @@ with gr.Blocks(
|
|
| 632 |
stats_display = gr.Textbox(
|
| 633 |
label="Activity Summary",
|
| 634 |
interactive=False,
|
| 635 |
-
max_lines=
|
| 636 |
)
|
| 637 |
|
| 638 |
with gr.Column(scale=2):
|
| 639 |
gr.Markdown("### π‘ Coordination Guidance")
|
| 640 |
output = gr.Textbox(
|
| 641 |
label="Recommended Actions",
|
| 642 |
-
lines=
|
| 643 |
show_copy_button=True
|
| 644 |
)
|
| 645 |
|
| 646 |
with gr.Accordion("π’ Department Overview", open=True):
|
| 647 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
|
| 649 |
with gr.Accordion("π Recent Activity", open=False):
|
| 650 |
history_display = gr.JSON(
|
| 651 |
label="Activity Log",
|
| 652 |
-
value=conversation_history[-10:] if conversation_history else []
|
| 653 |
)
|
| 654 |
|
| 655 |
# Event handlers
|
|
@@ -665,28 +935,50 @@ with gr.Blocks(
|
|
| 665 |
outputs=email_status
|
| 666 |
)
|
| 667 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
submit_btn.click(
|
| 669 |
fn=process_coordination_request,
|
| 670 |
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 671 |
outputs=output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
)
|
| 673 |
|
| 674 |
report_btn.click(
|
| 675 |
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),
|
| 676 |
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 677 |
outputs=output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
)
|
| 679 |
|
| 680 |
message_input.submit(
|
| 681 |
fn=process_coordination_request,
|
| 682 |
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 683 |
outputs=output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
)
|
| 685 |
|
| 686 |
clear_btn.click(
|
| 687 |
fn=clear_conversation,
|
| 688 |
inputs=[],
|
| 689 |
outputs=status_display
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
)
|
| 691 |
|
| 692 |
clear_form_btn.click(
|
|
@@ -702,8 +994,4 @@ with gr.Blocks(
|
|
| 702 |
)
|
| 703 |
|
| 704 |
if __name__ == "__main__":
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
|
|
|
| 12 |
BREVO_API_KEY = os.getenv("BREVO_API_KEY")
|
| 13 |
BREVO_FROM_EMAIL = os.getenv("BREVO_FROM_EMAIL", "noreply@yourdomain.com")
|
| 14 |
|
| 15 |
+
# Testing/Demo Mode Configuration
|
| 16 |
+
DEMO_MODE = os.getenv("DEMO_MODE", "true").lower() == "true" # Enable demo mode by default
|
| 17 |
+
|
| 18 |
# Store conversation history and settings
|
| 19 |
conversation_history = []
|
| 20 |
current_report_email = DEFAULT_REPORT_EMAIL
|
|
|
|
| 23 |
departments = {
|
| 24 |
"administration": {
|
| 25 |
"name": "Administration",
|
| 26 |
+
"email": os.getenv("ADMIN_EMAIL", "admin@demo.com"),
|
| 27 |
"responsibilities": "Overall management, decision making, and coordination"
|
| 28 |
},
|
| 29 |
"production": {
|
| 30 |
"name": "Production",
|
| 31 |
+
"email": os.getenv("PRODUCTION_EMAIL", "production@demo.com"),
|
| 32 |
"responsibilities": "Manufacturing, quality control, and production planning"
|
| 33 |
},
|
| 34 |
"magazine": {
|
| 35 |
"name": "Magazine Management",
|
| 36 |
+
"email": os.getenv("MAGAZINE_EMAIL", "magazine@demo.com"),
|
| 37 |
"responsibilities": "Content creation, publishing, and distribution"
|
| 38 |
}
|
| 39 |
}
|
|
|
|
| 50 |
def send_email_brevo_api(to_email, subject, html_content, from_name="AI Coordination System"):
|
| 51 |
"""Send email using Brevo API"""
|
| 52 |
try:
|
| 53 |
+
# Demo mode: simulate email sending
|
| 54 |
+
if DEMO_MODE or not BREVO_API_KEY:
|
| 55 |
+
return f"π§ͺ [DEMO MODE] Email simulated to {to_email}"
|
| 56 |
|
| 57 |
response = requests.post(
|
| 58 |
"https://api.brevo.com/v3/smtp/email",
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
return f"β Email error: {str(e)}"
|
| 84 |
|
| 85 |
+
def get_mock_ai_response(user_message, department, user_name, has_image=False):
|
| 86 |
+
"""Generate a mock AI response for demo/testing mode"""
|
| 87 |
+
|
| 88 |
+
image_section = """
|
| 89 |
+
=== IMAGE OBSERVATIONS ===
|
| 90 |
+
β’ Mock analysis: Image appears to show office equipment
|
| 91 |
+
β’ Suggested action: Forward to relevant department for review
|
| 92 |
+
""" if has_image else ""
|
| 93 |
+
|
| 94 |
+
responses = {
|
| 95 |
+
"administration": {
|
| 96 |
+
"equipment": f"""
|
| 97 |
+
=== EXECUTIVE SUMMARY ===
|
| 98 |
+
Request from {user_name} regarding equipment needs requires coordination between Administration and Production departments for budget approval and implementation.
|
| 99 |
+
|
| 100 |
+
{image_section}
|
| 101 |
+
=== RECOMMENDED TARGET DEPARTMENT ===
|
| 102 |
+
Production
|
| 103 |
+
This is primarily a production-related request that requires equipment or facility resources.
|
| 104 |
+
|
| 105 |
+
=== DEPARTMENTS INVOLVED ===
|
| 106 |
+
β’ Production β Equipment assessment and implementation
|
| 107 |
+
β’ Administration β Budget approval and procurement authorization
|
| 108 |
+
|
| 109 |
+
=== ACTION PLAN ===
|
| 110 |
+
1. Production department to assess equipment specifications and requirements
|
| 111 |
+
2. Administration to review budget implications and approve funding
|
| 112 |
+
3. Production to coordinate installation and training
|
| 113 |
+
|
| 114 |
+
=== PRIORITY ===
|
| 115 |
+
Medium β Standard equipment request requiring normal procurement timeline
|
| 116 |
+
|
| 117 |
+
=== FOLLOW-UP ===
|
| 118 |
+
Weekly status updates until equipment is operational
|
| 119 |
+
""",
|
| 120 |
+
"default": f"""
|
| 121 |
+
=== EXECUTIVE SUMMARY ===
|
| 122 |
+
Administrative request from {user_name} requires internal review and possible coordination with other departments.
|
| 123 |
+
|
| 124 |
+
{image_section}
|
| 125 |
+
=== RECOMMENDED TARGET DEPARTMENT ===
|
| 126 |
+
Administration
|
| 127 |
+
This request should be handled within the Administration department.
|
| 128 |
+
|
| 129 |
+
=== DEPARTMENTS INVOLVED ===
|
| 130 |
+
β’ Administration β Primary responsibility for coordination and decision-making
|
| 131 |
+
|
| 132 |
+
=== ACTION PLAN ===
|
| 133 |
+
1. Review request details and requirements
|
| 134 |
+
2. Determine resource allocation and timeline
|
| 135 |
+
3. Coordinate with relevant stakeholders
|
| 136 |
+
|
| 137 |
+
=== PRIORITY ===
|
| 138 |
+
Normal β Standard administrative workflow
|
| 139 |
+
|
| 140 |
+
=== FOLLOW-UP ===
|
| 141 |
+
Monitor progress and provide status updates as needed
|
| 142 |
+
"""
|
| 143 |
+
},
|
| 144 |
+
"production": {
|
| 145 |
+
"content": f"""
|
| 146 |
+
=== EXECUTIVE SUMMARY ===
|
| 147 |
+
Production request from {user_name} involves content creation, requiring Magazine department collaboration.
|
| 148 |
+
|
| 149 |
+
{image_section}
|
| 150 |
+
=== RECOMMENDED TARGET DEPARTMENT ===
|
| 151 |
+
Magazine
|
| 152 |
+
This request involves content creation and publishing activities.
|
| 153 |
+
|
| 154 |
+
=== DEPARTMENTS INVOLVED ===
|
| 155 |
+
β’ Magazine β Content development and design
|
| 156 |
+
β’ Production β Material production and quality control
|
| 157 |
+
|
| 158 |
+
=== ACTION PLAN ===
|
| 159 |
+
1. Magazine to develop content and design specifications
|
| 160 |
+
2. Production to review technical feasibility
|
| 161 |
+
3. Coordinate timeline for material production
|
| 162 |
+
|
| 163 |
+
=== PRIORITY ===
|
| 164 |
+
Medium β Requires cross-departmental coordination
|
| 165 |
+
|
| 166 |
+
=== FOLLOW-UP ===
|
| 167 |
+
Bi-weekly alignment meetings until project completion
|
| 168 |
+
""",
|
| 169 |
+
"default": f"""
|
| 170 |
+
=== EXECUTIVE SUMMARY ===
|
| 171 |
+
Production request from {user_name} requires assessment of manufacturing capabilities and resource allocation.
|
| 172 |
+
|
| 173 |
+
{image_section}
|
| 174 |
+
=== RECOMMENDED TARGET DEPARTMENT ===
|
| 175 |
+
Production
|
| 176 |
+
Internal production department matter requiring operational attention.
|
| 177 |
+
|
| 178 |
+
=== DEPARTMENTS INVOLVED ===
|
| 179 |
+
β’ Production β Manufacturing operations and quality assurance
|
| 180 |
+
β’ Administration β Resource approval if needed
|
| 181 |
+
|
| 182 |
+
=== ACTION PLAN ===
|
| 183 |
+
1. Assess current production capacity and requirements
|
| 184 |
+
2. Identify resource needs and constraints
|
| 185 |
+
3. Develop implementation timeline
|
| 186 |
+
|
| 187 |
+
=== PRIORITY ===
|
| 188 |
+
Normal β Standard production workflow
|
| 189 |
+
|
| 190 |
+
=== FOLLOW-UP ===
|
| 191 |
+
Regular progress monitoring through production meetings
|
| 192 |
+
"""
|
| 193 |
+
},
|
| 194 |
+
"magazine": {
|
| 195 |
+
"printing": f"""
|
| 196 |
+
=== EXECUTIVE SUMMARY ===
|
| 197 |
+
Magazine request from {user_name} regarding printing requires Production department involvement for manufacturing execution.
|
| 198 |
+
|
| 199 |
+
{image_section}
|
| 200 |
+
=== RECOMMENDED TARGET DEPARTMENT ===
|
| 201 |
+
Production
|
| 202 |
+
Printing and manufacturing activities fall under Production responsibility.
|
| 203 |
+
|
| 204 |
+
=== DEPARTMENTS INVOLVED ===
|
| 205 |
+
β’ Magazine β Content finalization and specifications
|
| 206 |
+
β’ Production β Printing operations and quality control
|
| 207 |
+
|
| 208 |
+
=== ACTION PLAN ===
|
| 209 |
+
1. Magazine to finalize all content and design specifications
|
| 210 |
+
2. Production to schedule printing resources
|
| 211 |
+
3. Coordinate delivery timeline and distribution
|
| 212 |
+
|
| 213 |
+
=== PRIORITY ===
|
| 214 |
+
High β Publishing deadline-sensitive activity
|
| 215 |
+
|
| 216 |
+
=== FOLLOW-UP ===
|
| 217 |
+
Daily updates during production phase
|
| 218 |
+
""",
|
| 219 |
+
"default": f"""
|
| 220 |
+
=== EXECUTIVE SUMMARY ===
|
| 221 |
+
Magazine department request from {user_name} involves content development and publishing coordination.
|
| 222 |
+
|
| 223 |
+
{image_section}
|
| 224 |
+
=== RECOMMENDED TARGET DEPARTMENT ===
|
| 225 |
+
Magazine
|
| 226 |
+
Editorial and publishing matter for Magazine department handling.
|
| 227 |
+
|
| 228 |
+
=== DEPARTMENTS INVOLVED ===
|
| 229 |
+
β’ Magazine β Content creation and editorial oversight
|
| 230 |
+
β’ Production β Technical production support if needed
|
| 231 |
+
|
| 232 |
+
=== ACTION PLAN ===
|
| 233 |
+
1. Develop content strategy and editorial plan
|
| 234 |
+
2. Coordinate with relevant stakeholders
|
| 235 |
+
3. Execute publishing timeline
|
| 236 |
+
|
| 237 |
+
=== PRIORITY ===
|
| 238 |
+
Normal β Standard editorial workflow
|
| 239 |
+
|
| 240 |
+
=== FOLLOW-UP ===
|
| 241 |
+
Editorial review meetings and publication tracking
|
| 242 |
+
"""
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
# Determine which mock response to use
|
| 247 |
+
message_lower = user_message.lower()
|
| 248 |
+
dept_responses = responses.get(department, responses["administration"])
|
| 249 |
+
|
| 250 |
+
for keyword, response in dept_responses.items():
|
| 251 |
+
if keyword != "default" and keyword in message_lower:
|
| 252 |
+
return response
|
| 253 |
+
|
| 254 |
+
return dept_responses["default"]
|
| 255 |
+
|
| 256 |
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
| 257 |
"""Use LLM to analyze coordination needs and suggest actions - with image support"""
|
| 258 |
|
| 259 |
+
# Demo/Testing Mode: Use mock responses
|
| 260 |
+
if DEMO_MODE or not OPENROUTER_API_KEY:
|
| 261 |
+
return get_mock_ai_response(user_message, department, user_name, has_image=image is not None)
|
| 262 |
+
|
| 263 |
if image:
|
| 264 |
# Convert image to base64 for vision model
|
| 265 |
buffered = io.BytesIO()
|
|
|
|
| 347 |
|
| 348 |
else:
|
| 349 |
# Text-only analysis
|
| 350 |
+
prompt = f"""
|
| 351 |
You are an Enterprise Operations Coordination AI used inside a professional organization.
|
| 352 |
|
| 353 |
Your responsibility is to:
|
|
|
|
| 483 |
|
| 484 |
def send_email_report(conversation_data):
|
| 485 |
"""Send comprehensive coordination report to administration"""
|
| 486 |
+
# Demo mode check
|
| 487 |
+
if DEMO_MODE or not BREVO_API_KEY or not current_report_email:
|
| 488 |
+
if DEMO_MODE:
|
| 489 |
+
return "π§ͺ [DEMO MODE] Report email simulated successfully"
|
| 490 |
+
else:
|
| 491 |
+
return "Email reporting not configured - missing Brevo API key or report email"
|
| 492 |
|
| 493 |
try:
|
| 494 |
# Calculate department activity
|
|
|
|
| 630 |
|
| 631 |
# Prepare response
|
| 632 |
response_parts = []
|
| 633 |
+
|
| 634 |
+
# Add demo mode indicator if active
|
| 635 |
+
if DEMO_MODE:
|
| 636 |
+
response_parts.append("π§ͺ **DEMO MODE ACTIVE** - Using simulated AI responses")
|
| 637 |
+
|
| 638 |
response_parts.append(f"**π€ Request from:** {name} | **π’ Department:** {departments[department]['name']}")
|
| 639 |
|
| 640 |
if image:
|
|
|
|
| 725 |
|
| 726 |
def get_current_email():
|
| 727 |
"""Get the current report email address"""
|
| 728 |
+
return current_report_email if current_report_email else "demo@example.com"
|
| 729 |
|
| 730 |
def clear_form():
|
| 731 |
"""Clear the form fields"""
|
| 732 |
return ["", None, "", None, "administration", "Normal", False]
|
| 733 |
|
| 734 |
+
def load_sample_request(sample_type):
|
| 735 |
+
"""Load sample requests for testing"""
|
| 736 |
+
samples = {
|
| 737 |
+
"Equipment Request": {
|
| 738 |
+
"name": "John Smith",
|
| 739 |
+
"dept": "production",
|
| 740 |
+
"message": "We need to upgrade our printing equipment. The current machines are outdated and causing quality issues.",
|
| 741 |
+
"recipient": "administration",
|
| 742 |
+
"urgency": "Medium"
|
| 743 |
+
},
|
| 744 |
+
"Content Collaboration": {
|
| 745 |
+
"name": "Sarah Johnson",
|
| 746 |
+
"dept": "magazine",
|
| 747 |
+
"message": "Need production support for our next magazine issue. We have special requirements for paper quality and binding.",
|
| 748 |
+
"recipient": "production",
|
| 749 |
+
"urgency": "High"
|
| 750 |
+
},
|
| 751 |
+
"Budget Approval": {
|
| 752 |
+
"name": "Mike Chen",
|
| 753 |
+
"dept": "production",
|
| 754 |
+
"message": "Requesting budget approval for preventive maintenance on critical machinery.",
|
| 755 |
+
"recipient": "administration",
|
| 756 |
+
"urgency": "Normal"
|
| 757 |
+
}
|
| 758 |
+
}
|
| 759 |
+
|
| 760 |
+
if sample_type in samples:
|
| 761 |
+
s = samples[sample_type]
|
| 762 |
+
return s["name"], s["dept"], s["message"], s["recipient"], s["urgency"]
|
| 763 |
+
return "", "administration", "", None, "Normal"
|
| 764 |
+
|
| 765 |
+
def get_system_status():
|
| 766 |
+
"""Get current system configuration status"""
|
| 767 |
+
status_lines = []
|
| 768 |
+
status_lines.append("**System Configuration:**")
|
| 769 |
+
status_lines.append(f"β’ Demo Mode: {'π§ͺ ENABLED' if DEMO_MODE else 'β
DISABLED'}")
|
| 770 |
+
status_lines.append(f"β’ OpenRouter API: {'β
Configured' if OPENROUTER_API_KEY else 'β Not configured (using demo)'}")
|
| 771 |
+
status_lines.append(f"β’ Brevo Email API: {'β
Configured' if BREVO_API_KEY else 'β Not configured (simulated)'}")
|
| 772 |
+
status_lines.append(f"β’ Report Email: {current_report_email if current_report_email else 'β Not set'}")
|
| 773 |
+
return "\n".join(status_lines)
|
| 774 |
+
|
| 775 |
# Gradio Interface
|
|
|
|
| 776 |
with gr.Blocks(
|
| 777 |
theme=gr.themes.Soft(),
|
| 778 |
title="AI Coordination System"
|
|
|
|
| 780 |
gr.Markdown("# π’ AI Department Coordination System")
|
| 781 |
gr.Markdown("*Streamline communication between Administration, Production, and Magazine Management*")
|
| 782 |
|
| 783 |
+
# Demo mode banner
|
| 784 |
+
if DEMO_MODE:
|
| 785 |
+
gr.Markdown("""
|
| 786 |
+
<div style="background-color: #fef3c7; padding: 15px; border-radius: 8px; border-left: 4px solid #f59e0b; margin-bottom: 20px;">
|
| 787 |
+
<strong>π§ͺ DEMO MODE ACTIVE</strong><br>
|
| 788 |
+
This space is running in demo mode with simulated AI responses.
|
| 789 |
+
To enable full functionality, configure the required API keys in Settings β Repository Secrets.
|
| 790 |
+
</div>
|
| 791 |
+
""")
|
| 792 |
+
|
| 793 |
with gr.Row():
|
| 794 |
with gr.Column(scale=1):
|
| 795 |
+
with gr.Accordion("βοΈ System Settings", open=False):
|
| 796 |
+
gr.Markdown("### Email Configuration")
|
| 797 |
+
email_input = gr.Textbox(
|
| 798 |
+
label="Administration Report Email",
|
| 799 |
+
value=get_current_email,
|
| 800 |
+
placeholder="admin@company.com"
|
| 801 |
+
)
|
| 802 |
+
email_status = gr.Textbox(
|
| 803 |
+
label="Email Status",
|
| 804 |
+
value=get_current_email,
|
| 805 |
+
interactive=False,
|
| 806 |
+
max_lines=2
|
| 807 |
+
)
|
| 808 |
+
update_email_btn = gr.Button("πΎ Save Email", variant="primary")
|
| 809 |
+
|
| 810 |
+
gr.Markdown("### System Status")
|
| 811 |
+
system_status = gr.Textbox(
|
| 812 |
+
label="Configuration",
|
| 813 |
+
value=get_system_status,
|
| 814 |
+
interactive=False,
|
| 815 |
+
max_lines=6
|
| 816 |
+
)
|
| 817 |
+
|
| 818 |
+
with gr.Accordion("π§ͺ Testing & Samples", open=True):
|
| 819 |
+
gr.Markdown("### Quick Test Samples")
|
| 820 |
+
sample_selector = gr.Radio(
|
| 821 |
+
choices=["Equipment Request", "Content Collaboration", "Budget Approval"],
|
| 822 |
+
label="Load Sample Request"
|
| 823 |
+
)
|
| 824 |
+
load_sample_btn = gr.Button("π Load Sample", variant="secondary")
|
| 825 |
|
| 826 |
gr.Markdown("---")
|
| 827 |
gr.Markdown("### π New Coordination Request")
|
|
|
|
| 835 |
|
| 836 |
department_dropdown = gr.Dropdown(
|
| 837 |
choices=["administration", "production", "magazine"],
|
| 838 |
+
label="Your Department",
|
| 839 |
+
value="administration"
|
| 840 |
)
|
| 841 |
|
| 842 |
with gr.Group():
|
|
|
|
| 888 |
stats_display = gr.Textbox(
|
| 889 |
label="Activity Summary",
|
| 890 |
interactive=False,
|
| 891 |
+
max_lines=8
|
| 892 |
)
|
| 893 |
|
| 894 |
with gr.Column(scale=2):
|
| 895 |
gr.Markdown("### π‘ Coordination Guidance")
|
| 896 |
output = gr.Textbox(
|
| 897 |
label="Recommended Actions",
|
| 898 |
+
lines=20,
|
| 899 |
show_copy_button=True
|
| 900 |
)
|
| 901 |
|
| 902 |
with gr.Accordion("π’ Department Overview", open=True):
|
| 903 |
+
gr.Markdown("""
|
| 904 |
+
**Administration**: Overall management, decision making, and coordination
|
| 905 |
+
**Production**: Manufacturing, quality control, and production planning
|
| 906 |
+
**Magazine Management**: Content creation, publishing, and distribution
|
| 907 |
+
|
| 908 |
+
*The AI will analyze your message and any attached images to provide specific coordination recommendations.*
|
| 909 |
+
|
| 910 |
+
---
|
| 911 |
+
|
| 912 |
+
**Testing Tips:**
|
| 913 |
+
- Use the sample requests to quickly test functionality
|
| 914 |
+
- Try different departments and priority levels
|
| 915 |
+
- Upload images to test vision capabilities (if API configured)
|
| 916 |
+
- Monitor the activity log to see coordination history
|
| 917 |
+
""")
|
| 918 |
|
| 919 |
with gr.Accordion("π Recent Activity", open=False):
|
| 920 |
history_display = gr.JSON(
|
| 921 |
label="Activity Log",
|
| 922 |
+
value=lambda: conversation_history[-10:] if conversation_history else []
|
| 923 |
)
|
| 924 |
|
| 925 |
# Event handlers
|
|
|
|
| 935 |
outputs=email_status
|
| 936 |
)
|
| 937 |
|
| 938 |
+
load_sample_btn.click(
|
| 939 |
+
fn=load_sample_request,
|
| 940 |
+
inputs=[sample_selector],
|
| 941 |
+
outputs=[name_input, department_dropdown, message_input, recipient_dropdown, urgency_dropdown]
|
| 942 |
+
)
|
| 943 |
+
|
| 944 |
submit_btn.click(
|
| 945 |
fn=process_coordination_request,
|
| 946 |
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 947 |
outputs=output
|
| 948 |
+
).then(
|
| 949 |
+
fn=lambda: conversation_history[-10:] if conversation_history else [],
|
| 950 |
+
inputs=[],
|
| 951 |
+
outputs=history_display
|
| 952 |
)
|
| 953 |
|
| 954 |
report_btn.click(
|
| 955 |
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),
|
| 956 |
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 957 |
outputs=output
|
| 958 |
+
).then(
|
| 959 |
+
fn=lambda: conversation_history[-10:] if conversation_history else [],
|
| 960 |
+
inputs=[],
|
| 961 |
+
outputs=history_display
|
| 962 |
)
|
| 963 |
|
| 964 |
message_input.submit(
|
| 965 |
fn=process_coordination_request,
|
| 966 |
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown],
|
| 967 |
outputs=output
|
| 968 |
+
).then(
|
| 969 |
+
fn=lambda: conversation_history[-10:] if conversation_history else [],
|
| 970 |
+
inputs=[],
|
| 971 |
+
outputs=history_display
|
| 972 |
)
|
| 973 |
|
| 974 |
clear_btn.click(
|
| 975 |
fn=clear_conversation,
|
| 976 |
inputs=[],
|
| 977 |
outputs=status_display
|
| 978 |
+
).then(
|
| 979 |
+
fn=lambda: [],
|
| 980 |
+
inputs=[],
|
| 981 |
+
outputs=history_display
|
| 982 |
)
|
| 983 |
|
| 984 |
clear_form_btn.click(
|
|
|
|
| 994 |
)
|
| 995 |
|
| 996 |
if __name__ == "__main__":
|
| 997 |
+
ui.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|