SahilSha commited on
Commit
06965ee
·
verified ·
1 Parent(s): d9bdefa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import ViTForImageClassification, AutoFeatureExtractor
4
+ from PIL import Image
5
+
6
+ # Load Model
7
+ model = ViTForImageClassification.from_pretrained("SahilSha/SahAi")
8
+ model.eval()
9
+
10
+ # Load Feature Extractor
11
+ feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
12
+
13
+ # Prediction Function
14
+ def predict(image):
15
+ image = Image.fromarray(image).convert("RGB")
16
+ inputs = feature_extractor(images=image, return_tensors="pt")
17
+
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ prediction = outputs.logits.argmax(-1).item()
21
+
22
+ confidence = torch.softmax(outputs.logits, dim=1).max().item() * 100
23
+ result = "✅ Real" if prediction == 0 else "❌ Fake"
24
+
25
+ return f"{result} (Confidence: {confidence:.2f}%)"
26
+
27
+ # Gradio Interface
28
+ iface = gr.Interface(
29
+ fn=predict,
30
+ inputs=gr.Image(type="numpy"),
31
+ outputs="text",
32
+ title="SahAi - Fake Image Detection",
33
+ description="Upload an image to check if it's Real or Fake using the Vision Transformer model."
34
+ )
35
+
36
+ iface.launch()