Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import json
|
|
| 5 |
import os
|
| 6 |
import requests
|
| 7 |
|
| 8 |
-
|
| 9 |
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 10 |
LLM_LOADED = bool(API_KEY)
|
| 11 |
|
|
@@ -74,7 +74,7 @@ def generate_case_reply(case_data_json):
|
|
| 74 |
if not LLM_LOADED:
|
| 75 |
return "Google API Key not loaded. Please configure it in the Space secrets."
|
| 76 |
|
| 77 |
-
# ---
|
| 78 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key={API_KEY}"
|
| 79 |
|
| 80 |
internal_data = json.loads(case_data_json)
|
|
@@ -82,25 +82,11 @@ def generate_case_reply(case_data_json):
|
|
| 82 |
internal_data.pop('case_id')
|
| 83 |
|
| 84 |
system_prompt = """
|
| 85 |
-
You are an expert AI assistant for "RecoEngine", specializing in Amazon customer support.
|
| 86 |
-
Your task is to analyze a customer's query and associated internal data to generate a structured JSON object.
|
| 87 |
-
This JSON object will be used to populate a standard reply template.
|
| 88 |
-
Your FINAL and ONLY output must be a single, well-formed JSON object. Do not add any text before or after the JSON.
|
| 89 |
-
Here is the required JSON schema:
|
| 90 |
-
{
|
| 91 |
-
"customer_name": "string",
|
| 92 |
-
"paraphrased_issue": "string (A concise summary of the customer's problem)",
|
| 93 |
-
"empathy_adjective": "string (e.g., 'frustrating', 'disappointing', 'concerning')",
|
| 94 |
-
"facts_statement": "string (A statement of fact based on internal data, e.g., 'Your order is delayed in transit.')",
|
| 95 |
-
"action_taken": "string (The resolution step that was performed, e.g., 'I have issued a full refund of ₹XXX.')",
|
| 96 |
-
"next_steps": ["an array of strings detailing what the customer should do or expect next"]
|
| 97 |
-
}
|
| 98 |
"""
|
| 99 |
user_prompt = f"### Customer Query ###\n{customer_query}\n### Internal Data ###\n{json.dumps(internal_data)}"
|
| 100 |
|
| 101 |
-
payload = {
|
| 102 |
-
"contents": [{"parts": [{"text": f"{system_prompt}\n{user_prompt}"}]}]
|
| 103 |
-
}
|
| 104 |
headers = {'Content-Type': 'application/json'}
|
| 105 |
|
| 106 |
try:
|
|
@@ -120,32 +106,8 @@ def generate_case_reply(case_data_json):
|
|
| 120 |
parsed_json = json.loads(json_output_str)
|
| 121 |
|
| 122 |
template = f"""
|
| 123 |
-
Subject: Regarding your recent inquiry about your order
|
| 124 |
-
|
| 125 |
-
Dear {parsed_json.get('customer_name', 'Valued Customer')},
|
| 126 |
-
|
| 127 |
-
Thank you for contacting Amazon Support.
|
| 128 |
-
|
| 129 |
-
I understand that you are concerned about {parsed_json.get('paraphrased_issue', 'your recent issue')}. I know how {parsed_json.get('empathy_adjective', 'concerning')} this can be, and I'm here to help you with this.
|
| 130 |
-
|
| 131 |
-
After reviewing the details, I can see that {parsed_json.get('facts_statement', 'we have reviewed your case.')}.
|
| 132 |
-
|
| 133 |
-
To resolve this for you, {parsed_json.get('action_taken', 'we have taken the necessary action.')}.
|
| 134 |
-
|
| 135 |
-
Here is a summary of the next steps:
|
| 136 |
"""
|
| 137 |
-
for i, step in enumerate(parsed_json.get('next_steps', []), 1):
|
| 138 |
-
template += f"{i}. {step}\n"
|
| 139 |
-
template += """
|
| 140 |
-
If there is anything else you need assistance with, please don't hesitate to reply to this email. We're always here to help.
|
| 141 |
-
|
| 142 |
-
Thank you for your patience and understanding.
|
| 143 |
-
|
| 144 |
-
Best regards,
|
| 145 |
-
|
| 146 |
-
Anjali
|
| 147 |
-
Amazon.in Customer Service
|
| 148 |
-
"""
|
| 149 |
return template
|
| 150 |
except Exception as e:
|
| 151 |
return f"Error parsing API output: {e}\nRaw Output:\n{generated_text}"
|
|
@@ -162,19 +124,13 @@ def generate_prep_center_email(client_name, shipment_id, status, arrival_date, n
|
|
| 162 |
if not LLM_LOADED:
|
| 163 |
return "Google API Key not loaded. Please configure it in the Space secrets."
|
| 164 |
|
| 165 |
-
|
| 166 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key={API_KEY}"
|
| 167 |
|
| 168 |
shipment_data = {"client_name": client_name, "shipment_id": shipment_id, "status": status, "expected_arrival_date": arrival_date, "num_cartons": num_cartons, "labels_provided": labels_provided}
|
| 169 |
-
|
| 170 |
-
system_prompt = """
|
| 171 |
-
You are an expert AI operations assistant... (Your prompt remains the same)
|
| 172 |
-
"""
|
| 173 |
user_prompt = f"Analyze this data and generate the JSON: {json.dumps(shipment_data)}"
|
| 174 |
-
|
| 175 |
-
payload = {
|
| 176 |
-
"contents": [{"parts": [{"text": f"{system_prompt}\n{user_prompt}"}]}]
|
| 177 |
-
}
|
| 178 |
headers = {'Content-Type': 'application/json'}
|
| 179 |
|
| 180 |
try:
|
|
@@ -193,34 +149,7 @@ def generate_prep_center_email(client_name, shipment_id, status, arrival_date, n
|
|
| 193 |
json_output_str = generated_text[start_index:end_index]
|
| 194 |
parsed_json = json.loads(json_output_str)
|
| 195 |
template = f"""
|
| 196 |
-
Subject: {parsed_json.get('urgency_tag')} - Action Required for Your Shipment
|
| 197 |
-
|
| 198 |
-
Dear {client_name},
|
| 199 |
-
|
| 200 |
-
This is an important reminder from PrepCenter India regarding your upcoming shipment.
|
| 201 |
-
|
| 202 |
-
{parsed_json.get('main_call_to_action')}.
|
| 203 |
-
|
| 204 |
-
**Shipment Details:**
|
| 205 |
-
* **Shipment ID:** {shipment_id}
|
| 206 |
-
* **Status:** {status}
|
| 207 |
-
* **Expected Arrival Date:** {arrival_date}
|
| 208 |
-
* **Number of Cartons:** {num_cartons}
|
| 209 |
-
|
| 210 |
-
**Action Required:**
|
| 211 |
-
{parsed_json.get('detailed_action')}
|
| 212 |
-
|
| 213 |
-
**Why is this important?**
|
| 214 |
-
{parsed_json.get('reason_for_action')}
|
| 215 |
-
|
| 216 |
-
**How to complete this action:**
|
| 217 |
-
{parsed_json.get('instructions')}
|
| 218 |
-
|
| 219 |
-
If you have already completed this action, please disregard this email. If you have any questions, please reply to this message.
|
| 220 |
-
|
| 221 |
-
Best regards,
|
| 222 |
-
|
| 223 |
-
The PrepCenter India Team
|
| 224 |
"""
|
| 225 |
return template
|
| 226 |
except Exception as e:
|
|
|
|
| 5 |
import os
|
| 6 |
import requests
|
| 7 |
|
| 8 |
+
# Securely load the API key from Hugging Face Secrets
|
| 9 |
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 10 |
LLM_LOADED = bool(API_KEY)
|
| 11 |
|
|
|
|
| 74 |
if not LLM_LOADED:
|
| 75 |
return "Google API Key not loaded. Please configure it in the Space secrets."
|
| 76 |
|
| 77 |
+
# --- THIS IS THE CORRECTED URL ---
|
| 78 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key={API_KEY}"
|
| 79 |
|
| 80 |
internal_data = json.loads(case_data_json)
|
|
|
|
| 82 |
internal_data.pop('case_id')
|
| 83 |
|
| 84 |
system_prompt = """
|
| 85 |
+
You are an expert AI assistant for "RecoEngine", specializing in Amazon customer support... (Your prompt remains the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
"""
|
| 87 |
user_prompt = f"### Customer Query ###\n{customer_query}\n### Internal Data ###\n{json.dumps(internal_data)}"
|
| 88 |
|
| 89 |
+
payload = { "contents": [{"parts": [{"text": f"{system_prompt}\n{user_prompt}"}]}] }
|
|
|
|
|
|
|
| 90 |
headers = {'Content-Type': 'application/json'}
|
| 91 |
|
| 92 |
try:
|
|
|
|
| 106 |
parsed_json = json.loads(json_output_str)
|
| 107 |
|
| 108 |
template = f"""
|
| 109 |
+
Subject: Regarding your recent inquiry about your order... (Your email template remains the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
return template
|
| 112 |
except Exception as e:
|
| 113 |
return f"Error parsing API output: {e}\nRaw Output:\n{generated_text}"
|
|
|
|
| 124 |
if not LLM_LOADED:
|
| 125 |
return "Google API Key not loaded. Please configure it in the Space secrets."
|
| 126 |
|
| 127 |
+
# --- THIS IS THE CORRECTED URL ---
|
| 128 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key={API_KEY}"
|
| 129 |
|
| 130 |
shipment_data = {"client_name": client_name, "shipment_id": shipment_id, "status": status, "expected_arrival_date": arrival_date, "num_cartons": num_cartons, "labels_provided": labels_provided}
|
| 131 |
+
system_prompt = "You are an expert AI operations assistant..."
|
|
|
|
|
|
|
|
|
|
| 132 |
user_prompt = f"Analyze this data and generate the JSON: {json.dumps(shipment_data)}"
|
| 133 |
+
payload = { "contents": [{"parts": [{"text": f"{system_prompt}\n{user_prompt}"}]}] }
|
|
|
|
|
|
|
|
|
|
| 134 |
headers = {'Content-Type': 'application/json'}
|
| 135 |
|
| 136 |
try:
|
|
|
|
| 149 |
json_output_str = generated_text[start_index:end_index]
|
| 150 |
parsed_json = json.loads(json_output_str)
|
| 151 |
template = f"""
|
| 152 |
+
Subject: {parsed_json.get('urgency_tag')} - Action Required for Your Shipment... (Your email template remains the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
"""
|
| 154 |
return template
|
| 155 |
except Exception as e:
|