Spaces:
Sleeping
Sleeping
File size: 2,998 Bytes
928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 928873d ff2cc46 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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 pipeline 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()
# Allow Streamlit access
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
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: file")
print("๐ฅ Received file:", file.filename)
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
if debug_mode:
debug_log.append("โ
Image loaded and converted to RGB")
print("โ
Image loaded and converted to RGB")
# ๐ Run YOLO
boxes, arrows, vis_debug = run_yolo(image)
if debug_mode:
debug_log.append(f"๐ฆ YOLO detected {len(boxes)} boxes and {len(arrows)} arrows")
# ๐ Run OCR
for box in boxes:
box["text"] = extract_text(image, box["bbox"], debug=debug_mode)
if debug_mode:
debug_log.append(f"๐ OCR text for box {box['id']}: {box['text']}")
print(f"๐ OCR text for box {box['id']}: {box['text']}")
# ๐ Map arrows and build graph
edges = map_arrows(boxes, arrows)
if debug_mode:
debug_log.append(f"๐งญ Mapped {len(edges)} edges from arrows to boxes")
flowchart_json = build_flowchart_json(boxes, edges)
print("๐ง Flowchart JSON structure:")
print(json.dumps(flowchart_json, indent=2))
# ๐งฎ Validate and count
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}")
# ๐ Summarize
summary = summarize_flowchart(flowchart_json)
print("๐ Generated English summary:")
print(summary)
# Optional: encode vis_debug for streamlit
yolo_vis = None
if debug_mode and vis_debug:
vis_io = io.BytesIO()
vis_debug.save(vis_io, format="PNG")
vis_io.seek(0)
yolo_vis = base64.b64encode(vis_io.read()).decode("utf-8")
return JSONResponse({
"flowchart": flowchart_json,
"summary": summary,
"yolo_vis": yolo_vis, # โ
key must match what Streamlit expects
"debug": "\n".join(debug_log) if debug_mode else ""
})
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |