syeda-Rija20 commited on
Commit
614a92d
·
verified ·
1 Parent(s): b261acc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -53,6 +53,7 @@ def load_text_model():
53
  @st.cache_resource
54
  def load_image_model():
55
  import os
 
56
  model_path = "image_model.h5"
57
 
58
  if not os.path.exists(model_path):
@@ -61,11 +62,21 @@ def load_image_model():
61
  with open(model_path, "wb") as f:
62
  f.write(response.content)
63
 
64
- # Use a lightweight PyTorch MobileNetV2 instead of TensorFlow
65
- model = models.mobilenet_v2(weights=None)
66
- model.classifier[1] = nn.Linear(model.last_channel, 1)
67
- model.eval()
68
  return model
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
 
71
  # # ---------------------------
@@ -98,21 +109,21 @@ def predict_news(text, tokenizer, text_model):
98
  # This model outputs: 0 = FAKE, 1 = REAL
99
  label = text_model.config.id2label[prediction]
100
  return label, confidence
101
- # ---------------------------
102
- # PREDICT IMAGE
103
- # ---------------------------
104
- def predict_image(img, image_model):
105
- transform = transforms.Compose([
106
- transforms.Resize((224, 224)),
107
- transforms.ToTensor(),
108
- transforms.Normalize([0.485, 0.456, 0.406],
109
- [0.229, 0.224, 0.225])
110
- ])
111
- tensor = transform(img).unsqueeze(0)
112
- with torch.no_grad():
113
- output = torch.sigmoid(image_model(tensor))
114
- confidence = output.item() * 100
115
- return confidence
116
 
117
  # ---------------------------
118
  # TABS
 
53
  @st.cache_resource
54
  def load_image_model():
55
  import os
56
+ import tensorflow as tf
57
  model_path = "image_model.h5"
58
 
59
  if not os.path.exists(model_path):
 
62
  with open(model_path, "wb") as f:
63
  f.write(response.content)
64
 
65
+ model = tf.keras.models.load_model(model_path)
 
 
 
66
  return model
67
+
68
+ def predict_image(img, image_model):
69
+ import numpy as np
70
+ import tensorflow as tf
71
+ img_resized = img.resize((224, 224))
72
+ img_array = tf.keras.preprocessing.image.img_to_array(img_resized) / 255.0
73
+ img_array = np.expand_dims(img_array, axis=0)
74
+ prediction = image_model.predict(img_array)
75
+ confidence = float(prediction[0][0]) * 100
76
+ if confidence > 50:
77
+ return "AI", confidence
78
+ else:
79
+ return "REAL", 100 - confidence
80
 
81
 
82
  # # ---------------------------
 
109
  # This model outputs: 0 = FAKE, 1 = REAL
110
  label = text_model.config.id2label[prediction]
111
  return label, confidence
112
+ # # ---------------------------
113
+ # # PREDICT IMAGE
114
+ # # ---------------------------
115
+ # def predict_image(img, image_model):
116
+ # transform = transforms.Compose([
117
+ # transforms.Resize((224, 224)),
118
+ # transforms.ToTensor(),
119
+ # transforms.Normalize([0.485, 0.456, 0.406],
120
+ # [0.229, 0.224, 0.225])
121
+ # ])
122
+ # tensor = transform(img).unsqueeze(0)
123
+ # with torch.no_grad():
124
+ # output = torch.sigmoid(image_model(tensor))
125
+ # confidence = output.item() * 100
126
+ # return confidence
127
 
128
  # ---------------------------
129
  # TABS