Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,11 @@ import shutil
|
|
| 4 |
from pathlib import Path
|
| 5 |
import pandas as pd
|
| 6 |
from openpyxl import load_workbook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
"""
|
| 9 |
Real Estate Financial Model Pipeline
|
|
@@ -1600,6 +1605,71 @@ class RealEstateModelPipeline:
|
|
| 1600 |
|
| 1601 |
return output_excel
|
| 1602 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1603 |
|
| 1604 |
if __name__ == "__main__":
|
| 1605 |
|
|
@@ -1715,4 +1785,11 @@ if __name__ == "__main__":
|
|
| 1715 |
""")
|
| 1716 |
|
| 1717 |
# Launch the app
|
| 1718 |
-
demo.launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
import pandas as pd
|
| 6 |
from openpyxl import load_workbook
|
| 7 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 8 |
+
from fastapi.responses import FileResponse
|
| 9 |
+
from typing import List
|
| 10 |
+
import uvicorn
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
"""
|
| 14 |
Real Estate Financial Model Pipeline
|
|
|
|
| 1605 |
|
| 1606 |
return output_excel
|
| 1607 |
|
| 1608 |
+
# ============= FastAPI Setup =============
|
| 1609 |
+
app = FastAPI(title="Real Estate Financial Model API")
|
| 1610 |
+
|
| 1611 |
+
# Hardcoded API Key
|
| 1612 |
+
GEMINI_API_KEY = "AIzaSyCy6GoBR724Hj9VyuW3hKM4N0P6liBOlDo"
|
| 1613 |
+
|
| 1614 |
+
@app.post("/api/generate-model")
|
| 1615 |
+
async def generate_model(files: List[UploadFile] = File(...)):
|
| 1616 |
+
"""
|
| 1617 |
+
API endpoint to process uploaded files and return Excel model
|
| 1618 |
+
|
| 1619 |
+
Parameters:
|
| 1620 |
+
- files: List of PDF/XLSX files to process
|
| 1621 |
+
|
| 1622 |
+
Returns:
|
| 1623 |
+
- Excel file with financial model
|
| 1624 |
+
"""
|
| 1625 |
+
if not files:
|
| 1626 |
+
raise HTTPException(status_code=400, detail="No files uploaded")
|
| 1627 |
+
|
| 1628 |
+
try:
|
| 1629 |
+
# Create temporary directory
|
| 1630 |
+
temp_dir = tempfile.mkdtemp()
|
| 1631 |
+
|
| 1632 |
+
# Save uploaded files
|
| 1633 |
+
saved_files = []
|
| 1634 |
+
for upload_file in files:
|
| 1635 |
+
file_path = Path(temp_dir) / upload_file.filename
|
| 1636 |
+
|
| 1637 |
+
# Write file content
|
| 1638 |
+
with open(file_path, "wb") as f:
|
| 1639 |
+
content = await upload_file.read()
|
| 1640 |
+
f.write(content)
|
| 1641 |
+
|
| 1642 |
+
saved_files.append(str(file_path))
|
| 1643 |
+
|
| 1644 |
+
# Initialize pipeline
|
| 1645 |
+
pipeline = RealEstateModelPipeline(GEMINI_API_KEY)
|
| 1646 |
+
|
| 1647 |
+
# Create output file
|
| 1648 |
+
output_file = Path(temp_dir) / "Real_Estate_Financial_Model.xlsx"
|
| 1649 |
+
|
| 1650 |
+
# Run pipeline
|
| 1651 |
+
pipeline.run_full_pipeline(temp_dir, str(output_file))
|
| 1652 |
+
|
| 1653 |
+
# Return Excel file
|
| 1654 |
+
return FileResponse(
|
| 1655 |
+
path=str(output_file),
|
| 1656 |
+
filename="Real_Estate_Financial_Model.xlsx",
|
| 1657 |
+
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 1658 |
+
)
|
| 1659 |
+
|
| 1660 |
+
except Exception as e:
|
| 1661 |
+
raise HTTPException(status_code=500, detail=f"Processing error: {str(e)}")
|
| 1662 |
+
|
| 1663 |
+
finally:
|
| 1664 |
+
# Cleanup will happen when temp directory is garbage collected
|
| 1665 |
+
pass
|
| 1666 |
+
|
| 1667 |
+
|
| 1668 |
+
@app.get("/api/health")
|
| 1669 |
+
async def health_check():
|
| 1670 |
+
"""Health check endpoint"""
|
| 1671 |
+
return {"status": "healthy", "service": "Real Estate Financial Model API"}
|
| 1672 |
+
|
| 1673 |
|
| 1674 |
if __name__ == "__main__":
|
| 1675 |
|
|
|
|
| 1785 |
""")
|
| 1786 |
|
| 1787 |
# Launch the app
|
| 1788 |
+
# demo.launch(share=False)
|
| 1789 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 1790 |
+
uvicorn.run(
|
| 1791 |
+
app,
|
| 1792 |
+
host="0.0.0.0",
|
| 1793 |
+
port=7860,
|
| 1794 |
+
log_level="info"
|
| 1795 |
+
)
|