AdityaRatan commited on
Commit
b355f13
·
verified ·
1 Parent(s): 0eac1ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -106
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import pandas as pd
3
  import google.generativeai as genai
@@ -9,17 +10,16 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
9
  import plotly.express as px
10
  import plotly.graph_objects as go
11
  import tempfile
12
- import os
13
  from datetime import datetime
14
- from dotenv import load_dotenv
15
-
16
-
17
- # Load environment variables
18
- load_dotenv()
19
 
20
  # Configure Gemini API
21
  GEMINI_API_KEY = os.getenv("gemini_api")
22
 
 
 
 
23
  genai.configure(api_key=GEMINI_API_KEY)
24
  generation_config = {
25
  "temperature": 1,
@@ -28,14 +28,61 @@ generation_config = {
28
  "max_output_tokens": 8192,
29
  }
30
 
31
- model = genai.GenerativeModel(
32
- model_name="gemini-pro",
33
- generation_config=generation_config,
34
- )
35
-
36
  chat_model = genai.GenerativeModel("gemini-pro")
37
 
38
- # Enhanced CSS for better styling
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  CUSTOM_CSS = '''
40
  .gradio-container {
41
  max-width: 1200px !important;
@@ -175,45 +222,6 @@ input:focus, select:focus, textarea:focus {
175
  border-top: 1px solid #404040 !important;
176
  color: #888888 !important;
177
  }
178
-
179
- .tabs {
180
- background: #2d2d2d !important;
181
- border-radius: 10px !important;
182
- padding: 10px !important;
183
- }
184
-
185
- .tab-selected {
186
- background: #3498DB !important;
187
- color: white !important;
188
- }
189
-
190
- .gr-box {
191
- background: #2d2d2d !important;
192
- border: 1px solid #404040 !important;
193
- }
194
-
195
- .gr-text-input {
196
- background: #363636 !important;
197
- color: #ffffff !important;
198
- }
199
-
200
- .gr-checkbox {
201
- border-color: #404040 !important;
202
- }
203
-
204
- .gr-checkbox:checked {
205
- background-color: #3498DB !important;
206
- }
207
-
208
- .gr-button-primary {
209
- background: #3498DB !important;
210
- color: white !important;
211
- }
212
-
213
- .gr-button-secondary {
214
- background: #404040 !important;
215
- color: white !important;
216
- }
217
  '''
218
 
219
  class SupplyChainState:
@@ -224,13 +232,11 @@ class SupplyChainState:
224
  self.chat_history = []
225
  self.analysis_results = {}
226
  self.freight_predictions = []
227
-
228
- # Load the XGBoost model
229
- self.model_path = "optimized_xgboost_model.pkl"
230
  try:
231
- self.freight_model = joblib.load(self.model_path)
232
  except Exception as e:
233
- print(f"Warning: Could not load freight prediction model from {self.model_path}: {e}")
234
  self.freight_model = None
235
 
236
  def process_uploaded_data(state, sales_file, supplier_file, text_data):
@@ -267,7 +273,6 @@ def perform_demand_forecasting(state):
267
  response = model.generate_content(prompt)
268
  analysis_text = response.text
269
 
270
- # Create visualization
271
  fig = px.line(state.sales_df, title='Historical Sales Data and Forecast')
272
  fig.update_layout(
273
  template='plotly_dark',
@@ -307,7 +312,6 @@ def perform_risk_assessment(state):
307
  response = model.generate_content(prompt)
308
  analysis_text = response.text
309
 
310
- # Create risk visualization
311
  fig = px.scatter(state.supplier_df, title='Supplier Risk Assessment')
312
  fig.update_layout(
313
  template='plotly_dark',
@@ -324,10 +328,35 @@ def perform_risk_assessment(state):
324
  except Exception as e:
325
  return f"❌ Error in risk assessment: {str(e)}", None, "Assessment failed"
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  def chat_with_navigator(state, message):
328
- """Handle chat interactions with the SupplyChainAI Navigator"""
329
  try:
330
- # Prepare context from available data
331
  context = "Available data and analysis:\n"
332
  if state.sales_df is not None:
333
  context += f"- Sales data with {len(state.sales_df)} records\n"
@@ -338,7 +367,6 @@ def chat_with_navigator(state, message):
338
  if state.freight_predictions:
339
  context += f"- Recent freight predictions: {state.freight_predictions[-5:]}\n"
340
 
341
- # Add analysis results
342
  if state.analysis_results:
343
  context += "\nRecent analysis results:\n"
344
  for analysis_type, results in state.analysis_results.items():
@@ -359,38 +387,12 @@ def chat_with_navigator(state, message):
359
 
360
  response = chat_model.generate_content(prompt)
361
 
362
- state.chat_history.append(("user", message))
363
- state.chat_history.append(("assistant", response.text))
364
 
365
  return state.chat_history
366
  except Exception as e:
367
- return [(msg_type, msg) for msg_type, msg in state.chat_history] + [("assistant", f"Error: {str(e)}")]
368
-
369
- def predict_freight_cost(state, weight, line_item_value, cost_per_kg,
370
- shipment_mode, air_charter_weight, ocean_weight, truck_weight,
371
- air_charter_value, ocean_value, truck_value):
372
- """Predict freight cost using the loaded model"""
373
- if state.freight_model is None:
374
- return "Error: Freight prediction model not loaded"
375
-
376
- try:
377
- features = {
378
- 'weight (kilograms)': weight,
379
- 'line item value': line_item_value,
380
- 'cost per kilogram': cost_per_kg,
381
- 'shipment mode_Air Charter_weight': air_charter_weight if "Air" in shipment_mode else 0,
382
- 'shipment mode_Ocean_weight': ocean_weight if "Ocean" in shipment_mode else 0,
383
- 'shipment mode_Truck_weight': truck_weight if "Truck" in shipment_mode else 0,
384
- 'shipment mode_Air Charter_line_item_value': air_charter_value if "Air" in shipment_mode else 0,
385
- 'shipment mode_Ocean_line_item_value': ocean_value if "Ocean" in shipment_mode else 0,
386
- 'shipment mode_Truck_line_item_value': truck_value if "Truck" in shipment_mode else 0
387
- }
388
- input_data = pd.DataFrame([features])
389
-
390
- prediction = state.freight_model.predict(input_data)
391
- return round(float(prediction[0]), 2)
392
- except Exception as e:
393
- return f"Error making prediction: {str(e)}"
394
 
395
  def generate_pdf_report(state, analysis_options):
396
  """Generate PDF report with analysis results"""
@@ -402,7 +404,6 @@ def generate_pdf_report(state, analysis_options):
402
  styles = getSampleStyleSheet()
403
  story = []
404
 
405
- # Enhanced title style
406
  title_style = ParagraphStyle(
407
  'CustomTitle',
408
  parent=styles['Heading1'],
@@ -411,22 +412,18 @@ def generate_pdf_report(state, analysis_options):
411
  textColor=colors.HexColor('#2C3E50')
412
  )
413
 
414
- # Add title
415
  story.append(Paragraph("SupplyChainAI Navigator Report", title_style))
416
  story.append(Spacer(1, 12))
417
 
418
- # Add timestamp
419
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
420
  story.append(Paragraph(f"Generated on: {timestamp}", styles['Normal']))
421
  story.append(Spacer(1, 20))
422
 
423
- # Add executive summary
424
  story.append(Paragraph("Executive Summary", styles['Heading2']))
425
  summary_text = "This report provides a comprehensive analysis of supply chain data, including demand forecasting, risk assessment, and optimization recommendations."
426
  story.append(Paragraph(summary_text, styles['Normal']))
427
  story.append(Spacer(1, 20))
428
 
429
- # Add analysis results
430
  if state.analysis_results:
431
  for analysis_type, results in state.analysis_results.items():
432
  if analysis_type in analysis_options:
@@ -442,7 +439,6 @@ def generate_pdf_report(state, analysis_options):
442
 
443
  story.append(Spacer(1, 20))
444
 
445
- # Add freight predictions if available
446
  if state.freight_predictions:
447
  story.append(Paragraph("Recent Freight Cost Predictions", styles['Heading2']))
448
  story.append(Spacer(1, 12))
@@ -468,7 +464,6 @@ def generate_pdf_report(state, analysis_options):
468
  story.append(table)
469
  story.append(Spacer(1, 20))
470
 
471
- # Build PDF
472
  doc.build(story)
473
  return pdf_path
474
  except Exception as e:
@@ -481,7 +476,6 @@ def run_analyses(state, choices, sales_file, supplier_file, text_data):
481
  figures = []
482
  status_messages = []
483
 
484
- # Process data first
485
  process_status = process_uploaded_data(state, sales_file, supplier_file, text_data)
486
  if "Error" in process_status:
487
  return process_status, None, process_status
@@ -505,7 +499,6 @@ def run_analyses(state, choices, sales_file, supplier_file, text_data):
505
 
506
  combined_results = "\n\n".join(results)
507
  combined_status = "\n".join(status_messages)
508
-
509
  final_figure = figures[-1] if figures else None
510
 
511
  return combined_results, final_figure, combined_status
@@ -528,7 +521,6 @@ def create_interface():
528
  gr.Markdown("# 🚢 SupplyChainAI Navigator", elem_classes="app-title")
529
  gr.Markdown("### Intelligent Supply Chain Analysis & Optimization", elem_classes="app-subtitle")
530
  gr.Markdown("An AI-powered platform for comprehensive supply chain analytics", elem_classes="app-description")
531
- gr.Markdown("Created by Aditya Ratan", elem_classes="creator-info")
532
 
533
  # Main Content Tabs
534
  with gr.Tabs() as tabs:
@@ -686,7 +678,8 @@ def create_interface():
686
  chatbot = gr.Chatbot(
687
  label="Chat History",
688
  elem_classes="chat-container",
689
- height=400
 
690
  )
691
  with gr.Row():
692
  msg = gr.Textbox(
@@ -724,7 +717,6 @@ def create_interface():
724
  truck_inputs: gr.update(visible=mode=="🚛 Truck")
725
  }
726
 
727
- # Connect all components
728
  upload_button.click(
729
  fn=lambda *args: process_uploaded_data(state, *args),
730
  inputs=[sales_data_upload, supplier_data_upload, text_input_area],
@@ -770,11 +762,12 @@ def create_interface():
770
 
771
  return demo
772
 
 
 
773
  if __name__ == "__main__":
774
  demo = create_interface()
775
  demo.launch(
776
- share=True,
 
777
  debug=True
778
- )
779
-
780
- # Enhanced title
 
1
+ import os
2
  import gradio as gr
3
  import pandas as pd
4
  import google.generativeai as genai
 
10
  import plotly.express as px
11
  import plotly.graph_objects as go
12
  import tempfile
 
13
  from datetime import datetime
14
+ import numpy as np
15
+ from xgboost import XGBRegressor
 
 
 
16
 
17
  # Configure Gemini API
18
  GEMINI_API_KEY = os.getenv("gemini_api")
19
 
20
+ if not GEMINI_API_KEY:
21
+ raise ValueError("GEMINI_API_KEY environment variable not found")
22
+
23
  genai.configure(api_key=GEMINI_API_KEY)
24
  generation_config = {
25
  "temperature": 1,
 
28
  "max_output_tokens": 8192,
29
  }
30
 
31
+ model = genai.GenerativeModel("gemini-pro", generation_config=generation_config)
 
 
 
 
32
  chat_model = genai.GenerativeModel("gemini-pro")
33
 
34
+ # Create and save a simple model on first run
35
+ def create_initial_model():
36
+ n_samples = 1000
37
+ np.random.seed(42)
38
+
39
+ data = {
40
+ 'weight (kilograms)': np.random.uniform(100, 10000, n_samples),
41
+ 'line item value': np.random.uniform(1000, 1000000, n_samples),
42
+ 'cost per kilogram': np.random.uniform(1, 500, n_samples),
43
+ 'shipment mode_Air Charter_weight': np.zeros(n_samples),
44
+ 'shipment mode_Ocean_weight': np.zeros(n_samples),
45
+ 'shipment mode_Truck_weight': np.zeros(n_samples),
46
+ 'shipment mode_Air Charter_line_item_value': np.zeros(n_samples),
47
+ 'shipment mode_Ocean_line_item_value': np.zeros(n_samples),
48
+ 'shipment mode_Truck_line_item_value': np.zeros(n_samples)
49
+ }
50
+
51
+ modes = ['Air', 'Ocean', 'Truck']
52
+ for i in range(n_samples):
53
+ mode = np.random.choice(modes)
54
+ if mode == 'Air':
55
+ data['shipment mode_Air Charter_weight'][i] = data['weight (kilograms)'][i]
56
+ data['shipment mode_Air Charter_line_item_value'][i] = data['line item value'][i]
57
+ elif mode == 'Ocean':
58
+ data['shipment mode_Ocean_weight'][i] = data['weight (kilograms)'][i]
59
+ data['shipment mode_Ocean_line_item_value'][i] = data['line item value'][i]
60
+ else:
61
+ data['shipment mode_Truck_weight'][i] = data['weight (kilograms)'][i]
62
+ data['shipment mode_Truck_line_item_value'][i] = data['line item value'][i]
63
+
64
+ df = pd.DataFrame(data)
65
+ base_cost = (df['weight (kilograms)'] * df['cost per kilogram'] * 0.8 +
66
+ df['line item value'] * 0.02)
67
+
68
+ air_multiplier = 1.5
69
+ ocean_multiplier = 0.8
70
+ truck_multiplier = 1.0
71
+
72
+ freight_cost = (
73
+ base_cost * (air_multiplier * (df['shipment mode_Air Charter_weight'] > 0) +
74
+ ocean_multiplier * (df['shipment mode_Ocean_weight'] > 0) +
75
+ truck_multiplier * (df['shipment mode_Truck_weight'] > 0))
76
+ )
77
+
78
+ freight_cost = freight_cost + np.random.normal(0, freight_cost * 0.1)
79
+
80
+ model = XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=5, random_state=42)
81
+ model.fit(df, freight_cost)
82
+
83
+ return model
84
+
85
+ # Enhanced CSS styling
86
  CUSTOM_CSS = '''
87
  .gradio-container {
88
  max-width: 1200px !important;
 
222
  border-top: 1px solid #404040 !important;
223
  color: #888888 !important;
224
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  '''
226
 
227
  class SupplyChainState:
 
232
  self.chat_history = []
233
  self.analysis_results = {}
234
  self.freight_predictions = []
235
+
 
 
236
  try:
237
+ self.freight_model = create_initial_model()
238
  except Exception as e:
239
+ print(f"Warning: Could not create freight prediction model: {e}")
240
  self.freight_model = None
241
 
242
  def process_uploaded_data(state, sales_file, supplier_file, text_data):
 
273
  response = model.generate_content(prompt)
274
  analysis_text = response.text
275
 
 
276
  fig = px.line(state.sales_df, title='Historical Sales Data and Forecast')
277
  fig.update_layout(
278
  template='plotly_dark',
 
312
  response = model.generate_content(prompt)
313
  analysis_text = response.text
314
 
 
315
  fig = px.scatter(state.supplier_df, title='Supplier Risk Assessment')
316
  fig.update_layout(
317
  template='plotly_dark',
 
328
  except Exception as e:
329
  return f"❌ Error in risk assessment: {str(e)}", None, "Assessment failed"
330
 
331
+ def predict_freight_cost(state, weight, line_item_value, cost_per_kg,
332
+ shipment_mode, air_charter_weight, ocean_weight, truck_weight,
333
+ air_charter_value, ocean_value, truck_value):
334
+ """Predict freight cost using the model"""
335
+ if state.freight_model is None:
336
+ return "Error: Freight prediction model not loaded"
337
+
338
+ try:
339
+ features = {
340
+ 'weight (kilograms)': weight,
341
+ 'line item value': line_item_value,
342
+ 'cost per kilogram': cost_per_kg,
343
+ 'shipment mode_Air Charter_weight': air_charter_weight if "Air" in shipment_mode else 0,
344
+ 'shipment mode_Ocean_weight': ocean_weight if "Ocean" in shipment_mode else 0,
345
+ 'shipment mode_Truck_weight': truck_weight if "Truck" in shipment_mode else 0,
346
+ 'shipment mode_Air Charter_line_item_value': air_charter_value if "Air" in shipment_mode else 0,
347
+ 'shipment mode_Ocean_line_item_value': ocean_value if "Ocean" in shipment_mode else 0,
348
+ 'shipment mode_Truck_line_item_value': truck_value if "Truck" in shipment_mode else 0
349
+ }
350
+ input_data = pd.DataFrame([features])
351
+
352
+ prediction = state.freight_model.predict(input_data)
353
+ return round(float(prediction[0]), 2)
354
+ except Exception as e:
355
+ return f"Error making prediction: {str(e)}"
356
+
357
  def chat_with_navigator(state, message):
358
+ """Handle chat interactions"""
359
  try:
 
360
  context = "Available data and analysis:\n"
361
  if state.sales_df is not None:
362
  context += f"- Sales data with {len(state.sales_df)} records\n"
 
367
  if state.freight_predictions:
368
  context += f"- Recent freight predictions: {state.freight_predictions[-5:]}\n"
369
 
 
370
  if state.analysis_results:
371
  context += "\nRecent analysis results:\n"
372
  for analysis_type, results in state.analysis_results.items():
 
387
 
388
  response = chat_model.generate_content(prompt)
389
 
390
+ state.chat_history.append({"role": "user", "content": message})
391
+ state.chat_history.append({"role": "assistant", "content": response.text})
392
 
393
  return state.chat_history
394
  except Exception as e:
395
+ return [{"role": "assistant", "content": f"Error: {str(e)}"}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
 
397
  def generate_pdf_report(state, analysis_options):
398
  """Generate PDF report with analysis results"""
 
404
  styles = getSampleStyleSheet()
405
  story = []
406
 
 
407
  title_style = ParagraphStyle(
408
  'CustomTitle',
409
  parent=styles['Heading1'],
 
412
  textColor=colors.HexColor('#2C3E50')
413
  )
414
 
 
415
  story.append(Paragraph("SupplyChainAI Navigator Report", title_style))
416
  story.append(Spacer(1, 12))
417
 
 
418
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
419
  story.append(Paragraph(f"Generated on: {timestamp}", styles['Normal']))
420
  story.append(Spacer(1, 20))
421
 
 
422
  story.append(Paragraph("Executive Summary", styles['Heading2']))
423
  summary_text = "This report provides a comprehensive analysis of supply chain data, including demand forecasting, risk assessment, and optimization recommendations."
424
  story.append(Paragraph(summary_text, styles['Normal']))
425
  story.append(Spacer(1, 20))
426
 
 
427
  if state.analysis_results:
428
  for analysis_type, results in state.analysis_results.items():
429
  if analysis_type in analysis_options:
 
439
 
440
  story.append(Spacer(1, 20))
441
 
 
442
  if state.freight_predictions:
443
  story.append(Paragraph("Recent Freight Cost Predictions", styles['Heading2']))
444
  story.append(Spacer(1, 12))
 
464
  story.append(table)
465
  story.append(Spacer(1, 20))
466
 
 
467
  doc.build(story)
468
  return pdf_path
469
  except Exception as e:
 
476
  figures = []
477
  status_messages = []
478
 
 
479
  process_status = process_uploaded_data(state, sales_file, supplier_file, text_data)
480
  if "Error" in process_status:
481
  return process_status, None, process_status
 
499
 
500
  combined_results = "\n\n".join(results)
501
  combined_status = "\n".join(status_messages)
 
502
  final_figure = figures[-1] if figures else None
503
 
504
  return combined_results, final_figure, combined_status
 
521
  gr.Markdown("# 🚢 SupplyChainAI Navigator", elem_classes="app-title")
522
  gr.Markdown("### Intelligent Supply Chain Analysis & Optimization", elem_classes="app-subtitle")
523
  gr.Markdown("An AI-powered platform for comprehensive supply chain analytics", elem_classes="app-description")
 
524
 
525
  # Main Content Tabs
526
  with gr.Tabs() as tabs:
 
678
  chatbot = gr.Chatbot(
679
  label="Chat History",
680
  elem_classes="chat-container",
681
+ height=400,
682
+ type="messages"
683
  )
684
  with gr.Row():
685
  msg = gr.Textbox(
 
717
  truck_inputs: gr.update(visible=mode=="🚛 Truck")
718
  }
719
 
 
720
  upload_button.click(
721
  fn=lambda *args: process_uploaded_data(state, *args),
722
  inputs=[sales_data_upload, supplier_data_upload, text_input_area],
 
762
 
763
  return demo
764
 
765
+
766
+ # Update the launch parameters in __main__:
767
  if __name__ == "__main__":
768
  demo = create_interface()
769
  demo.launch(
770
+ server_name="0.0.0.0", # Add this line
771
+ server_port=7860, # Add this line
772
  debug=True
773
+ )