from fastapi import FastAPI, UploadFile, File, Form from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse import uvicorn from PIL import Image import io import json import base64 # ๐Ÿ’ก Import modules from yolo_module import run_yolo from ocr_module import extract_text, count_elements, validate_structure from graph_module import map_arrows, build_flowchart_json from summarizer_module import summarize_flowchart app = FastAPI() # ๐Ÿ”“ Enable CORS for Streamlit frontend app.add_middleware( CORSMiddleware, allow_origins=["*"], # Update with actual domain if needed allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.post("/process-image") async def process_image(file: UploadFile = File(...), debug: str = Form("false")): debug_mode = debug.lower() == "true" debug_log = [] if debug_mode: debug_log.append("๐Ÿ“ฅ Received file upload") print(f"๐Ÿ“ฅ File received: {file.filename}") # ๐Ÿ–ผ๏ธ Load image contents = await file.read() image = Image.open(io.BytesIO(contents)).convert("RGB") if debug_mode: debug_log.append("โœ… Image converted to RGB") print("โœ… Image converted to RGB") # ๐Ÿ“ฆ YOLO Detection boxes, arrows, vis_debug = run_yolo(image) if debug_mode: debug_log.append(f"๐Ÿ“ฆ Detected {len(boxes)} boxes, {len(arrows)} arrows") # ๐Ÿ” OCR for each box for box in boxes: box["text"] = extract_text(image, box["bbox"], debug=debug_mode) print(f"๐Ÿ” OCR for {box['id']}: {box['text']}") if debug_mode: debug_log.append(f"๐Ÿ” {box['id']}: {box['text']}") # โžก๏ธ Build directional edges edges = map_arrows(boxes, arrows) if debug_mode: debug_log.append(f"โžก๏ธ Mapped {len(edges)} directional edges") # ๐Ÿง  Build structured flowchart flowchart_json = build_flowchart_json(boxes, edges) print("๐Ÿง  Flowchart JSON:", json.dumps(flowchart_json, indent=2)) # โœ… Sanity checks structure_info = count_elements(boxes, arrows, debug=debug_mode) validation = validate_structure( flowchart_json, expected_boxes=structure_info["box_count"], expected_arrows=len(arrows), debug=debug_mode ) if debug_mode: debug_log.append(f"๐Ÿงพ Validation: {validation}") # โœ๏ธ Generate Summary summary = summarize_flowchart(flowchart_json) print("๐Ÿ“ Summary:", summary) # ๐Ÿ–ผ๏ธ Encode visual debug yolo_vis = None if debug_mode and vis_debug: vis_io = io.BytesIO() vis_debug.save(vis_io, format="PNG") yolo_vis = base64.b64encode(vis_io.getvalue()).decode("utf-8") return JSONResponse({ "flowchart": flowchart_json, "summary": summary, "yolo_vis": yolo_vis, "debug": "\n".join(debug_log) if debug_mode else "" }) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)