atharvaballa commited on
Commit
bee63f3
·
1 Parent(s): 9fd0a85

Update app.py with new UI changes

Browse files
Files changed (1) hide show
  1. app.py +149 -101
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
 
3
  # ---- IMPORT BACKENDS ----
4
  from image_backend import predict_image_pil
5
- # =========================
6
 
7
 
8
  # =========================
@@ -10,124 +9,173 @@ from image_backend import predict_image_pil
10
  # =========================
11
  def analyze_image(image):
12
  if image is None:
13
- return "", "", "", None
14
 
15
  label, confidence, heatmap = predict_image_pil(image)
16
- # Use CSS classes for vertical centering with the icon appearing first
 
17
  if label == "Fake":
18
  if confidence >= 90:
19
- risk = '<div class="risk-container risk-fake"><span class="material-icons">error</span><span> High likelihood of deepfake</span></div>'
 
20
  elif confidence >= 60:
21
- risk = '<div class="risk-container risk-warning"><span class="material-icons">warning</span><span> Possibly deepfake</span></div>'
 
22
  else:
23
- risk = '<div class="risk-container risk-neutral"><span class="material-icons">help_outline</span><span> Uncertain deepfake</span></div>'
 
24
  else:
25
  if confidence >= 90:
26
- risk = '<div class="risk-container risk-real"><span class="material-icons">check_circle</span><span> Likely real</span></div>'
 
27
  elif confidence >= 60:
28
- risk = '<div class="risk-container risk-warning"><span class="material-icons">warning</span><span> Possibly real</span></div>'
 
29
  else:
30
- risk = '<div class="risk-container risk-neutral"><span class="material-icons">help_outline</span><span> Uncertain - needs review</span></div>'
 
31
 
32
- return label, f"{confidence} %", risk, heatmap
 
 
 
 
 
33
 
 
34
 
35
 
36
  # =========================
37
- # UI
38
  # =========================
39
- head = """
40
- <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  """
42
- with gr.Blocks( head = head) as demo:
43
-
44
-
45
- gr.Markdown("# AI Driven Deepfake Detection System")
46
-
47
- with gr.Tabs():
48
-
49
- # =========================
50
- # HOME TAB (RESTORED)
51
- # =========================
52
- with gr.Tab("Home"):
53
- gr.Markdown("""
54
- ## Welcome
55
-
56
- This system detects AI-generated (deepfake) content in images using
57
- transformer-based deep learning models.
58
- """)
59
-
60
- gr.Markdown("""
61
- ### Supported inputs
62
- - Images: JPG, PNG (face-centric images recommended)
63
- """)
64
-
65
- gr.Markdown("""
66
- ### How to use
67
- 1. Select a detection mode using the tabs above.
68
- 2. Upload an image or audio file.
69
- 3. Click **Submit** to start analysis.
70
- 4. Review the prediction, confidence score, and risk assessment.
71
- """)
72
-
73
- gr.Markdown("""
74
- ### Understanding the results
75
- - **Prediction**: Model decision (Real / Fake)
76
- - **Confidence**: Certainty percentage of the prediction
77
- - **Risk Assessment**:
78
- - High likelihood → strong indication
79
- - Possibly → caution advised
80
- - Uncertain → manual review recommended
81
- """)
82
-
83
- gr.Markdown("""
84
- ### Explainability
85
- For images, attention heatmaps highlight the facial regions that influenced
86
- the model’s decision, supporting transparency and forensic analysis.
87
- """)
88
-
89
- gr.Markdown("""
90
- ### Data privacy & intended use
91
- Uploaded files are processed temporarily and are not stored.
92
- This system is intended as a decision-support tool and should not be used
93
- as the sole source of verification.
94
- """)
95
-
96
- # =========================
97
- # IMAGE TAB
98
- # =========================
99
- with gr.Tab("Image Deepfake"):
100
- gr.Markdown("## Deepfake Image Detection")
101
-
102
- with gr.Row():
103
- with gr.Column(scale=1):
104
- image_input = gr.Image(
105
- label="Upload Image",
106
- type="pil",
107
- height=280
108
- )
109
- img_submit = gr.Button("Submit")
110
- img_clear = gr.Button("Clear")
111
-
112
- with gr.Column(scale=2):
113
- img_pred = gr.Text(label="Prediction")
114
- img_conf = gr.Text(label="Confidence")
115
- img_risk = gr.HTML(label="Risk Assessment", value="")
116
- img_heatmap = gr.Image(
117
- label="Explainability Heatmap",
118
- height=280
119
- )
120
-
121
- img_submit.click(
122
- analyze_image,
123
- image_input,
124
- [img_pred, img_conf, img_risk, img_heatmap]
125
  )
126
 
127
- img_clear.click(
128
- lambda: (None, "", "", "", None),
129
- None,
130
- [image_input, img_pred, img_conf, img_risk, img_heatmap]
 
 
 
 
 
 
 
 
 
 
 
131
  )
132
 
133
- demo.launch(css="style.css")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # ---- IMPORT BACKENDS ----
4
  from image_backend import predict_image_pil
 
5
 
6
 
7
  # =========================
 
9
  # =========================
10
  def analyze_image(image):
11
  if image is None:
12
+ return "", "", "", None, "Idle"
13
 
14
  label, confidence, heatmap = predict_image_pil(image)
15
+
16
+ # Risk styling
17
  if label == "Fake":
18
  if confidence >= 90:
19
+ risk = "high"
20
+ message = "High likelihood of deepfake"
21
  elif confidence >= 60:
22
+ risk = "warning"
23
+ message = "Possibly deepfake"
24
  else:
25
+ risk = "neutral"
26
+ message = "Uncertain deepfake"
27
  else:
28
  if confidence >= 90:
29
+ risk = "real"
30
+ message = "Likely real"
31
  elif confidence >= 60:
32
+ risk = "warning"
33
+ message = "Possibly real"
34
  else:
35
+ risk = "neutral"
36
+ message = "Uncertain - review needed"
37
 
38
+ risk_html = f"""
39
+ <div class="risk-card {risk}">
40
+ <div class="risk-title">{label}</div>
41
+ <div class="risk-msg">{message}</div>
42
+ </div>
43
+ """
44
 
45
+ return label, f"{confidence} %", risk_html, heatmap, "Completed"
46
 
47
 
48
  # =========================
49
+ # CUSTOM CSS (DASHBOARD STYLE)
50
  # =========================
51
+ css = """
52
+ body {
53
+ background-color: #0f172a;
54
+ }
55
+
56
+ h1 {
57
+ text-align: center;
58
+ color: white;
59
+ }
60
+
61
+ /* Header */
62
+ .header {
63
+ text-align: center;
64
+ padding: 10px;
65
+ font-size: 26px;
66
+ font-weight: bold;
67
+ color: white;
68
+ }
69
+
70
+ /* Card Layout */
71
+ .gr-box {
72
+ border-radius: 12px !important;
73
+ background: #1e293b !important;
74
+ padding: 15px !important;
75
+ }
76
+
77
+ /* Buttons */
78
+ button {
79
+ border-radius: 8px !important;
80
+ }
81
+
82
+ /* Risk Cards */
83
+ .risk-card {
84
+ padding: 15px;
85
+ border-radius: 10px;
86
+ color: white;
87
+ font-weight: bold;
88
+ }
89
+
90
+ .risk-card.real {
91
+ background: #16a34a;
92
+ }
93
+
94
+ .risk-card.high {
95
+ background: #dc2626;
96
+ }
97
+
98
+ .risk-card.warning {
99
+ background: #f59e0b;
100
+ }
101
+
102
+ .risk-card.neutral {
103
+ background: #64748b;
104
+ }
105
+
106
+ /* Status Badge */
107
+ .status {
108
+ padding: 6px 12px;
109
+ border-radius: 20px;
110
+ background: #334155;
111
+ color: white;
112
+ display: inline-block;
113
+ }
114
  """
115
+
116
+
117
+ # =========================
118
+ # UI
119
+ # =========================
120
+ with gr.Blocks(css=css) as demo:
121
+
122
+ # HEADER
123
+ gr.Markdown('<div class="header">AI Driven Deepfake Detection System</div>')
124
+
125
+ # DESCRIPTION
126
+ gr.Markdown("""
127
+ Detect whether an image is **Real or AI-generated (Deepfake)** using a ViT-based model.
128
+ """)
129
+
130
+ # STATUS BAR
131
+ status = gr.HTML('<div class="status">Status: Idle</div>')
132
+
133
+ # MAIN LAYOUT
134
+ with gr.Row():
135
+
136
+ # LEFT PANEL (INPUT)
137
+ with gr.Column(scale=1):
138
+ gr.Markdown("### Upload Image")
139
+
140
+ image_input = gr.Image(
141
+ type="pil",
142
+ height=300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  )
144
 
145
+ img_submit = gr.Button("Analyze", variant="primary")
146
+ img_clear = gr.Button("Reset")
147
+
148
+ # RIGHT PANEL (OUTPUT)
149
+ with gr.Column(scale=2):
150
+ gr.Markdown("### Analysis Results")
151
+
152
+ img_pred = gr.Text(label="Prediction")
153
+ img_conf = gr.Text(label="Confidence")
154
+
155
+ img_risk = gr.HTML()
156
+
157
+ img_heatmap = gr.Image(
158
+ label="Model Explainability",
159
+ height=300
160
  )
161
 
162
+ # =========================
163
+ # EVENTS
164
+ # =========================
165
+ def run_analysis(image):
166
+ return analyze_image(image)
167
+
168
+ img_submit.click(
169
+ run_analysis,
170
+ inputs=image_input,
171
+ outputs=[img_pred, img_conf, img_risk, img_heatmap, status]
172
+ )
173
+
174
+ img_clear.click(
175
+ lambda: (None, "", "", "", None, '<div class="status">Status: Idle</div>'),
176
+ None,
177
+ [image_input, img_pred, img_conf, img_risk, img_heatmap, status]
178
+ )
179
+
180
+
181
+ demo.launch()