pariii10 commited on
Commit
75c0195
·
verified ·
1 Parent(s): 1ece950

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
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 os
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 sicuro
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
- # Salva immagine temporanea su disco
50
- temp_img_path = os.path.join(TMP_DIR, "temp_image.png")
51
- image.convert("RGB").save(temp_img_path)
 
 
 
 
 
 
52
  pdf.image(temp_img_path, x=10, y=pdf.get_y()+10, w=180)
53
 
54
- # Salva PDF finale su disco temporaneo
55
- pdf_path = os.path.join(TMP_DIR, "report.pdf")
56
- pdf.output(pdf_path)
57
- return pdf_path # Faccio tornare il path reale, Gradio File lo legge
 
 
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
- pdf_path = create_pdf(image, output_text)
83
- return heatmap, output_text, pdf_path
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, PDF e heatmap completamente compatibili con HF Spaces."
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