Spaces:
Sleeping
Sleeping
Sahil Garg
commited on
Commit
·
b9d7594
1
Parent(s):
233cb63
api errors solved
Browse files- .gitignore +3 -0
- Dockerfile +6 -13
- app/api.py +24 -15
- docker-compose.yml +4 -16
.gitignore
CHANGED
|
@@ -16,6 +16,9 @@ __pycache__/
|
|
| 16 |
input/
|
| 17 |
output*/
|
| 18 |
csv_notes_pnl/
|
|
|
|
|
|
|
|
|
|
| 19 |
generated_notes*/
|
| 20 |
balancesheet_excel/
|
| 21 |
cashflow_excel/
|
|
|
|
| 16 |
input/
|
| 17 |
output*/
|
| 18 |
csv_notes_pnl/
|
| 19 |
+
csv_notes_bs/
|
| 20 |
+
clean_financial_data_bs.json
|
| 21 |
+
clean_financial_data_pnl.json
|
| 22 |
generated_notes*/
|
| 23 |
balancesheet_excel/
|
| 24 |
cashflow_excel/
|
Dockerfile
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# Use Python 3.11 as base image
|
| 2 |
FROM python:3.11-slim
|
| 3 |
|
| 4 |
# -------------------------------
|
|
@@ -14,15 +14,12 @@ RUN apt-get update && apt-get install -y \
|
|
| 14 |
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
| 16 |
# -------------------------------
|
| 17 |
-
# Copy
|
| 18 |
COPY requirements.txt .
|
| 19 |
-
|
| 20 |
-
# -------------------------------
|
| 21 |
-
# Install Python dependencies
|
| 22 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 23 |
|
| 24 |
# -------------------------------
|
| 25 |
-
# Create necessary directories
|
| 26 |
RUN mkdir -p /app/input \
|
| 27 |
/app/output1 \
|
| 28 |
/app/generated_notes \
|
|
@@ -35,19 +32,15 @@ RUN mkdir -p /app/input \
|
|
| 35 |
/app/cashflow_excel \
|
| 36 |
&& chmod -R 777 /app/input /app/output1 /app/generated_notes /app/output2 /app/output3 /app/csv_notes_bs /app/csv_notes_pnl /app/balancesheet_excel /app/pnl_excel /app/cashflow_excel
|
| 37 |
|
| 38 |
-
# -------------------------------
|
| 39 |
-
# Copy the application code
|
| 40 |
-
COPY . /app/
|
| 41 |
-
|
| 42 |
# -------------------------------
|
| 43 |
# Set environment variables
|
| 44 |
ENV PYTHONPATH=/app
|
| 45 |
ENV PYTHONUNBUFFERED=1
|
| 46 |
|
| 47 |
# -------------------------------
|
| 48 |
-
# Expose the port
|
| 49 |
EXPOSE 8000
|
| 50 |
|
| 51 |
# -------------------------------
|
| 52 |
-
#
|
| 53 |
-
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
|
|
| 1 |
+
# Use Python 3.11 as base image
|
| 2 |
FROM python:3.11-slim
|
| 3 |
|
| 4 |
# -------------------------------
|
|
|
|
| 14 |
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
| 16 |
# -------------------------------
|
| 17 |
+
# Copy and install Python dependencies
|
| 18 |
COPY requirements.txt .
|
|
|
|
|
|
|
|
|
|
| 19 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
|
| 21 |
# -------------------------------
|
| 22 |
+
# Optional: Create necessary directories if not bind-mounted
|
| 23 |
RUN mkdir -p /app/input \
|
| 24 |
/app/output1 \
|
| 25 |
/app/generated_notes \
|
|
|
|
| 32 |
/app/cashflow_excel \
|
| 33 |
&& chmod -R 777 /app/input /app/output1 /app/generated_notes /app/output2 /app/output3 /app/csv_notes_bs /app/csv_notes_pnl /app/balancesheet_excel /app/pnl_excel /app/cashflow_excel
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# -------------------------------
|
| 36 |
# Set environment variables
|
| 37 |
ENV PYTHONPATH=/app
|
| 38 |
ENV PYTHONUNBUFFERED=1
|
| 39 |
|
| 40 |
# -------------------------------
|
| 41 |
+
# Expose the port
|
| 42 |
EXPOSE 8000
|
| 43 |
|
| 44 |
# -------------------------------
|
| 45 |
+
# Start FastAPI with reload enabled
|
| 46 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
app/api.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import APIRouter, UploadFile, File, Form, HTTPException
|
| 2 |
-
from fastapi.responses import JSONResponse, PlainTextResponse
|
| 3 |
from typing import Optional, Dict, Any
|
| 4 |
from app.notes import generate_notes
|
| 5 |
from app.utils import clean_value
|
|
@@ -147,44 +147,41 @@ async def llm_generate_and_excel(
|
|
| 147 |
wrapped_json_path = "generated_notes/notes_wrapped.json"
|
| 148 |
|
| 149 |
if note_number:
|
| 150 |
-
#
|
| 151 |
note_numbers = [n.strip() for n in note_number.split(",")]
|
| 152 |
all_notes = []
|
| 153 |
for n in note_numbers:
|
| 154 |
success = generator.generate_note(n, trial_balance_path=output_json)
|
| 155 |
if success:
|
| 156 |
-
# Read the just-generated note
|
| 157 |
with open("generated_notes/notes.json", "r", encoding="utf-8") as f:
|
| 158 |
note_json = json.load(f)
|
| 159 |
all_notes.append(note_json)
|
| 160 |
-
# Now write all notes together
|
| 161 |
with open("generated_notes/notes.json", "w", encoding="utf-8") as f:
|
| 162 |
json.dump({"notes": all_notes}, f, indent=2, ensure_ascii=False)
|
| 163 |
-
# --- Normalize all notes ---
|
| 164 |
wrapped = normalize_llm_notes_json({"notes": all_notes})
|
| 165 |
with open(wrapped_json_path, "w", encoding="utf-8") as f2:
|
| 166 |
json.dump(wrapped, f2, ensure_ascii=False, indent=2)
|
| 167 |
-
# --------------------------
|
| 168 |
excel_path = "generated_notes_excel/notes.xlsx"
|
| 169 |
json_to_xlsx(wrapped_json_path, excel_path)
|
| 170 |
-
return {"message": f"Notes {', '.join(note_numbers)} generated. Excel saved at {excel_path}."}
|
| 171 |
else:
|
| 172 |
-
#
|
| 173 |
results = generator.generate_all_notes(trial_balance_path=output_json)
|
| 174 |
if not any(results.values()):
|
| 175 |
logger.error("Failed to generate any notes. LLM API may be down or unreachable.")
|
| 176 |
raise HTTPException(status_code=500, detail="Failed to generate any notes. LLM API may be down or unreachable.")
|
| 177 |
-
# Read all notes.json
|
| 178 |
with open("generated_notes/notes.json", "r", encoding="utf-8") as f:
|
| 179 |
notes_json = json.load(f)
|
| 180 |
-
# --- Normalize all notes ---
|
| 181 |
wrapped = normalize_llm_notes_json(notes_json)
|
| 182 |
with open(wrapped_json_path, "w", encoding="utf-8") as f2:
|
| 183 |
json.dump(wrapped, f2, ensure_ascii=False, indent=2)
|
| 184 |
-
# --------------------------
|
| 185 |
excel_path = "generated_notes_excel/notes.xlsx"
|
| 186 |
json_to_xlsx(wrapped_json_path, excel_path)
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
|
| 190 |
|
|
@@ -264,7 +261,11 @@ async def run_full_pipeline(
|
|
| 264 |
logger.error(f"json_xlsx.json_to_xlsx failed: {e}")
|
| 265 |
raise HTTPException(status_code=500, detail=f"json_xlsx.json_to_xlsx failed: {e}")
|
| 266 |
|
| 267 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
def run_subprocess(
|
| 270 |
script_path: str,
|
|
@@ -352,7 +353,11 @@ async def bs_from_notes(file: UploadFile = File(...)):
|
|
| 352 |
raise HTTPException(status_code=500, detail=f"Could not determine output file from bl_llm.py output.{debug_msg}")
|
| 353 |
|
| 354 |
logger.info(f"Pipeline completed. Output file: {output_file_path}")
|
| 355 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
|
| 358 |
|
|
@@ -400,4 +405,8 @@ async def pnl_from_notes(file: UploadFile = File(...)):
|
|
| 400 |
raise HTTPException(status_code=500, detail=f"Could not determine output file from pnl_note.py output.{debug_msg}")
|
| 401 |
|
| 402 |
logger.info(f"Pipeline completed. Output file: {output_file_path}")
|
| 403 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, UploadFile, File, Form, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse, PlainTextResponse, FileResponse
|
| 3 |
from typing import Optional, Dict, Any
|
| 4 |
from app.notes import generate_notes
|
| 5 |
from app.utils import clean_value
|
|
|
|
| 147 |
wrapped_json_path = "generated_notes/notes_wrapped.json"
|
| 148 |
|
| 149 |
if note_number:
|
| 150 |
+
# ...existing code for note_number...
|
| 151 |
note_numbers = [n.strip() for n in note_number.split(",")]
|
| 152 |
all_notes = []
|
| 153 |
for n in note_numbers:
|
| 154 |
success = generator.generate_note(n, trial_balance_path=output_json)
|
| 155 |
if success:
|
|
|
|
| 156 |
with open("generated_notes/notes.json", "r", encoding="utf-8") as f:
|
| 157 |
note_json = json.load(f)
|
| 158 |
all_notes.append(note_json)
|
|
|
|
| 159 |
with open("generated_notes/notes.json", "w", encoding="utf-8") as f:
|
| 160 |
json.dump({"notes": all_notes}, f, indent=2, ensure_ascii=False)
|
|
|
|
| 161 |
wrapped = normalize_llm_notes_json({"notes": all_notes})
|
| 162 |
with open(wrapped_json_path, "w", encoding="utf-8") as f2:
|
| 163 |
json.dump(wrapped, f2, ensure_ascii=False, indent=2)
|
|
|
|
| 164 |
excel_path = "generated_notes_excel/notes.xlsx"
|
| 165 |
json_to_xlsx(wrapped_json_path, excel_path)
|
|
|
|
| 166 |
else:
|
| 167 |
+
# ...existing code for all notes...
|
| 168 |
results = generator.generate_all_notes(trial_balance_path=output_json)
|
| 169 |
if not any(results.values()):
|
| 170 |
logger.error("Failed to generate any notes. LLM API may be down or unreachable.")
|
| 171 |
raise HTTPException(status_code=500, detail="Failed to generate any notes. LLM API may be down or unreachable.")
|
|
|
|
| 172 |
with open("generated_notes/notes.json", "r", encoding="utf-8") as f:
|
| 173 |
notes_json = json.load(f)
|
|
|
|
| 174 |
wrapped = normalize_llm_notes_json(notes_json)
|
| 175 |
with open(wrapped_json_path, "w", encoding="utf-8") as f2:
|
| 176 |
json.dump(wrapped, f2, ensure_ascii=False, indent=2)
|
|
|
|
| 177 |
excel_path = "generated_notes_excel/notes.xlsx"
|
| 178 |
json_to_xlsx(wrapped_json_path, excel_path)
|
| 179 |
+
# Return the Excel file as a downloadable response
|
| 180 |
+
return FileResponse(
|
| 181 |
+
excel_path,
|
| 182 |
+
filename=os.path.basename(excel_path),
|
| 183 |
+
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 184 |
+
)
|
| 185 |
|
| 186 |
|
| 187 |
|
|
|
|
| 261 |
logger.error(f"json_xlsx.json_to_xlsx failed: {e}")
|
| 262 |
raise HTTPException(status_code=500, detail=f"json_xlsx.json_to_xlsx failed: {e}")
|
| 263 |
|
| 264 |
+
return FileResponse(
|
| 265 |
+
output3_xlsx,
|
| 266 |
+
filename=os.path.basename(output3_xlsx),
|
| 267 |
+
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 268 |
+
)
|
| 269 |
|
| 270 |
def run_subprocess(
|
| 271 |
script_path: str,
|
|
|
|
| 353 |
raise HTTPException(status_code=500, detail=f"Could not determine output file from bl_llm.py output.{debug_msg}")
|
| 354 |
|
| 355 |
logger.info(f"Pipeline completed. Output file: {output_file_path}")
|
| 356 |
+
return FileResponse(
|
| 357 |
+
output_file_path,
|
| 358 |
+
filename=os.path.basename(output_file_path),
|
| 359 |
+
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 360 |
+
)
|
| 361 |
|
| 362 |
|
| 363 |
|
|
|
|
| 405 |
raise HTTPException(status_code=500, detail=f"Could not determine output file from pnl_note.py output.{debug_msg}")
|
| 406 |
|
| 407 |
logger.info(f"Pipeline completed. Output file: {output_file_path}")
|
| 408 |
+
return FileResponse(
|
| 409 |
+
output_file_path,
|
| 410 |
+
filename=os.path.basename(output_file_path),
|
| 411 |
+
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 412 |
+
)
|
docker-compose.yml
CHANGED
|
@@ -1,23 +1,11 @@
|
|
| 1 |
-
version: '3.9'
|
| 2 |
-
|
| 3 |
services:
|
| 4 |
-
|
| 5 |
build: .
|
| 6 |
-
container_name:
|
|
|
|
|
|
|
| 7 |
ports:
|
| 8 |
- "8000:8000"
|
| 9 |
-
volumes:
|
| 10 |
-
- ./input:/app/input
|
| 11 |
-
- ./output1:/app/output1
|
| 12 |
-
- ./output2:/app/output2
|
| 13 |
-
- ./output3:/app/output3
|
| 14 |
-
- ./generated_notes:/app/generated_notes
|
| 15 |
-
- ./csv_notes_bs:/app/csv_notes_bs
|
| 16 |
-
- ./csv_notes_pnl:/app/csv_notes_pnl
|
| 17 |
-
- ./balancesheet_excel:/app/balancesheet_excel
|
| 18 |
-
- ./pnl_excel:/app/pnl_excel
|
| 19 |
-
- ./cashflow_excel:/app/cashflow_excel
|
| 20 |
-
- ./.env:/app/.env
|
| 21 |
environment:
|
| 22 |
- PYTHONUNBUFFERED=1
|
| 23 |
- PORT=8000
|
|
|
|
|
|
|
|
|
|
| 1 |
services:
|
| 2 |
+
app:
|
| 3 |
build: .
|
| 4 |
+
container_name: Financial-Statements
|
| 5 |
+
volumes:
|
| 6 |
+
- .:/app
|
| 7 |
ports:
|
| 8 |
- "8000:8000"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
environment:
|
| 10 |
- PYTHONUNBUFFERED=1
|
| 11 |
- PORT=8000
|