Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 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
|