Update app.py
Browse files
app.py
CHANGED
|
@@ -1,339 +1,183 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import base64
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import io
|
| 7 |
-
import datetime
|
| 8 |
-
import resend
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# Store conversation history and settings
|
| 19 |
-
conversation_history = []
|
| 20 |
-
current_report_email = DEFAULT_REPORT_EMAIL
|
| 21 |
-
|
| 22 |
-
def update_report_email(new_email):
|
| 23 |
-
"""Update the report email address"""
|
| 24 |
-
global current_report_email
|
| 25 |
-
if new_email and "@" in new_email and "." in new_email:
|
| 26 |
-
current_report_email = new_email.strip()
|
| 27 |
-
return f"✅ Report email updated to: {current_report_email}"
|
| 28 |
-
else:
|
| 29 |
-
return "❌ Please enter a valid email address"
|
| 30 |
-
|
| 31 |
-
def send_email_report(conversation_data):
|
| 32 |
-
"""Send conversation report directly via Resend"""
|
| 33 |
-
if not RESEND_API_KEY or not current_report_email:
|
| 34 |
-
return "Email reporting not configured - missing Resend API key or report email"
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Create HTML email content
|
| 41 |
-
html_content = f"""
|
| 42 |
-
<html>
|
| 43 |
-
<head>
|
| 44 |
-
<style>
|
| 45 |
-
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 46 |
-
.header {{ background-color: #f4f4f4; padding: 20px; border-radius: 8px; margin-bottom: 20px; }}
|
| 47 |
-
.exchange {{ border: 1px solid #e0e0e0; margin: 15px 0; padding: 15px; border-radius: 8px; background-color: #fafafa; }}
|
| 48 |
-
.user {{ color: #2563eb; font-weight: 600; }}
|
| 49 |
-
.ai {{ color: #059669; }}
|
| 50 |
-
.timestamp {{ color: #6b7280; font-size: 0.8em; font-style: italic; }}
|
| 51 |
-
.image-info {{ background-color: #fef3c7; padding: 5px 10px; border-radius: 4px; display: inline-block; margin: 5px 0; }}
|
| 52 |
-
</style>
|
| 53 |
-
</head>
|
| 54 |
-
<body>
|
| 55 |
-
<div class="header">
|
| 56 |
-
<h1 style="margin: 0; color: #1f2937;">🤖 Chat Conversation Report</h1>
|
| 57 |
-
<p style="margin: 5px 0;"><strong>Date:</strong> {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 58 |
-
<p style="margin: 5px 0;"><strong>Total Exchanges:</strong> {len(conversation_data)}</p>
|
| 59 |
-
<p style="margin: 5px 0;"><strong>Sent to:</strong> {current_report_email}</p>
|
| 60 |
-
</div>
|
| 61 |
-
"""
|
| 62 |
-
|
| 63 |
-
for i, entry in enumerate(conversation_data, 1):
|
| 64 |
-
html_content += f"""
|
| 65 |
-
<div class="exchange">
|
| 66 |
-
<h3 style="margin-top: 0; color: #374151;">Exchange #{i}</h3>
|
| 67 |
-
<p class="user"><strong>👤 User:</strong> {entry['user_input']}</p>
|
| 68 |
-
{f"<p class='image-info'>📷 Image: {entry.get('image_filename', 'Uploaded image')}</p>" if entry.get('image_uploaded') else ""}
|
| 69 |
-
<p class="ai"><strong>🤖 AI:</strong> {entry['ai_response']}</p>
|
| 70 |
-
<p class="timestamp">{entry['timestamp']}</p>
|
| 71 |
-
</div>
|
| 72 |
-
"""
|
| 73 |
-
|
| 74 |
-
html_content += "</body></html>"
|
| 75 |
-
|
| 76 |
-
# Send email directly using Resend
|
| 77 |
-
params = {
|
| 78 |
"from": "Chat App <onboarding@resend.dev>",
|
| 79 |
-
"to": [
|
| 80 |
"subject": subject,
|
| 81 |
-
"html":
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
email = resend.Emails.send(params)
|
| 85 |
-
|
| 86 |
-
return f"📧 Report sent successfully to {current_report_email}"
|
| 87 |
-
|
| 88 |
except Exception as e:
|
| 89 |
-
return f"
|
| 90 |
-
|
| 91 |
-
def
|
| 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 |
-
return ai_response
|
| 128 |
-
|
| 129 |
-
def ask_llm(message):
|
| 130 |
-
"""Text-only query to OpenRouter"""
|
| 131 |
try:
|
| 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 |
-
response = requests.post(
|
| 163 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 164 |
-
headers={
|
| 165 |
-
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 166 |
-
"Content-Type": "application/json",
|
| 167 |
-
},
|
| 168 |
-
json={
|
| 169 |
-
"model": "google/gemini-flash-1.5:free", # Vision model
|
| 170 |
-
"messages": [
|
| 171 |
-
{
|
| 172 |
-
"role": "user",
|
| 173 |
-
"content": [
|
| 174 |
-
{
|
| 175 |
-
"type": "text",
|
| 176 |
-
"text": message
|
| 177 |
-
},
|
| 178 |
-
{
|
| 179 |
-
"type": "image_url",
|
| 180 |
-
"image_url": {
|
| 181 |
-
"url": f"data:image/png;base64,{img_base64}"
|
| 182 |
-
}
|
| 183 |
-
}
|
| 184 |
-
]
|
| 185 |
-
}
|
| 186 |
-
]
|
| 187 |
-
},
|
| 188 |
-
timeout=30
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
if response.status_code == 200:
|
| 192 |
-
data = response.json()
|
| 193 |
-
return data["choices"][0]["message"]["content"]
|
| 194 |
else:
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
def
|
| 201 |
-
"""
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
total_exchanges = len(conversation_history)
|
| 212 |
-
image_uploads = sum(1 for entry in conversation_history if entry['image_uploaded'])
|
| 213 |
-
last_time = conversation_history[-1]['timestamp'] if conversation_history else "N/A"
|
| 214 |
-
|
| 215 |
-
return f"Exchanges: {total_exchanges} | Images: {image_uploads} | Last: {last_time}"
|
| 216 |
|
| 217 |
-
|
| 218 |
-
"""Get the current report email address"""
|
| 219 |
-
return current_report_email if current_report_email else "Not set"
|
| 220 |
|
| 221 |
-
#
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
# 🤖 OpenRouter Chat with Email Reports
|
| 225 |
-
*Powered by OpenRouter API + Resend Email*
|
| 226 |
-
""")
|
| 227 |
-
|
| 228 |
-
with gr.Row():
|
| 229 |
-
with gr.Column(scale=1):
|
| 230 |
-
gr.Markdown("### 📧 Email Settings")
|
| 231 |
-
email_input = gr.Textbox(
|
| 232 |
-
label="Report Email Address",
|
| 233 |
-
value=get_current_email,
|
| 234 |
-
placeholder="Enter email address for reports...",
|
| 235 |
-
info="This email will receive conversation reports"
|
| 236 |
-
)
|
| 237 |
-
email_status = gr.Textbox(
|
| 238 |
-
label="Email Status",
|
| 239 |
-
value=get_current_email,
|
| 240 |
-
interactive=False,
|
| 241 |
-
max_lines=2
|
| 242 |
-
)
|
| 243 |
-
update_email_btn = gr.Button("💾 Update Email", variant="primary")
|
| 244 |
-
|
| 245 |
-
gr.Markdown("---")
|
| 246 |
-
gr.Markdown("### 💬 Chat Input")
|
| 247 |
-
text_input = gr.Textbox(
|
| 248 |
-
label="Your message:",
|
| 249 |
-
lines=3,
|
| 250 |
-
placeholder="Type your message here...",
|
| 251 |
-
max_lines=5
|
| 252 |
-
)
|
| 253 |
-
image_input = gr.Image(
|
| 254 |
-
label="📷 Upload an image (optional)",
|
| 255 |
-
type="pil",
|
| 256 |
-
height=200
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
-
with gr.Row():
|
| 260 |
-
submit_btn = gr.Button("🚀 Submit", variant="primary", size="lg")
|
| 261 |
-
report_btn = gr.Button("📧 Submit & Email Report", variant="secondary")
|
| 262 |
-
|
| 263 |
-
with gr.Row():
|
| 264 |
-
clear_btn = gr.Button("🗑️ Clear History", variant="stop")
|
| 265 |
-
stats_btn = gr.Button("📊 Get Stats")
|
| 266 |
-
|
| 267 |
-
status_display = gr.Textbox(
|
| 268 |
-
label="Status",
|
| 269 |
-
interactive=False,
|
| 270 |
-
max_lines=2
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
stats_display = gr.Textbox(
|
| 274 |
-
label="Conversation Statistics",
|
| 275 |
-
interactive=False,
|
| 276 |
-
max_lines=2
|
| 277 |
-
)
|
| 278 |
-
|
| 279 |
-
with gr.Column(scale=2):
|
| 280 |
-
gr.Markdown("### 🤖 AI Response")
|
| 281 |
-
output = gr.Textbox(
|
| 282 |
-
label="Response",
|
| 283 |
-
lines=12,
|
| 284 |
-
show_copy_button=True
|
| 285 |
-
)
|
| 286 |
-
|
| 287 |
-
with gr.Accordion("📜 Conversation History (Last 10)", open=False):
|
| 288 |
-
history_display = gr.JSON(
|
| 289 |
-
label="Recent History",
|
| 290 |
-
value=conversation_history[-10:] if conversation_history else [],
|
| 291 |
-
every=2
|
| 292 |
-
)
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
fn=update_report_email,
|
| 297 |
-
inputs=[email_input],
|
| 298 |
-
outputs=email_status
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
email_input.submit(
|
| 302 |
-
fn=update_report_email,
|
| 303 |
-
inputs=[email_input],
|
| 304 |
-
outputs=email_status
|
| 305 |
-
)
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
fn=process_input,
|
| 310 |
-
inputs=[text_input, image_input, gr.Checkbox(value=False, visible=False)],
|
| 311 |
-
outputs=output
|
| 312 |
-
)
|
| 313 |
-
|
| 314 |
-
report_btn.click(
|
| 315 |
-
fn=process_input,
|
| 316 |
-
inputs=[text_input, image_input, gr.Checkbox(value=True, visible=False)],
|
| 317 |
-
outputs=output
|
| 318 |
-
)
|
| 319 |
-
|
| 320 |
-
text_input.submit(
|
| 321 |
-
fn=process_input,
|
| 322 |
-
inputs=[text_input, image_input, gr.Checkbox(value=False, visible=False)],
|
| 323 |
-
outputs=output
|
| 324 |
-
)
|
| 325 |
-
|
| 326 |
-
clear_btn.click(
|
| 327 |
-
fn=clear_conversation,
|
| 328 |
-
inputs=[],
|
| 329 |
-
outputs=status_display
|
| 330 |
-
)
|
| 331 |
-
|
| 332 |
-
stats_btn.click(
|
| 333 |
-
fn=get_conversation_stats,
|
| 334 |
-
inputs=[],
|
| 335 |
-
outputs=stats_display
|
| 336 |
-
)
|
| 337 |
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -------------------------------
|
| 2 |
+
# Coordinator & email helper additions
|
| 3 |
+
# -------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
ADMIN_EMAIL = os.getenv("ADMIN_EMAIL")
|
| 6 |
+
PRODUCTION_MANAGER_EMAIL = os.getenv("PRODUCTION_MANAGER_EMAIL")
|
| 7 |
+
MAGAZINE_MANAGER_EMAIL = os.getenv("MAGAZINE_MANAGER_EMAIL")
|
|
|
|
| 8 |
|
| 9 |
+
def send_email_via_resend(to_email, subject, html_body):
|
| 10 |
+
"""Send an email via Resend; returns status string."""
|
| 11 |
+
if not RESEND_API_KEY:
|
| 12 |
+
return "❌ Resend API key not set"
|
| 13 |
+
if not to_email or "@" not in to_email:
|
| 14 |
+
return "❌ Invalid recipient email"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
+
resend.api_key = RESEND_API_KEY
|
| 18 |
+
resend.Emails.send({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"from": "Chat App <onboarding@resend.dev>",
|
| 20 |
+
"to": [to_email],
|
| 21 |
"subject": subject,
|
| 22 |
+
"html": html_body,
|
| 23 |
+
})
|
| 24 |
+
return f"✅ Email sent to {to_email}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
+
return f"❌ Email failed: {e}"
|
| 27 |
+
|
| 28 |
+
def route_request_via_llm(user_message, image=None):
|
| 29 |
+
"""
|
| 30 |
+
Ask the LLM to classify the request and prepare an email draft.
|
| 31 |
+
It will try to return a dict with keys:
|
| 32 |
+
- category (production|magazine|administration|other)
|
| 33 |
+
- manager (one of: production, magazine, administration) OR manager_email
|
| 34 |
+
- subject
|
| 35 |
+
- body
|
| 36 |
+
- suggestion (LLM-suggested next step for staff)
|
| 37 |
+
Falls back to simple keyword mapping if parsing fails.
|
| 38 |
+
"""
|
| 39 |
+
# Prompt instructing the LLM to return JSON only
|
| 40 |
+
prompt = f"""
|
| 41 |
+
You are an assistant that classifies employee requests and drafts an email to the responsible manager.
|
| 42 |
+
Return ONLY valid JSON with these fields:
|
| 43 |
+
{{
|
| 44 |
+
"category": "<production|magazine|administration|other>",
|
| 45 |
+
"manager": "<production|magazine|administration>",
|
| 46 |
+
"subject": "<short email subject>",
|
| 47 |
+
"body": "<detailed email body to send to the manager>",
|
| 48 |
+
"suggestion": "<one-line suggestion for next steps>"
|
| 49 |
+
}}
|
| 50 |
+
User request: {user_message}
|
| 51 |
+
If unsure, use 'other' for category and 'administration' for manager.
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
# Use your existing ask_llm (text-only) for classification; attach image info if provided
|
| 55 |
+
msg = prompt
|
| 56 |
+
if image is not None:
|
| 57 |
+
msg += "\nNote: The user attached an image. Describe it briefly in the body if relevant."
|
| 58 |
+
|
| 59 |
+
llm_text = ask_llm(msg)
|
| 60 |
+
|
| 61 |
+
# Try to parse JSON from the llm_text
|
| 62 |
+
import json
|
| 63 |
+
parsed = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
+
# sometimes LLMs wrap json in backticks or text - find first '{' and last '}' and parse
|
| 66 |
+
start = llm_text.find('{')
|
| 67 |
+
end = llm_text.rfind('}')
|
| 68 |
+
if start != -1 and end != -1:
|
| 69 |
+
jstr = llm_text[start:end+1]
|
| 70 |
+
parsed = json.loads(jstr)
|
| 71 |
+
except Exception:
|
| 72 |
+
parsed = None
|
| 73 |
+
|
| 74 |
+
# Fallback keyword-based simple mapping if parse failed
|
| 75 |
+
if not parsed:
|
| 76 |
+
cat = "other"
|
| 77 |
+
mgr = "administration"
|
| 78 |
+
text = user_message.lower()
|
| 79 |
+
if any(k in text for k in ["tape", "packing", "tool", "machine", "production", "line"]):
|
| 80 |
+
cat = "production"; mgr = "production"
|
| 81 |
+
elif any(k in text for k in ["magazine", "stock", "warehouse", "inventory", "store"]):
|
| 82 |
+
cat = "magazine"; mgr = "magazine"
|
| 83 |
+
elif any(k in text for k in ["wifi", "access", "account", "admin", "policy"]):
|
| 84 |
+
cat = "administration"; mgr = "administration"
|
| 85 |
+
|
| 86 |
+
parsed = {
|
| 87 |
+
"category": cat,
|
| 88 |
+
"manager": mgr,
|
| 89 |
+
"subject": f"Request ({cat}): {user_message[:50]}",
|
| 90 |
+
"body": f"Employee request: {user_message}\n\nPlease take action and reply with status.",
|
| 91 |
+
"suggestion": "Auto-suggestion not available (fallback)."
|
| 92 |
+
}
|
| 93 |
|
| 94 |
+
# Normalize manager_email
|
| 95 |
+
manager_key = parsed.get("manager")
|
| 96 |
+
manager_email = parsed.get("manager_email") or ""
|
| 97 |
+
if not manager_email:
|
| 98 |
+
if manager_key == "production":
|
| 99 |
+
manager_email = PRODUCTION_MANAGER_EMAIL
|
| 100 |
+
elif manager_key == "magazine":
|
| 101 |
+
manager_email = MAGAZINE_MANAGER_EMAIL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
else:
|
| 103 |
+
manager_email = ADMIN_EMAIL
|
| 104 |
+
|
| 105 |
+
parsed["manager_email"] = manager_email
|
| 106 |
+
return parsed
|
| 107 |
+
|
| 108 |
+
def handle_request_and_route(user_message, image=None, send_report=False):
|
| 109 |
+
"""
|
| 110 |
+
Orchestrator to:
|
| 111 |
+
- classify via LLM
|
| 112 |
+
- send email to manager
|
| 113 |
+
- always send a report copy to admin
|
| 114 |
+
- append actions to conversation_history and return reply status + LLM suggestion
|
| 115 |
+
"""
|
| 116 |
+
# 1) Ask the LLM for suggested next step and classification (use existing ask_llm to create a "assistant suggestion")
|
| 117 |
+
classification = route_request_via_llm(user_message, image=image)
|
| 118 |
+
|
| 119 |
+
# 2) Compose email to manager
|
| 120 |
+
subject = classification.get("subject", f"Request: {user_message[:50]}")
|
| 121 |
+
body = classification.get("body", f"Employee request:\n\n{user_message}")
|
| 122 |
+
manager_email = classification.get("manager_email")
|
| 123 |
+
|
| 124 |
+
# include a short header in the email showing original request and suggested action
|
| 125 |
+
html_body = f"""
|
| 126 |
+
<h3>Automated Request Forward</h3>
|
| 127 |
+
<p><strong>Category:</strong> {classification.get('category')}</p>
|
| 128 |
+
<p><strong>User message:</strong> {user_message}</p>
|
| 129 |
+
<p><strong>Suggested next step:</strong> {classification.get('suggestion')}</p>
|
| 130 |
+
<hr/>
|
| 131 |
+
<p>{body}</p>
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
# 3) Send to manager
|
| 135 |
+
manager_status = send_email_via_resend(manager_email, subject, html_body)
|
| 136 |
+
|
| 137 |
+
# 4) Always send a copy/report to administration (if ADMIN_EMAIL set)
|
| 138 |
+
admin_status = "Admin email not configured"
|
| 139 |
+
if ADMIN_EMAIL:
|
| 140 |
+
admin_subject = f"[COPY] {subject}"
|
| 141 |
+
admin_html = f"""
|
| 142 |
+
<h3>Copy of forwarded request</h3>
|
| 143 |
+
<p><strong>Forwarded to:</strong> {manager_email}</p>
|
| 144 |
+
{html_body}
|
| 145 |
+
"""
|
| 146 |
+
admin_status = send_email_via_resend(ADMIN_EMAIL, admin_subject, admin_html)
|
| 147 |
+
|
| 148 |
+
# 5) Append to conversation history
|
| 149 |
+
# Save the user's request and the assistant suggestion as exchanges
|
| 150 |
+
suggestion_text = classification.get("suggestion", "")
|
| 151 |
+
add_to_conversation_history(user_message, image, suggestion_text)
|
| 152 |
+
|
| 153 |
+
# 6) Return a combined message for UI
|
| 154 |
+
result_text = (
|
| 155 |
+
f"LLM suggestion: {suggestion_text}\n\n"
|
| 156 |
+
f"Manager email send status: {manager_status}\n"
|
| 157 |
+
f"Admin report status: {admin_status}"
|
| 158 |
+
)
|
| 159 |
|
| 160 |
+
# Optionally send a full email report using your existing send_email_report flow
|
| 161 |
+
if send_report:
|
| 162 |
+
report_status = send_email_report(conversation_history)
|
| 163 |
+
result_text += f"\n\nFull conversation report status: {report_status}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
return result_text
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
# -------------------------------
|
| 168 |
+
# UI: add Request & Route button and binding
|
| 169 |
+
# -------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
# Inside your Gradio UI block, after defining buttons, add this:
|
| 172 |
+
# (If you already defined send_btn, report_btn, etc., put this in the same area.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
# new button
|
| 175 |
+
route_btn = gr.Button("📬 Request & Route", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
# Hook it to the handler. It receives same inputs as process_input but will route instead of just chat.
|
| 178 |
+
# (Place this next to your other .click() binding calls in the UI section)
|
| 179 |
+
route_btn.click(
|
| 180 |
+
fn=handle_request_and_route,
|
| 181 |
+
inputs=[text_input, image_input, gr.Checkbox(value=False, visible=False)], # last checkbox is send_report flag
|
| 182 |
+
outputs=output
|
| 183 |
+
)
|