ShettyAkshay commited on
Commit
1007e07
·
verified ·
1 Parent(s): 700222b

Files uploaded

Browse files
app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from prophet import Prophet
4
+ import json
5
+ import os
6
+ import google.generativeai as genai
7
+
8
+ try:
9
+ api_key = os.environ.get("GOOGLE_API_KEY")
10
+ genai.configure(api_key=api_key)
11
+ print("Google AI API key configured successfully!")
12
+ LLM_LOADED = True
13
+ model = genai.GenerativeModel('gemini-pro')
14
+ except Exception as e:
15
+ print(f"Could not configure Google AI. AI features will be disabled. Error: {e}")
16
+ LLM_LOADED = False
17
+ model = None
18
+
19
+
20
+ def create_cases_data():
21
+ """Creates a dummy CSV with customer case data."""
22
+ cases_data = {
23
+ 'case_id': [101, 102],
24
+ 'customer_name': ['Rohan Gupta', 'Priya Sharma'],
25
+ 'customer_query': [
26
+ "The Leather care conditioner 500 ml I ordered (Order #404-7654321-1234567) just arrived, but the bottle is leaking. I want a replacement.",
27
+ "Hi, my order for the Cockpit Cleaner Matt 500 ml (Order #404-1234567-9876543) was supposed to be delivered yesterday, but the tracking says it's delayed. Where is it?"
28
+ ],
29
+ 'order_number': ['404-7654321-1234567', '404-1234567-9876543'],
30
+ 'item_name': ['Leather care conditioner 500 ml', 'Cockpit Cleaner Matt 500 ml'],
31
+ 'order_status': ['Delivered', 'Shipped']
32
+ }
33
+ df_cases = pd.DataFrame(cases_data)
34
+ df_cases.to_csv('customer_cases.csv', index=False)
35
+
36
+ create_cases_data()
37
+
38
+ # === USE CASE 1: Future Sales Prediction ===
39
+ def predict_future_sales(sku_choice):
40
+ if not sku_choice:
41
+ return "Please select a SKU."
42
+
43
+ df_sales = pd.read_csv('dummy_sales_history.csv')
44
+ df_sku = df_sales[df_sales['sku'] == sku_choice]
45
+
46
+ if df_sku.empty:
47
+ return f"No sales data could be found for '{sku_choice}'. Please check the CSV file."
48
+
49
+ df_sku = df_sku.rename(columns={'date': 'ds', 'units_sold': 'y'})
50
+ df_sku['ds'] = pd.to_datetime(df_sku['ds'])
51
+
52
+ model_prophet = Prophet(daily_seasonality=False, weekly_seasonality=True, yearly_seasonality=True)
53
+ model_prophet.fit(df_sku)
54
+ future = model_prophet.make_future_dataframe(periods=30)
55
+ forecast = model_prophet.predict(future)
56
+
57
+ thirty_day_forecast = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(30)
58
+ total_sales = int(thirty_day_forecast['yhat'].sum())
59
+ lower_bound = int(thirty_day_forecast['yhat_lower'].sum())
60
+ upper_bound = int(thirty_day_forecast['yhat_upper'].sum())
61
+
62
+ return f"Predicted sales for **{sku_choice}** in the next 30 days: **{total_sales} units**.\nPrediction Range: *{lower_bound} to {upper_bound} units*."
63
+
64
+ # === USE CASE 2: Automated Case Reply Generation ===
65
+ def get_open_cases():
66
+ if not os.path.exists('customer_cases.csv'):
67
+ return []
68
+ df_cases = pd.read_csv('customer_cases.csv')
69
+ return [f"Case {row['case_id']}: {row['customer_name']}" for index, row in df_cases.iterrows()]
70
+
71
+ def get_case_details(case_selection):
72
+ if not case_selection:
73
+ return "", "", gr.update(visible=False)
74
+
75
+ case_id = int(case_selection.split(':')[0].replace('Case', '').strip())
76
+ df_cases = pd.read_csv('customer_cases.csv')
77
+ case_data = df_cases[df_cases['case_id'] == case_id].iloc[0]
78
+
79
+ return case_data['customer_query'], case_data.to_json(), gr.update(visible=True)
80
+
81
+ def generate_case_reply(case_data_json):
82
+ """Generates an email reply using the Gemini API."""
83
+ if not LLM_LOADED:
84
+ return "Gemini API not loaded. Cannot generate reply."
85
+
86
+ internal_data = json.loads(case_data_json)
87
+ customer_query = internal_data.pop('customer_query')
88
+ internal_data.pop('case_id')
89
+
90
+ system_prompt = """
91
+ You are an expert AI assistant for "RecoEngine", specializing in Amazon customer support...
92
+ (Your detailed system prompt remains exactly the same)
93
+ """
94
+
95
+ user_prompt = f"### Customer Query ###\n{customer_query}\n### Internal Data ###\n{json.dumps(internal_data)}"
96
+
97
+ # --- MODIFIED: Call the Gemini API ---
98
+ try:
99
+ response = model.generate_content(f"{system_prompt}\n{user_prompt}")
100
+ generated_text = response.text
101
+ except Exception as e:
102
+ return f"Error calling Gemini API: {e}"
103
+
104
+ try:
105
+ start_index = generated_text.find('{')
106
+ end_index = generated_text.rfind('}') + 1
107
+ json_output_str = generated_text[start_index:end_index]
108
+ parsed_json = json.loads(json_output_str)
109
+
110
+ template = f"""
111
+ Subject: Regarding your recent inquiry about your order...
112
+ (Your email template remains exactly the same)
113
+ """
114
+ return template
115
+ except Exception as e:
116
+ return f"Error parsing API output: {e}\nRaw Output:\n{generated_text}"
117
+
118
+ def send_reply(case_selection):
119
+ case_id = int(case_selection.split(':')[0].replace('Case', '').strip())
120
+ df_cases = pd.read_csv('customer_cases.csv')
121
+ df_cases = df_cases[df_cases['case_id'] != case_id]
122
+ df_cases.to_csv('customer_cases.csv', index=False)
123
+ return "Reply sent, case closed.", "", "", gr.update(choices=get_open_cases(), value=None), gr.update(visible=False)
124
+
125
+ # === USE CASE 3: Automated Prep Center Communication ===
126
+ def generate_prep_center_email(client_name, shipment_id, status, arrival_date, num_cartons, labels_provided):
127
+ if not LLM_LOADED:
128
+ return "Gemini API not loaded. Cannot generate email."
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
+
132
+ system_prompt = """
133
+ You are an expert AI operations assistant...
134
+ (Your detailed system prompt remains exactly the same)
135
+ """
136
+
137
+ user_prompt = f"Analyze this data and generate the JSON: {json.dumps(shipment_data)}"
138
+
139
+ # --- MODIFIED: Call the Gemini API ---
140
+ try:
141
+ response = model.generate_content(f"{system_prompt}\n{user_prompt}")
142
+ generated_text = response.text
143
+ except Exception as e:
144
+ return f"Error calling Gemini API: {e}"
145
+
146
+ try:
147
+ start_index = generated_text.find('{')
148
+ end_index = generated_text.rfind('}') + 1
149
+ json_output_str = generated_text[start_index:end_index]
150
+ parsed_json = json.loads(json_output_str)
151
+
152
+ template = f"""
153
+ Subject: {parsed_json.get('urgency_tag')} - Action Required for Your Shipment...
154
+ (Your email template remains exactly the same)
155
+ """
156
+ return template
157
+ except Exception as e:
158
+ return f"Error parsing API output: {e}\nRaw Output:\n{generated_text}"
159
+
160
+ # ... (The rest of your code for 'recommend_best_warehouse' and the Gradio UI remains exactly the same) ...
161
+
162
+ # --- 4. GRADIO UI CONSTRUCTION ---
163
+ with gr.Blocks(theme=gr.themes.Soft(text_size='lg'), title="RecoEngine Demo") as demo:
164
+ gr.Markdown("# RecoEngine Demo!!")
165
+ # ... (Your entire Gradio UI layout remains the same) ...
166
+
167
+ # --- 5. LAUNCH THE APP ---
168
+ if __name__ == "__main__":
169
+ demo.launch()
dummy_regional_orders.csv ADDED
The diff for this file is too large to render. See raw diff
 
dummy_sales_history.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ prophet
4
+ google-generative-ai
5
+ scikit-learn