Spaces:
Sleeping
Sleeping
Kesheratmex
commited on
Commit
路
011a229
1
Parent(s):
f9d898d
feat(multimodal): add Generar analisis fuerte demo and GPT-OSS wrapper
Browse files- blade-inspection-demo/app.py +283 -0
blade-inspection-demo/app.py
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import json
|
| 4 |
+
import shutil
|
| 5 |
+
import cv2
|
| 6 |
+
from typing import List, Dict, Any
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Local GPT-OSS wrapper (created previously)
|
| 11 |
+
from gptoss_wrapper import GPTOSSWrapper
|
| 12 |
+
|
| 13 |
+
# Try to import ReportLab for PDF generation; fall back to plain text PDF if unavailable
|
| 14 |
+
try:
|
| 15 |
+
from reportlab.lib.pagesizes import A4
|
| 16 |
+
from reportlab.pdfgen import canvas
|
| 17 |
+
REPORTLAB_AVAILABLE = True
|
| 18 |
+
except Exception:
|
| 19 |
+
REPORTLAB_AVAILABLE = False
|
| 20 |
+
|
| 21 |
+
# Simple helper: write a PDF with the narrative and per-frame detections
|
| 22 |
+
def _write_pdf(path: str, title: str, narrative: str, frames: List[Dict[str, Any]]):
|
| 23 |
+
if REPORTLAB_AVAILABLE:
|
| 24 |
+
c = canvas.Canvas(path, pagesize=A4)
|
| 25 |
+
width, height = A4
|
| 26 |
+
margin = 40
|
| 27 |
+
y = height - margin
|
| 28 |
+
c.setFont("Helvetica-Bold", 16)
|
| 29 |
+
c.drawString(margin, y, title)
|
| 30 |
+
y -= 30
|
| 31 |
+
c.setFont("Helvetica", 11)
|
| 32 |
+
# Narrative (wrap simple)
|
| 33 |
+
for line in narrative.splitlines():
|
| 34 |
+
if y < margin + 50:
|
| 35 |
+
c.showPage()
|
| 36 |
+
y = height - margin
|
| 37 |
+
c.setFont("Helvetica", 11)
|
| 38 |
+
c.drawString(margin, y, line)
|
| 39 |
+
y -= 16
|
| 40 |
+
y -= 10
|
| 41 |
+
c.setFont("Helvetica-Bold", 12)
|
| 42 |
+
c.drawString(margin, y, "Per-frame detections:")
|
| 43 |
+
y -= 18
|
| 44 |
+
c.setFont("Helvetica", 10)
|
| 45 |
+
for f in frames:
|
| 46 |
+
if y < margin + 50:
|
| 47 |
+
c.showPage()
|
| 48 |
+
y = height - margin
|
| 49 |
+
c.setFont("Helvetica", 10)
|
| 50 |
+
header = f"Frame {f.get('frame_index')}:"
|
| 51 |
+
c.drawString(margin, y, header)
|
| 52 |
+
y -= 14
|
| 53 |
+
dets = f.get("detections", [])
|
| 54 |
+
if not dets:
|
| 55 |
+
c.drawString(margin + 12, y, "No detections")
|
| 56 |
+
y -= 12
|
| 57 |
+
else:
|
| 58 |
+
for d in dets:
|
| 59 |
+
line = f"- {d.get('label')} | conf={d.get('confidence')} | bbox={d.get('bbox')}"
|
| 60 |
+
if y < margin + 50:
|
| 61 |
+
c.showPage()
|
| 62 |
+
y = height - margin
|
| 63 |
+
c.setFont("Helvetica", 10)
|
| 64 |
+
c.drawString(margin + 12, y, line)
|
| 65 |
+
y -= 12
|
| 66 |
+
c.save()
|
| 67 |
+
else:
|
| 68 |
+
# Fallback: write a very small text-like PDF using binary write (not a real PDF viewer-friendly)
|
| 69 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 70 |
+
f.write(title + "\n\n")
|
| 71 |
+
f.write(narrative + "\n\n")
|
| 72 |
+
f.write("Per-frame detections:\n")
|
| 73 |
+
for frame in frames:
|
| 74 |
+
f.write(f"Frame {frame.get('frame_index')}:\n")
|
| 75 |
+
dets = frame.get("detections", [])
|
| 76 |
+
if not dets:
|
| 77 |
+
f.write(" No detections\n")
|
| 78 |
+
else:
|
| 79 |
+
for d in dets:
|
| 80 |
+
f.write(f" - {d}\n")
|
| 81 |
+
|
| 82 |
+
# Build a compact prompt for the GPT model from per-frame detections
|
| 83 |
+
def _build_prompt(frames: List[Dict[str, Any]]) -> str:
|
| 84 |
+
lines = []
|
| 85 |
+
lines.append("You are an expert inspection assistant for wind turbine blade images/videos.")
|
| 86 |
+
lines.append("Given per-frame detections (label, confidence, bbox), write a concise inspection report with:")
|
| 87 |
+
lines.append("- Summary of main findings")
|
| 88 |
+
lines.append("- Suggested severity (low/medium/high) when appropriate")
|
| 89 |
+
lines.append("- Recommended next steps for inspection/repair")
|
| 90 |
+
lines.append("")
|
| 91 |
+
lines.append("Frame detections follow:")
|
| 92 |
+
for f in frames:
|
| 93 |
+
fid = f.get("frame_index")
|
| 94 |
+
dets = f.get("detections", [])
|
| 95 |
+
if not dets:
|
| 96 |
+
lines.append(f"Frame {fid}: No detections")
|
| 97 |
+
else:
|
| 98 |
+
det_texts = []
|
| 99 |
+
for d in dets:
|
| 100 |
+
conf = d.get("confidence")
|
| 101 |
+
conf_s = f"{conf:.2f}" if isinstance(conf, float) else str(conf)
|
| 102 |
+
det_texts.append(f"{d.get('label')}({conf_s})")
|
| 103 |
+
lines.append(f"Frame {fid}: " + ", ".join(det_texts))
|
| 104 |
+
lines.append("")
|
| 105 |
+
lines.append("Produce the report in plain text, 6-10 short paragraphs.")
|
| 106 |
+
return "\n".join(lines)
|
| 107 |
+
|
| 108 |
+
# Minimal (safe) detector synthesizer:
|
| 109 |
+
# If YOLO model exists at repo root (../best2.pt), we try to perform simple detection on up to N frames.
|
| 110 |
+
# Otherwise we synthesize a small example so the GPT step can be exercised in the Space without heavy deps.
|
| 111 |
+
def extract_detections_from_media(media_path: str, max_frames: int = 3) -> List[Dict[str, Any]]:
|
| 112 |
+
frames = []
|
| 113 |
+
# Try to locate best2.pt one level above this folder
|
| 114 |
+
root_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "best2.pt"))
|
| 115 |
+
try:
|
| 116 |
+
if os.path.exists(root_model_path):
|
| 117 |
+
# Attempt to use YOLO if available
|
| 118 |
+
try:
|
| 119 |
+
from ultralytics import YOLO
|
| 120 |
+
model = YOLO(root_model_path)
|
| 121 |
+
ext = os.path.splitext(media_path)[1].lower()
|
| 122 |
+
if ext in [".mp4", ".mov", ".avi", ".mkv"]:
|
| 123 |
+
cap = cv2.VideoCapture(media_path)
|
| 124 |
+
idx = 0
|
| 125 |
+
grabbed = 0
|
| 126 |
+
while grabbed < max_frames:
|
| 127 |
+
ret, frame = cap.read()
|
| 128 |
+
if not ret:
|
| 129 |
+
break
|
| 130 |
+
# save frame temporarily
|
| 131 |
+
tmpf = os.path.join(tempfile.gettempdir(), f"tmp_frame_{idx}.jpg")
|
| 132 |
+
cv2.imwrite(tmpf, frame)
|
| 133 |
+
results = model.predict(source=tmpf, conf=0.25, iou=0.45)
|
| 134 |
+
dets = []
|
| 135 |
+
if results and len(results) > 0:
|
| 136 |
+
for box in results[0].boxes:
|
| 137 |
+
try:
|
| 138 |
+
cls_id = int(box.cls[0])
|
| 139 |
+
label = model.names[cls_id]
|
| 140 |
+
except Exception:
|
| 141 |
+
label = "object"
|
| 142 |
+
try:
|
| 143 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 144 |
+
except Exception:
|
| 145 |
+
x1 = y1 = x2 = y2 = 0
|
| 146 |
+
try:
|
| 147 |
+
confv = float(box.conf[0])
|
| 148 |
+
except Exception:
|
| 149 |
+
confv = None
|
| 150 |
+
dets.append({"label": label, "confidence": confv, "bbox": [x1, y1, x2, y2]})
|
| 151 |
+
frames.append({"frame_index": idx, "detections": dets})
|
| 152 |
+
idx += 1
|
| 153 |
+
grabbed += 1
|
| 154 |
+
cap.release()
|
| 155 |
+
else:
|
| 156 |
+
# Single image
|
| 157 |
+
results = model.predict(source=media_path, conf=0.25, iou=0.45)
|
| 158 |
+
dets = []
|
| 159 |
+
if results and len(results) > 0:
|
| 160 |
+
for box in results[0].boxes:
|
| 161 |
+
try:
|
| 162 |
+
cls_id = int(box.cls[0])
|
| 163 |
+
label = model.names[cls_id]
|
| 164 |
+
except Exception:
|
| 165 |
+
label = "object"
|
| 166 |
+
try:
|
| 167 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 168 |
+
except Exception:
|
| 169 |
+
x1 = y1 = x2 = y2 = 0
|
| 170 |
+
try:
|
| 171 |
+
confv = float(box.conf[0])
|
| 172 |
+
except Exception:
|
| 173 |
+
confv = None
|
| 174 |
+
dets.append({"label": label, "confidence": confv, "bbox": [x1, y1, x2, y2]})
|
| 175 |
+
frames.append({"frame_index": 0, "detections": dets})
|
| 176 |
+
return frames
|
| 177 |
+
except Exception:
|
| 178 |
+
# If any error happens with YOLO or ultralytics, fall through to synthesize
|
| 179 |
+
pass
|
| 180 |
+
except Exception:
|
| 181 |
+
pass
|
| 182 |
+
|
| 183 |
+
# Synthesize fallback detections for demo
|
| 184 |
+
ext = os.path.splitext(media_path)[1].lower()
|
| 185 |
+
if ext in [".mp4", ".mov", ".avi", ".mkv"]:
|
| 186 |
+
# create a small synthetic set
|
| 187 |
+
for i in range(max_frames):
|
| 188 |
+
if i == 0:
|
| 189 |
+
dets = [{"label": "crack", "confidence": 0.87, "bbox": [120, 80, 300, 220]},
|
| 190 |
+
{"label": "erosion", "confidence": 0.62, "bbox": [400, 200, 520, 330]}]
|
| 191 |
+
elif i == 1:
|
| 192 |
+
dets = [{"label": "crack", "confidence": 0.81, "bbox": [125, 85, 305, 225]}]
|
| 193 |
+
else:
|
| 194 |
+
dets = []
|
| 195 |
+
frames.append({"frame_index": i, "detections": dets})
|
| 196 |
+
else:
|
| 197 |
+
# single image fallback
|
| 198 |
+
frames.append({"frame_index": 0, "detections": [{"label": "crack", "confidence": 0.78, "bbox": [100, 50, 260, 210]}]})
|
| 199 |
+
return frames
|
| 200 |
+
|
| 201 |
+
# Main action triggered by the Gradio button
|
| 202 |
+
def generar_analisis_fuerte(media: str):
|
| 203 |
+
"""
|
| 204 |
+
media: filepath provided by Gradio (video or image)
|
| 205 |
+
Returns: dict with paths to generated artifacts
|
| 206 |
+
"""
|
| 207 |
+
if not media:
|
| 208 |
+
return {"status": "No media provided", "report_pdf": None, "report_md": None, "report_json": None}
|
| 209 |
+
|
| 210 |
+
tmpdir = tempfile.mkdtemp()
|
| 211 |
+
try:
|
| 212 |
+
frames = extract_detections_from_media(media)
|
| 213 |
+
prompt = _build_prompt(frames)
|
| 214 |
+
wrapper = GPTOSSWrapper(model="gpt-oss-120")
|
| 215 |
+
try:
|
| 216 |
+
narrative = wrapper.generate(prompt)
|
| 217 |
+
except Exception as e:
|
| 218 |
+
narrative = f"(GPT call failed) {e}\n\nFallback narrative:\n"
|
| 219 |
+
# simple fallback narrative constructed from frames
|
| 220 |
+
counts = {}
|
| 221 |
+
for f in frames:
|
| 222 |
+
for d in f.get("detections", []):
|
| 223 |
+
counts[d["label"]] = counts.get(d["label"], 0) + 1
|
| 224 |
+
narrative += "Detected classes: " + ", ".join([f"{k}({v})" for k, v in counts.items()]) if counts else "No detections"
|
| 225 |
+
|
| 226 |
+
# Write Markdown
|
| 227 |
+
report_md = os.path.join(tmpdir, "report.md")
|
| 228 |
+
with open(report_md, "w", encoding="utf-8") as md:
|
| 229 |
+
md.write("# Informe de inspecci贸n (Generar analisis fuerte)\n\n")
|
| 230 |
+
md.write(narrative or "Sin narrativa disponible.\n\n")
|
| 231 |
+
md.write("\n## Per-frame detections\n\n")
|
| 232 |
+
for f in frames:
|
| 233 |
+
md.write(f"- Frame {f.get('frame_index')}: ")
|
| 234 |
+
dets = f.get("detections", [])
|
| 235 |
+
if not dets:
|
| 236 |
+
md.write("No detections\n")
|
| 237 |
+
else:
|
| 238 |
+
md.write("; ".join([f\"{d['label']}({d['confidence']}) bbox={d['bbox']}\" for d in dets]) + "\n")
|
| 239 |
+
|
| 240 |
+
# Write JSON
|
| 241 |
+
report_json = os.path.join(tmpdir, "report.json")
|
| 242 |
+
with open(report_json, "w", encoding="utf-8") as jf:
|
| 243 |
+
json.dump({"narrative": narrative, "frames": frames}, jf, indent=2)
|
| 244 |
+
|
| 245 |
+
# Write PDF
|
| 246 |
+
report_pdf = os.path.join(tmpdir, "report.pdf")
|
| 247 |
+
_write_pdf(report_pdf, "Informe de inspecci贸n - Generar analisis fuerte", narrative, frames)
|
| 248 |
+
|
| 249 |
+
return {
|
| 250 |
+
"status": "done",
|
| 251 |
+
"report_pdf": report_pdf,
|
| 252 |
+
"report_md": report_md,
|
| 253 |
+
"report_json": report_json
|
| 254 |
+
}
|
| 255 |
+
except Exception as e:
|
| 256 |
+
return {"status": f"error: {e}", "report_pdf": None, "report_md": None, "report_json": None}
|
| 257 |
+
finally:
|
| 258 |
+
# do not remove tmpdir: keep outputs available for download
|
| 259 |
+
pass
|
| 260 |
+
|
| 261 |
+
# Gradio UI
|
| 262 |
+
with gr.Blocks(title="Generador de an谩lisis fuerte") as demo:
|
| 263 |
+
gr.Markdown("## Generar an谩lisis multimodal (GPT-OSS 120)\n\nSube una imagen o v铆deo y pulsa **Generar analisis fuerte** para producir un PDF con el informe AI.")
|
| 264 |
+
with gr.Row():
|
| 265 |
+
media = gr.File(label="Sube imagen o v铆deo (archivo)")
|
| 266 |
+
btn = gr.Button("Generar analisis fuerte")
|
| 267 |
+
status = gr.Textbox(label="Estado", interactive=False)
|
| 268 |
+
pdf_out = gr.File(label="Reporte PDF")
|
| 269 |
+
md_out = gr.File(label="Reporte Markdown")
|
| 270 |
+
json_out = gr.File(label="Reporte JSON")
|
| 271 |
+
|
| 272 |
+
def _on_click(file_obj):
|
| 273 |
+
if file_obj is None:
|
| 274 |
+
return {"status": "No file provided", "report_pdf": None, "report_md": None, "report_json": None}
|
| 275 |
+
# Gradio File returns dict with 'name' key on local runs
|
| 276 |
+
path = file_obj.name if hasattr(file_obj, "name") else file_obj
|
| 277 |
+
res = generar_analisis_fuerte(path)
|
| 278 |
+
return res.get("status"), (res.get("report_pdf") if res.get("report_pdf") else None), (res.get("report_md") if res.get("report_md") else None), (res.get("report_json") if res.get("report_json") else None)
|
| 279 |
+
|
| 280 |
+
btn.click(fn=_on_click, inputs=[media], outputs=[status, pdf_out, md_out, json_out])
|
| 281 |
+
|
| 282 |
+
if __name__ == "__main__":
|
| 283 |
+
demo.launch()
|