Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,37 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from google import genai
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
# 1. Custom CSS for Visual Trajectory Badges
|
| 6 |
-
custom_css = """
|
| 7 |
-
.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;}
|
| 8 |
-
.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;}
|
| 9 |
-
.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;}
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
def ground_truth_engine(past_img, present_img, audit_level, progress=gr.Progress()):
|
| 13 |
-
progress(0, desc="Waking Spatial Sentinel...")
|
| 14 |
-
|
| 15 |
-
api_key = os.environ.get("GOOGLE_API_KEY")
|
| 16 |
-
if not api_key:
|
| 17 |
-
return None, "<div class='declining'>ERROR: API Key Missing</div>", "Set GOOGLE_API_KEY in Secrets."
|
| 18 |
-
|
| 19 |
-
client = genai.Client(api_key=api_key)
|
| 20 |
-
if past_img is None or present_img is None:
|
| 21 |
-
return None, "<div class='stable'>Missing Images</div>", "Please upload both images."
|
| 22 |
-
|
| 23 |
-
audit_directives = {
|
| 24 |
-
"Standard Audit": "Perform a structural comparison of roofing, siding, and landscaping.",
|
| 25 |
-
"Deep Forensic": """Perform an exhaustive spatial audit.
|
| 26 |
-
Identify subtle wear (shingles, foundation, paint) and biological encroachment.
|
| 27 |
-
MANDATORY: Return center points for top 3 changes as [y, x] normalized coordinates (0-1000)."""
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
directive = audit_directives.get(audit_level)
|
| 31 |
-
progress(0.2, desc=f"Running {audit_level}...")
|
| 32 |
-
|
| 33 |
-
prompt = f"""
|
| 34 |
-
SYSTEM INSTRUCTION: You are a structural forensic agent.
|
| 35 |
-
DIRECTIVE: {directive}
|
| 36 |
-
|
| 37 |
-
TASK: Compare 'Past Condition' vs 'Current Condition'.
|
| 38 |
-
Return a structured report. YOU MUST CONCLUDE with exactly one of these phrases:
|
| 39 |
-
'Maintenance Trajectory: IMPROVING', 'Maintenance Trajectory: STABLE', or 'Maintenance Trajectory: DECLINING'.
|
| 40 |
-
"""
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
progress(0.5, desc="Robotics-ER is scanning for spatial anomalies...")
|
| 44 |
-
|
| 45 |
-
response = client.models.generate_content(
|
| 46 |
-
model="gemini-robotics-er-1.5-preview",
|
| 47 |
-
contents=[prompt, past_img, present_img]
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
text_out = response.text
|
| 51 |
-
|
| 52 |
-
# 2. Logic to trigger the correct CSS Badge
|
| 53 |
-
badge_html = "<div class='stable'>➖ Trajectory: STABLE</div>" # Default fallback
|
| 54 |
-
if "IMPROVING" in text_out.upper():
|
| 55 |
-
badge_html = "<div class='improving'>📈 Trajectory: IMPROVING</div>"
|
| 56 |
-
elif "DECLINING" in text_out.upper():
|
| 57 |
-
badge_html = "<div class='declining'>📉 Trajectory: DECLINING</div>"
|
| 58 |
-
|
| 59 |
-
progress(1.0, desc="Audit Finalized.")
|
| 60 |
-
|
| 61 |
-
# 3. Return a tuple of images to automatically populate the Slider
|
| 62 |
-
return (past_img, present_img), badge_html, text_out
|
| 63 |
-
|
| 64 |
-
except Exception as e:
|
| 65 |
-
return None, "<div class='declining'>Analysis Failed</div>", str(e)
|
| 66 |
-
|
| 67 |
# Inject the CSS block into the main Gradio application
|
| 68 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 69 |
gr.Markdown("# 🏠 GroundTruth AI: Spatial Property Sentinel")
|
| 70 |
|
|
|
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
-
# Left Side: The "Agent Console"
|
| 73 |
with gr.Column(scale=1):
|
| 74 |
p_img = gr.Image(label="Past Condition", type="pil")
|
|
|
|
| 75 |
c_img = gr.Image(label="Current Condition", type="pil")
|
|
|
|
| 76 |
audit_mode = gr.Radio(
|
| 77 |
choices=["Standard Audit", "Deep Forensic"],
|
| 78 |
value="Standard Audit",
|
| 79 |
label="Select Audit Precision"
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
with gr.Column(scale=1):
|
| 85 |
-
|
| 86 |
status_badge = gr.HTML()
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
slider_out = gr.ImageSlider(label="Spatial Delta Comparison", type="pil")
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Accordion("View Detailed Forensic Data", open=False):
|
| 94 |
report_out = gr.Markdown()
|
| 95 |
|
|
@@ -98,7 +40,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
| 98 |
fn=ground_truth_engine,
|
| 99 |
inputs=[p_img, c_img, audit_mode],
|
| 100 |
outputs=[slider_out, status_badge, report_out],
|
| 101 |
-
show_progress="
|
| 102 |
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Inject the CSS block into the main Gradio application
|
| 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")
|
| 10 |
+
with gr.Column(scale=1):
|
| 11 |
c_img = gr.Image(label="Current Condition", type="pil")
|
| 12 |
+
with gr.Column(scale=1):
|
| 13 |
audit_mode = gr.Radio(
|
| 14 |
choices=["Standard Audit", "Deep Forensic"],
|
| 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 |
|
|
|
|
| 40 |
fn=ground_truth_engine,
|
| 41 |
inputs=[p_img, c_img, audit_mode],
|
| 42 |
outputs=[slider_out, status_badge, report_out],
|
| 43 |
+
show_progress="minimal"
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|