Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,7 +75,7 @@ def generate_plot(planned_cost, actual_spend, forecast_cost):
|
|
| 75 |
return image_base64, gradio_image, buf_pdf
|
| 76 |
|
| 77 |
# Function to generate a PDF report locally
|
| 78 |
-
def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes,
|
| 79 |
pdf_path = f"budget_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 80 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 81 |
width, height = letter
|
|
@@ -93,7 +93,7 @@ def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights
|
|
| 93 |
y_position -= 20
|
| 94 |
c.drawString(50, y_position, f"Forecasted Cost: ${forecast_cost:,.2f}")
|
| 95 |
y_position -= 20
|
| 96 |
-
c.drawString(50, y_position, f"Forecasted Cost (
|
| 97 |
y_position -= 20
|
| 98 |
c.drawString(50, y_position, f"Total Risk: {total_risk}")
|
| 99 |
y_position -= 20
|
|
@@ -113,12 +113,12 @@ def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights
|
|
| 113 |
return pdf_path
|
| 114 |
|
| 115 |
# Function to generate an Excel file with prediction results
|
| 116 |
-
def generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes,
|
| 117 |
data = {
|
| 118 |
"Planned Cost ($)": [planned_cost],
|
| 119 |
"Actual Spend ($)": [actual_spend],
|
| 120 |
"Forecasted Cost ($)": [forecast_cost],
|
| 121 |
-
"Forecasted Cost (
|
| 122 |
"Total Risk": [total_risk],
|
| 123 |
"Insights": [insights],
|
| 124 |
"Status": [status],
|
|
@@ -205,21 +205,21 @@ def predict_risk(planned_cost, actual_spend, category, cement_index, labor_index
|
|
| 205 |
logger.error(f"Failed to save to Salesforce: {str(e)}")
|
| 206 |
return f"Error: Failed to save to Salesforce: {str(e)}", None, None, None
|
| 207 |
|
| 208 |
-
# Fetch exchange rates
|
| 209 |
try:
|
| 210 |
response = requests.get("https://api.exchangerate-api.com/v4/latest/USD")
|
| 211 |
exchange_rates = response.json()
|
| 212 |
-
|
| 213 |
-
|
| 214 |
except Exception as e:
|
| 215 |
logger.error(f"Failed to fetch exchange rates: {str(e)}")
|
| 216 |
-
|
| 217 |
|
| 218 |
# Generate local PDF report
|
| 219 |
-
pdf_file = generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes,
|
| 220 |
|
| 221 |
# Generate Excel file
|
| 222 |
-
excel_file = generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes,
|
| 223 |
|
| 224 |
# Format the output for Gradio with enhanced summary and risk percentage
|
| 225 |
risk_level = "High" if total_risk == 1 else "Low"
|
|
@@ -236,7 +236,7 @@ def predict_risk(planned_cost, actual_spend, category, cement_index, labor_index
|
|
| 236 |
f"Detailed Metrics\n"
|
| 237 |
f"----------------------------------------\n"
|
| 238 |
f"Total Risk: {total_risk}\n"
|
| 239 |
-
f"Forecasted Cost: ${forecast_cost:,.2f} (
|
| 240 |
f"Top Causes: {top_causes}\n"
|
| 241 |
f"Salesforce PDF URL (requires login): {pdf_url}\n"
|
| 242 |
f"Local PDF Report: [Download link below]\n"
|
|
|
|
| 75 |
return image_base64, gradio_image, buf_pdf
|
| 76 |
|
| 77 |
# Function to generate a PDF report locally
|
| 78 |
+
def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, chart_image):
|
| 79 |
pdf_path = f"budget_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 80 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 81 |
width, height = letter
|
|
|
|
| 93 |
y_position -= 20
|
| 94 |
c.drawString(50, y_position, f"Forecasted Cost: ${forecast_cost:,.2f}")
|
| 95 |
y_position -= 20
|
| 96 |
+
c.drawString(50, y_position, f"Forecasted Cost (INR): {forecast_cost_inr if isinstance(forecast_cost_inr, str) else f'₹{forecast_cost_inr:,.2f}'}")
|
| 97 |
y_position -= 20
|
| 98 |
c.drawString(50, y_position, f"Total Risk: {total_risk}")
|
| 99 |
y_position -= 20
|
|
|
|
| 113 |
return pdf_path
|
| 114 |
|
| 115 |
# Function to generate an Excel file with prediction results
|
| 116 |
+
def generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr):
|
| 117 |
data = {
|
| 118 |
"Planned Cost ($)": [planned_cost],
|
| 119 |
"Actual Spend ($)": [actual_spend],
|
| 120 |
"Forecasted Cost ($)": [forecast_cost],
|
| 121 |
+
"Forecasted Cost (INR)": [forecast_cost_inr if isinstance(forecast_cost_inr, str) else forecast_cost_inr],
|
| 122 |
"Total Risk": [total_risk],
|
| 123 |
"Insights": [insights],
|
| 124 |
"Status": [status],
|
|
|
|
| 205 |
logger.error(f"Failed to save to Salesforce: {str(e)}")
|
| 206 |
return f"Error: Failed to save to Salesforce: {str(e)}", None, None, None
|
| 207 |
|
| 208 |
+
# Fetch exchange rates for USD to INR
|
| 209 |
try:
|
| 210 |
response = requests.get("https://api.exchangerate-api.com/v4/latest/USD")
|
| 211 |
exchange_rates = response.json()
|
| 212 |
+
usd_to_inr = exchange_rates['rates']['INR']
|
| 213 |
+
forecast_cost_inr = forecast_cost * usd_to_inr
|
| 214 |
except Exception as e:
|
| 215 |
logger.error(f"Failed to fetch exchange rates: {str(e)}")
|
| 216 |
+
forecast_cost_inr = "N/A"
|
| 217 |
|
| 218 |
# Generate local PDF report
|
| 219 |
+
pdf_file = generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, chart_image_pdf)
|
| 220 |
|
| 221 |
# Generate Excel file
|
| 222 |
+
excel_file = generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr)
|
| 223 |
|
| 224 |
# Format the output for Gradio with enhanced summary and risk percentage
|
| 225 |
risk_level = "High" if total_risk == 1 else "Low"
|
|
|
|
| 236 |
f"Detailed Metrics\n"
|
| 237 |
f"----------------------------------------\n"
|
| 238 |
f"Total Risk: {total_risk}\n"
|
| 239 |
+
f"Forecasted Cost: ${forecast_cost:,.2f} (INR: {forecast_cost_inr if isinstance(forecast_cost_inr, str) else f'₹{forecast_cost_inr:,.2f}'})\n"
|
| 240 |
f"Top Causes: {top_causes}\n"
|
| 241 |
f"Salesforce PDF URL (requires login): {pdf_url}\n"
|
| 242 |
f"Local PDF Report: [Download link below]\n"
|