Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageStat
|
| 3 |
from fpdf import FPDF
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# Directory temporanea sicura
|
| 7 |
-
TMP_DIR = "/tmp"
|
| 8 |
-
if not os.path.exists(TMP_DIR):
|
| 9 |
-
os.makedirs(TMP_DIR)
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
# EURISTIC AI DETECTOR
|
|
@@ -38,23 +33,31 @@ def generate_heatmap(image, score):
|
|
| 38 |
return Image.alpha_composite(base, overlay)
|
| 39 |
|
| 40 |
# -----------------------------
|
| 41 |
-
# PDF
|
| 42 |
# -----------------------------
|
| 43 |
def create_pdf(image, text):
|
| 44 |
pdf = FPDF()
|
| 45 |
pdf.add_page()
|
| 46 |
pdf.set_font("Arial", size=12)
|
| 47 |
pdf.multi_cell(0, 6, txt=text)
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
image.convert("RGB").save(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
pdf.image(temp_img_path, x=10, y=pdf.get_y()+10, w=180)
|
| 53 |
|
| 54 |
-
# Salva PDF
|
| 55 |
-
|
| 56 |
-
pdf.output(
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# -----------------------------
|
| 60 |
# FUNZIONE PRINCIPALE
|
|
@@ -77,10 +80,11 @@ def check_image(image):
|
|
| 77 |
"- Metadata EXIF presente\n"
|
| 78 |
)
|
| 79 |
reliability = "Alta"
|
|
|
|
| 80 |
output_text = f"{explanation}Score AI stimato: {score:.1f}%\nLivello affidabilità: {reliability}"
|
| 81 |
heatmap = generate_heatmap(image, score)
|
| 82 |
-
|
| 83 |
-
return heatmap, output_text,
|
| 84 |
|
| 85 |
# -----------------------------
|
| 86 |
# INTERFACCIA GRADIO
|
|
@@ -96,7 +100,7 @@ iface = gr.Interface(
|
|
| 96 |
title="💎 Image Trust Checker 2026 (Stable Version)",
|
| 97 |
description=(
|
| 98 |
"Analizza un'immagine per determinare quanto è affidabile.\n"
|
| 99 |
-
"Basato su euristiche open-source,
|
| 100 |
),
|
| 101 |
)
|
| 102 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageStat
|
| 3 |
from fpdf import FPDF
|
| 4 |
+
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
# EURISTIC AI DETECTOR
|
|
|
|
| 33 |
return Image.alpha_composite(base, overlay)
|
| 34 |
|
| 35 |
# -----------------------------
|
| 36 |
+
# PDF in-memory (funziona HF Spaces)
|
| 37 |
# -----------------------------
|
| 38 |
def create_pdf(image, text):
|
| 39 |
pdf = FPDF()
|
| 40 |
pdf.add_page()
|
| 41 |
pdf.set_font("Arial", size=12)
|
| 42 |
pdf.multi_cell(0, 6, txt=text)
|
| 43 |
+
|
| 44 |
+
# Convert image in RGB e salva in BytesIO
|
| 45 |
+
buf_img = io.BytesIO()
|
| 46 |
+
image.convert("RGB").save(buf_img, format="PNG")
|
| 47 |
+
buf_img.seek(0)
|
| 48 |
+
|
| 49 |
+
# Salva immagine nel PDF usando fpdf
|
| 50 |
+
temp_img_path = "/tmp/temp_image.png"
|
| 51 |
+
with open(temp_img_path, "wb") as f:
|
| 52 |
+
f.write(buf_img.read())
|
| 53 |
pdf.image(temp_img_path, x=10, y=pdf.get_y()+10, w=180)
|
| 54 |
|
| 55 |
+
# Salva PDF in BytesIO per Gradio
|
| 56 |
+
pdf_bytes = io.BytesIO()
|
| 57 |
+
pdf.output(pdf_bytes)
|
| 58 |
+
pdf_bytes.seek(0)
|
| 59 |
+
pdf_bytes.name = "report.pdf" # GRADIO richiede .name per scaricare
|
| 60 |
+
return pdf_bytes
|
| 61 |
|
| 62 |
# -----------------------------
|
| 63 |
# FUNZIONE PRINCIPALE
|
|
|
|
| 80 |
"- Metadata EXIF presente\n"
|
| 81 |
)
|
| 82 |
reliability = "Alta"
|
| 83 |
+
|
| 84 |
output_text = f"{explanation}Score AI stimato: {score:.1f}%\nLivello affidabilità: {reliability}"
|
| 85 |
heatmap = generate_heatmap(image, score)
|
| 86 |
+
pdf = create_pdf(image, output_text)
|
| 87 |
+
return heatmap, output_text, pdf
|
| 88 |
|
| 89 |
# -----------------------------
|
| 90 |
# INTERFACCIA GRADIO
|
|
|
|
| 100 |
title="💎 Image Trust Checker 2026 (Stable Version)",
|
| 101 |
description=(
|
| 102 |
"Analizza un'immagine per determinare quanto è affidabile.\n"
|
| 103 |
+
"Basato su euristiche open-source, heatmap e PDF compatibili con HF Spaces."
|
| 104 |
),
|
| 105 |
)
|
| 106 |
|