Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,104 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 3 |
gr.Markdown("# 🏠 GroundTruth AI: Spatial Property Sentinel")
|
| 4 |
|
| 5 |
# --- TOP ROW: The "Input Console" ---
|
| 6 |
-
# Breaking this into 3 columns forces the images to be smaller and sit side-by-side
|
| 7 |
with gr.Row():
|
| 8 |
with gr.Column(scale=1):
|
| 9 |
p_img = gr.Image(label="Past Condition", type="pil")
|
|
@@ -15,23 +110,17 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 15 |
value="Standard Audit",
|
| 16 |
label="Select Audit Precision"
|
| 17 |
)
|
| 18 |
-
# Added some margin-top to push the button down slightly for better alignment
|
| 19 |
submit = gr.Button("Analyze Property Trajectory", variant="primary", size="lg")
|
| 20 |
|
| 21 |
# --- MIDDLE ROW: The "Hero Output" ---
|
| 22 |
-
# This row takes up the full width of the screen to highlight the visual evidence
|
| 23 |
with gr.Row():
|
| 24 |
with gr.Column(scale=1):
|
| 25 |
-
# The CSS Badge renders immediately above the slider
|
| 26 |
status_badge = gr.HTML()
|
| 27 |
-
|
| 28 |
-
# Using height=600 ensures the slider is large and premium-feeling
|
| 29 |
slider_out = gr.ImageSlider(label="Spatial Delta Comparison", type="pil", height=600)
|
| 30 |
|
| 31 |
# --- BOTTOM ROW: The "Forensic Data" ---
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column(scale=1):
|
| 34 |
-
# The Accordion spans the bottom, keeping the technical text neatly tucked away
|
| 35 |
with gr.Accordion("View Detailed Forensic Data", open=False):
|
| 36 |
report_out = gr.Markdown()
|
| 37 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from google import genai
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
from PIL import ImageDraw
|
| 6 |
+
|
| 7 |
+
# 1. Custom CSS for Visual Trajectory Badges
|
| 8 |
+
custom_css = """
|
| 9 |
+
.improving { background-color: #dcf2dc; color: #155724; padding: 12px; border-radius: 8px; font-weight: bold; font-size: 1.2em; text-align: center; border: 1px solid #c3e6cb;}
|
| 10 |
+
.declining { background-color: #f8d7da; color: #721c24; padding: 12px; border-radius: 8px; font-weight: bold; font-size: 1.2em; text-align: center; border: 1px solid #f5c6cb;}
|
| 11 |
+
.stable { background-color: #fff3cd; color: #856404; padding: 12px; border-radius: 8px; font-weight: bold; font-size: 1.2em; text-align: center; border: 1px solid #ffeeba;}
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
def draw_forensic_targets(image, report_text):
|
| 15 |
+
"""Parses [y, x] coordinates and draws red circles on the image."""
|
| 16 |
+
annotated_img = image.copy()
|
| 17 |
+
draw = ImageDraw.Draw(annotated_img)
|
| 18 |
+
width, height = annotated_img.size
|
| 19 |
+
|
| 20 |
+
# Find all instances of [y, x] in the text
|
| 21 |
+
coords = re.findall(r'\[(\d+),\s*(\d+)\]', report_text)
|
| 22 |
+
|
| 23 |
+
for y_str, x_str in coords:
|
| 24 |
+
# Denormalize (0-1000 scale) to actual image pixels
|
| 25 |
+
y = int((int(y_str) / 1000) * height)
|
| 26 |
+
x = int((int(x_str) / 1000) * width)
|
| 27 |
+
|
| 28 |
+
# Draw a red circle around the target point
|
| 29 |
+
r = 15
|
| 30 |
+
draw.ellipse((x - r, y - r, x + r, y + r), outline="red", width=5)
|
| 31 |
+
|
| 32 |
+
return annotated_img
|
| 33 |
+
|
| 34 |
+
def ground_truth_engine(past_img, present_img, audit_level, progress=gr.Progress()):
|
| 35 |
+
progress(0, desc="Waking Spatial Sentinel...")
|
| 36 |
+
|
| 37 |
+
api_key = os.environ.get("GOOGLE_API_KEY")
|
| 38 |
+
if not api_key:
|
| 39 |
+
return None, "<div class='declining'>ERROR: API Key Missing</div>", "Set GOOGLE_API_KEY in Secrets."
|
| 40 |
+
|
| 41 |
+
client = genai.Client(api_key=api_key)
|
| 42 |
+
if past_img is None or present_img is None:
|
| 43 |
+
return None, "<div class='stable'>Missing Images</div>", "Please upload both images."
|
| 44 |
+
|
| 45 |
+
audit_directives = {
|
| 46 |
+
"Standard Audit": "Perform a structural comparison of roofing, siding, and landscaping.",
|
| 47 |
+
"Deep Forensic": """Perform an exhaustive spatial audit.
|
| 48 |
+
Identify subtle wear (shingles, foundation, paint) and biological encroachment.
|
| 49 |
+
MANDATORY: Return center points for top 3 changes as [y, x] normalized coordinates (0-1000)."""
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
directive = audit_directives.get(audit_level)
|
| 53 |
+
progress(0.2, desc=f"Running {audit_level}...")
|
| 54 |
+
|
| 55 |
+
prompt = f"""
|
| 56 |
+
SYSTEM INSTRUCTION: You are a structural forensic agent.
|
| 57 |
+
DIRECTIVE: {directive}
|
| 58 |
+
|
| 59 |
+
TASK: Compare 'Past Condition' vs 'Current Condition'.
|
| 60 |
+
Return a structured report. YOU MUST CONCLUDE with exactly one of these phrases:
|
| 61 |
+
'Maintenance Trajectory: IMPROVING', 'Maintenance Trajectory: STABLE', or 'Maintenance Trajectory: DECLINING'.
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
progress(0.5, desc="Robotics-ER is scanning for spatial anomalies...")
|
| 66 |
+
|
| 67 |
+
response = client.models.generate_content(
|
| 68 |
+
model="gemini-robotics-er-1.5-preview",
|
| 69 |
+
contents=[prompt, past_img, present_img]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
text_out = response.text
|
| 73 |
+
|
| 74 |
+
# Logic to trigger the correct CSS Badge
|
| 75 |
+
badge_html = "<div class='stable'>➖ Trajectory: STABLE</div>" # Default fallback
|
| 76 |
+
if "IMPROVING" in text_out.upper():
|
| 77 |
+
badge_html = "<div class='improving'>📈 Trajectory: IMPROVING</div>"
|
| 78 |
+
elif "DECLINING" in text_out.upper():
|
| 79 |
+
badge_html = "<div class='declining'>📉 Trajectory: DECLINING</div>"
|
| 80 |
+
|
| 81 |
+
progress(0.8, desc="Annotating image...")
|
| 82 |
+
|
| 83 |
+
# Determine which image to show on the right side of the slider
|
| 84 |
+
final_present_img = present_img
|
| 85 |
+
if "Deep Forensic" in audit_level:
|
| 86 |
+
final_present_img = draw_forensic_targets(present_img, text_out)
|
| 87 |
+
|
| 88 |
+
progress(1.0, desc="Audit Finalized.")
|
| 89 |
+
|
| 90 |
+
# Return a tuple of images to automatically populate the Slider
|
| 91 |
+
return (past_img, final_present_img), badge_html, text_out
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return None, "<div class='declining'>Analysis Failed</div>", str(e)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# --- UI LAYOUT ---
|
| 98 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 99 |
gr.Markdown("# 🏠 GroundTruth AI: Spatial Property Sentinel")
|
| 100 |
|
| 101 |
# --- TOP ROW: The "Input Console" ---
|
|
|
|
| 102 |
with gr.Row():
|
| 103 |
with gr.Column(scale=1):
|
| 104 |
p_img = gr.Image(label="Past Condition", type="pil")
|
|
|
|
| 110 |
value="Standard Audit",
|
| 111 |
label="Select Audit Precision"
|
| 112 |
)
|
|
|
|
| 113 |
submit = gr.Button("Analyze Property Trajectory", variant="primary", size="lg")
|
| 114 |
|
| 115 |
# --- MIDDLE ROW: The "Hero Output" ---
|
|
|
|
| 116 |
with gr.Row():
|
| 117 |
with gr.Column(scale=1):
|
|
|
|
| 118 |
status_badge = gr.HTML()
|
|
|
|
|
|
|
| 119 |
slider_out = gr.ImageSlider(label="Spatial Delta Comparison", type="pil", height=600)
|
| 120 |
|
| 121 |
# --- BOTTOM ROW: The "Forensic Data" ---
|
| 122 |
with gr.Row():
|
| 123 |
with gr.Column(scale=1):
|
|
|
|
| 124 |
with gr.Accordion("View Detailed Forensic Data", open=False):
|
| 125 |
report_out = gr.Markdown()
|
| 126 |
|