Spaces:
Sleeping
Sleeping
Upload main.py
Browse files- app/main.py +69 -0
app/main.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
# Add project root to path
|
| 6 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 7 |
+
|
| 8 |
+
from src.inference.predictor import VisionGuardPredictor
|
| 9 |
+
|
| 10 |
+
# 1. Load Model
|
| 11 |
+
model_path = "models_saved/dinov2_best.pt"
|
| 12 |
+
print(f"⏳ Loading VisionGuard AI ({model_path})...")
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
predictor = VisionGuardPredictor(model_path)
|
| 16 |
+
print("✅ System Ready.")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"❌ Error loading model: {e}")
|
| 19 |
+
sys.exit(1)
|
| 20 |
+
|
| 21 |
+
# 2. Logic
|
| 22 |
+
def analyze_image(image):
|
| 23 |
+
if image is None:
|
| 24 |
+
return None, None, "Please upload an image."
|
| 25 |
+
|
| 26 |
+
temp_path = "temp_analysis.jpg"
|
| 27 |
+
image.save(temp_path)
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Run Prediction
|
| 31 |
+
result = predictor.predict(temp_path)
|
| 32 |
+
|
| 33 |
+
summary = (
|
| 34 |
+
f"Verdict: {result['verdict']}\n"
|
| 35 |
+
f"Confidence: {result['confidence']}%"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Return: Label Dict, Heatmap Image, Summary Text
|
| 39 |
+
return result['probabilities'], result['heatmap'], summary
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return None, None, f"Error: {str(e)}"
|
| 43 |
+
|
| 44 |
+
# 3. UI
|
| 45 |
+
with gr.Blocks(title="VisionGuard AI") as demo:
|
| 46 |
+
gr.Markdown("# 🛡️ VisionGuard AI")
|
| 47 |
+
gr.Markdown("Upload an image to detect AI artifacts. The **Heatmap** shows which areas triggered the detection.")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
input_image = gr.Image(type="pil", label="Upload Source")
|
| 52 |
+
submit_btn = gr.Button("Analyze Integrity", variant="primary")
|
| 53 |
+
|
| 54 |
+
with gr.Column():
|
| 55 |
+
# Output 1: Probability
|
| 56 |
+
label_output = gr.Label(num_top_classes=2, label="Probability")
|
| 57 |
+
# Output 2: Heatmap
|
| 58 |
+
heatmap_output = gr.Image(label="Attention Heatmap (X-Ray)")
|
| 59 |
+
# Output 3: Text
|
| 60 |
+
info_output = gr.Textbox(label="Verdict")
|
| 61 |
+
|
| 62 |
+
submit_btn.click(
|
| 63 |
+
fn=analyze_image,
|
| 64 |
+
inputs=input_image,
|
| 65 |
+
outputs=[label_output, heatmap_output, info_output]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|