Update app.py
Browse files
app.py
CHANGED
|
@@ -36,59 +36,19 @@ departments = {
|
|
| 36 |
}
|
| 37 |
|
| 38 |
# ======================================================
|
| 39 |
-
# TEST EXAMPLES
|
| 40 |
# ======================================================
|
| 41 |
|
| 42 |
examples = [
|
| 43 |
-
[
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"administration",
|
| 49 |
-
True,
|
| 50 |
-
"High"
|
| 51 |
-
],
|
| 52 |
-
[
|
| 53 |
-
"Sara Trabelsi",
|
| 54 |
-
"magazine",
|
| 55 |
-
"Need content approval and printing coordination before tomorrow’s issue.",
|
| 56 |
-
None,
|
| 57 |
-
"administration",
|
| 58 |
-
True,
|
| 59 |
-
"Medium"
|
| 60 |
-
],
|
| 61 |
-
[
|
| 62 |
-
"Mohamed Kefi",
|
| 63 |
-
"administration",
|
| 64 |
-
"Requesting budget validation for new raw materials purchase.",
|
| 65 |
-
None,
|
| 66 |
-
"production",
|
| 67 |
-
True,
|
| 68 |
-
"Medium"
|
| 69 |
-
],
|
| 70 |
-
[
|
| 71 |
-
"Ines Haddad",
|
| 72 |
-
"production",
|
| 73 |
-
"Quality defect rate increased to 7%. Root cause investigation required.",
|
| 74 |
-
None,
|
| 75 |
-
"administration",
|
| 76 |
-
False,
|
| 77 |
-
"High"
|
| 78 |
-
],
|
| 79 |
-
[
|
| 80 |
-
"Karim Jaziri",
|
| 81 |
-
"magazine",
|
| 82 |
-
"Vendor delayed magazine printing. Need backup supplier urgently.",
|
| 83 |
-
None,
|
| 84 |
-
"administration",
|
| 85 |
-
True,
|
| 86 |
-
"High"
|
| 87 |
-
],
|
| 88 |
]
|
| 89 |
|
| 90 |
# ======================================================
|
| 91 |
-
#
|
| 92 |
# ======================================================
|
| 93 |
|
| 94 |
def update_report_email(new_email):
|
|
@@ -133,117 +93,45 @@ def send_email_brevo_api(to_email, subject, html_content, from_name="AI Coordina
|
|
| 133 |
return f"❌ Email error: {str(e)}"
|
| 134 |
|
| 135 |
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
| 136 |
-
"""Use LLM to analyze coordination needs and suggest actions - with image support"""
|
| 137 |
-
|
| 138 |
if image:
|
| 139 |
-
# Convert image to base64 for vision model
|
| 140 |
buffered = io.BytesIO()
|
| 141 |
image.save(buffered, format="PNG")
|
| 142 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 143 |
-
|
| 144 |
-
prompt = f"""As a helpful coordination assistant, analyze this request from {user_name} in the {department} department along with the attached image.
|
| 145 |
-
|
| 146 |
-
Please provide clear, practical advice in plain English that's easy for anyone to understand.
|
| 147 |
-
|
| 148 |
-
REQUEST DETAILS:
|
| 149 |
-
- Department: {department}
|
| 150 |
-
- Requestor: {user_name}
|
| 151 |
-
- Message: {user_message}
|
| 152 |
-
|
| 153 |
-
Please analyze both the text and image to provide helpful guidance about:
|
| 154 |
-
|
| 155 |
-
1. Which teams need to be involved
|
| 156 |
-
2. What specific steps should be taken next
|
| 157 |
-
3. Any important information from the image
|
| 158 |
-
4. What priority this request should have
|
| 159 |
-
5. What follow-up might be needed
|
| 160 |
-
|
| 161 |
-
Please format your response in a friendly, conversational way that's easy to read and act upon."""
|
| 162 |
-
|
| 163 |
try:
|
| 164 |
response = requests.post(
|
| 165 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 166 |
-
headers={
|
| 167 |
-
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 168 |
-
"Content-Type": "application/json",
|
| 169 |
-
},
|
| 170 |
json={
|
| 171 |
-
"model": "google/gemma-3-27b-it:free",
|
| 172 |
-
"messages": [
|
| 173 |
-
|
| 174 |
-
"role": "user",
|
| 175 |
-
"content": [
|
| 176 |
-
{
|
| 177 |
-
"type": "text",
|
| 178 |
-
"text": prompt
|
| 179 |
-
},
|
| 180 |
-
{
|
| 181 |
-
"type": "image_url",
|
| 182 |
-
"image_url": {
|
| 183 |
-
"url": f"data:image/png;base64,{img_base64}"
|
| 184 |
-
}
|
| 185 |
-
}
|
| 186 |
-
]
|
| 187 |
-
}
|
| 188 |
-
],
|
| 189 |
-
"temperature": 0.3
|
| 190 |
},
|
| 191 |
timeout=60
|
| 192 |
)
|
| 193 |
-
|
| 194 |
if response.status_code == 200:
|
| 195 |
data = response.json()
|
| 196 |
return data["choices"][0]["message"]["content"]
|
| 197 |
else:
|
| 198 |
-
return "I'm having trouble analyzing the image right now. Please try again
|
| 199 |
-
|
| 200 |
except Exception as e:
|
| 201 |
-
return f"
|
| 202 |
-
|
| 203 |
else:
|
| 204 |
-
|
| 205 |
-
prompt = f"""As a helpful coordination assistant, analyze this request from {user_name} in the {department} department.
|
| 206 |
-
|
| 207 |
-
Please provide clear, practical advice in plain English that's easy for anyone to understand.
|
| 208 |
-
|
| 209 |
-
REQUEST DETAILS:
|
| 210 |
-
- Department: {department}
|
| 211 |
-
- Requestor: {user_name}
|
| 212 |
-
- Message: {user_message}
|
| 213 |
-
|
| 214 |
-
Please provide helpful guidance about:
|
| 215 |
-
- Which teams need to be involved
|
| 216 |
-
- What specific steps should be taken next
|
| 217 |
-
- What priority this request should have
|
| 218 |
-
- What follow-up might be needed
|
| 219 |
-
|
| 220 |
-
Please format your response in a friendly, conversational way that's easy to read and act upon. Avoid technical jargon and use bullet points or simple sections to organize your thoughts."""
|
| 221 |
-
|
| 222 |
try:
|
| 223 |
response = requests.post(
|
| 224 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 225 |
-
headers={
|
| 226 |
-
|
| 227 |
-
"Content-Type": "application/json",
|
| 228 |
-
},
|
| 229 |
-
json={
|
| 230 |
-
"model": "google/gemma-3-27b-it:free",
|
| 231 |
-
"messages": [{"role": "user", "content": prompt}],
|
| 232 |
-
"temperature": 0.3
|
| 233 |
-
},
|
| 234 |
timeout=30
|
| 235 |
)
|
| 236 |
-
|
| 237 |
if response.status_code == 200:
|
| 238 |
data = response.json()
|
| 239 |
return data["choices"][0]["message"]["content"]
|
| 240 |
else:
|
| 241 |
-
return "I'm having trouble analyzing your request right now. Please try again
|
| 242 |
-
|
| 243 |
except Exception as e:
|
| 244 |
-
return f"
|
| 245 |
-
|
| 246 |
-
# ... The rest of your original functions (send_department_email, send_email_report, add_to_conversation_history, process_coordination_request, etc.) remain unchanged ...
|
| 247 |
|
| 248 |
# ======================================================
|
| 249 |
# GRADIO UI
|
|
@@ -255,26 +143,22 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Coordination System") as ui:
|
|
| 255 |
|
| 256 |
with gr.Row():
|
| 257 |
with gr.Column(scale=1):
|
| 258 |
-
# Form Inputs
|
| 259 |
name_input = gr.Textbox(label="Your Name", placeholder="Enter your full name")
|
| 260 |
department_dropdown = gr.Dropdown(choices=["administration", "production", "magazine"], label="Your Department")
|
| 261 |
-
message_input = gr.Textbox(label="What do you need help with?", lines=4
|
| 262 |
image_input = gr.Image(label="📷 Attach Image (Optional)", type="pil")
|
| 263 |
recipient_dropdown = gr.Dropdown(choices=["administration", "production", "magazine"], label="Notify Department")
|
| 264 |
-
urgency_dropdown = gr.Dropdown(choices=["Normal",
|
| 265 |
send_email_checkbox = gr.Checkbox(label="Send email notification", value=True)
|
| 266 |
-
|
| 267 |
submit_btn = gr.Button("🚀 Get Coordination Help", variant="primary")
|
| 268 |
output = gr.Textbox(lines=18, label="Recommended Actions", show_copy_button=True)
|
| 269 |
|
| 270 |
-
#
|
| 271 |
-
# EXAMPLES COMPONENT
|
| 272 |
-
# ======================================================
|
| 273 |
gr.Markdown("### 🧪 Quick Test Examples")
|
| 274 |
gr.Examples(
|
| 275 |
examples=examples,
|
| 276 |
-
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown]
|
| 277 |
-
label="Click an example to auto-fill the form and test the workflow"
|
| 278 |
)
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
# ======================================================
|
| 39 |
+
# TEST EXAMPLES
|
| 40 |
# ======================================================
|
| 41 |
|
| 42 |
examples = [
|
| 43 |
+
["Ali Ben Salah", "production", "Packaging machine #4 stopped. Production is blocked. Need urgent maintenance support.", None, "administration", True, "High"],
|
| 44 |
+
["Sara Trabelsi", "magazine", "Need content approval and printing coordination before tomorrow’s issue.", None, "administration", True, "Medium"],
|
| 45 |
+
["Mohamed Kefi", "administration", "Requesting budget validation for new raw materials purchase.", None, "production", True, "Medium"],
|
| 46 |
+
["Ines Haddad", "production", "Quality defect rate increased to 7%. Root cause investigation required.", None, "administration", False, "High"],
|
| 47 |
+
["Karim Jaziri", "magazine", "Vendor delayed magazine printing. Need backup supplier urgently.", None, "administration", True, "High"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
]
|
| 49 |
|
| 50 |
# ======================================================
|
| 51 |
+
# ORIGINAL FUNCTIONS (UNCHANGED)
|
| 52 |
# ======================================================
|
| 53 |
|
| 54 |
def update_report_email(new_email):
|
|
|
|
| 93 |
return f"❌ Email error: {str(e)}"
|
| 94 |
|
| 95 |
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
|
|
|
|
|
|
| 96 |
if image:
|
|
|
|
| 97 |
buffered = io.BytesIO()
|
| 98 |
image.save(buffered, format="PNG")
|
| 99 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 100 |
+
prompt = f"""As a helpful coordination assistant, analyze this request from {user_name} in the {department} department along with the attached image. REQUEST: {user_message}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
try:
|
| 102 |
response = requests.post(
|
| 103 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 104 |
+
headers={"Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json"},
|
|
|
|
|
|
|
|
|
|
| 105 |
json={
|
| 106 |
+
"model": "google/gemma-3-27b-it:free",
|
| 107 |
+
"messages": [{"role":"user","content":[{"type":"text","text":prompt},{"type":"image_url","image_url":{"url":f"data:image/png;base64,{img_base64}"}}]}],
|
| 108 |
+
"temperature":0.3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
},
|
| 110 |
timeout=60
|
| 111 |
)
|
|
|
|
| 112 |
if response.status_code == 200:
|
| 113 |
data = response.json()
|
| 114 |
return data["choices"][0]["message"]["content"]
|
| 115 |
else:
|
| 116 |
+
return "I'm having trouble analyzing the image right now. Please try again later."
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
+
return f"Error: {str(e)}"
|
|
|
|
| 119 |
else:
|
| 120 |
+
prompt = f"As a helpful coordination assistant, analyze this request from {user_name} in the {department} department. REQUEST: {user_message}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
try:
|
| 122 |
response = requests.post(
|
| 123 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 124 |
+
headers={"Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json"},
|
| 125 |
+
json={"model": "google/gemma-3-27b-it:free", "messages":[{"role":"user","content":prompt}],"temperature":0.3},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
timeout=30
|
| 127 |
)
|
|
|
|
| 128 |
if response.status_code == 200:
|
| 129 |
data = response.json()
|
| 130 |
return data["choices"][0]["message"]["content"]
|
| 131 |
else:
|
| 132 |
+
return "I'm having trouble analyzing your request right now. Please try again later."
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# ======================================================
|
| 137 |
# GRADIO UI
|
|
|
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column(scale=1):
|
|
|
|
| 146 |
name_input = gr.Textbox(label="Your Name", placeholder="Enter your full name")
|
| 147 |
department_dropdown = gr.Dropdown(choices=["administration", "production", "magazine"], label="Your Department")
|
| 148 |
+
message_input = gr.Textbox(label="What do you need help with?", lines=4)
|
| 149 |
image_input = gr.Image(label="📷 Attach Image (Optional)", type="pil")
|
| 150 |
recipient_dropdown = gr.Dropdown(choices=["administration", "production", "magazine"], label="Notify Department")
|
| 151 |
+
urgency_dropdown = gr.Dropdown(choices=["Normal","Medium","High"], value="Normal", label="Priority Level")
|
| 152 |
send_email_checkbox = gr.Checkbox(label="Send email notification", value=True)
|
| 153 |
+
|
| 154 |
submit_btn = gr.Button("🚀 Get Coordination Help", variant="primary")
|
| 155 |
output = gr.Textbox(lines=18, label="Recommended Actions", show_copy_button=True)
|
| 156 |
|
| 157 |
+
# ✅ WORKING EXAMPLES
|
|
|
|
|
|
|
| 158 |
gr.Markdown("### 🧪 Quick Test Examples")
|
| 159 |
gr.Examples(
|
| 160 |
examples=examples,
|
| 161 |
+
inputs=[name_input, department_dropdown, message_input, image_input, recipient_dropdown, send_email_checkbox, urgency_dropdown]
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|