syeda-Rija20 commited on
Commit
4cfa903
·
verified ·
1 Parent(s): 778f1fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tensorflow.keras.preprocessing import image
5
+ import pickle
6
+ import requests
7
+ import io
8
+
9
+ # ---------------------------
10
+ # PAGE CONFIG
11
+ # ---------------------------
12
+ st.set_page_config(page_title="TruthGuard AI", layout="wide")
13
+
14
+ st.title("🛡️ TruthGuard AI")
15
+ st.subheader("Multi-Modal Fake News & AI Image Detection")
16
+
17
+ # ---------------------------
18
+ # LOAD MODELS
19
+ # ---------------------------
20
+
21
+ # Fake News Model (load from Hugging Face)
22
+ @st.cache_resource
23
+ def load_text_model():
24
+ url = "https://huggingface.co/Maheentouqeer1/truthguard-fake-news-detector/resolve/main/fake_news_model.pkl"
25
+ vectorizer_url = "https://huggingface.co/Maheentouqeer1/truthguard-fake-news-detector/resolve/main/tfidf_vectorizer.pkl"
26
+
27
+ model_file = requests.get(url).content
28
+ vectorizer_file = requests.get(vectorizer_url).content
29
+
30
+ model = pickle.loads(model_file)
31
+ vectorizer = pickle.loads(vectorizer_file)
32
+
33
+ return model, vectorizer
34
+
35
+ # Image Model
36
+ @st.cache_resource
37
+ def load_image_model():
38
+ url = "https://huggingface.co/syeda-Rija20/image-detector/resolve/main/image_detector_finetuned.h5"
39
+ model_file = requests.get(url).content
40
+
41
+ with open("temp_model.h5", "wb") as f:
42
+ f.write(model_file)
43
+
44
+ model = tf.keras.models.load_model("temp_model.h5")
45
+ return model
46
+
47
+
48
+ text_model, vectorizer = load_text_model()
49
+ image_model = load_image_model()
50
+
51
+ # ---------------------------
52
+ # TEXT CLEANING FUNCTION
53
+ # ---------------------------
54
+ import re
55
+ import string
56
+
57
+ def clean_text(text):
58
+ text = text.lower()
59
+ text = re.sub(r'http\S+', '', text)
60
+ text = text.translate(str.maketrans('', '', string.punctuation))
61
+ text = re.sub(r'\d+', '', text)
62
+ return text
63
+
64
+ # ---------------------------
65
+ # TABS UI
66
+ # ---------------------------
67
+ tab1, tab2 = st.tabs(["📰 Fake News Detection", "🖼️ AI Image Detection"])
68
+
69
+ # ===========================
70
+ # TAB 1 → FAKE NEWS
71
+ # ===========================
72
+ with tab1:
73
+ st.header("Fake News Detector")
74
+
75
+ user_input = st.text_area("Enter news text:")
76
+
77
+ if st.button("Analyze News"):
78
+ if user_input.strip() == "":
79
+ st.warning("Please enter some text")
80
+ else:
81
+ clean = clean_text(user_input)
82
+ vector = vectorizer.transform([clean])
83
+
84
+ prediction = text_model.predict(vector)[0]
85
+ prob = text_model.predict_proba(vector)[0]
86
+
87
+ fake_prob = prob[0] * 100
88
+ real_prob = prob[1] * 100
89
+
90
+ if prediction == 0:
91
+ st.error(f"⚠️ FAKE NEWS ({fake_prob:.2f}%)")
92
+ else:
93
+ st.success(f"✅ REAL NEWS ({real_prob:.2f}%)")
94
+
95
+ st.write("### Confidence Scores")
96
+ st.write(f"Fake: {fake_prob:.2f}%")
97
+ st.write(f"Real: {real_prob:.2f}%")
98
+
99
+ # ===========================
100
+ # TAB 2 → IMAGE DETECTOR
101
+ # ===========================
102
+ with tab2:
103
+ st.header("AI Image Detector")
104
+
105
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png"])
106
+
107
+ if uploaded_file is not None:
108
+ img = image.load_img(uploaded_file, target_size=(224, 224))
109
+ img_array = image.img_to_array(img) / 255.0
110
+ img_array = np.expand_dims(img_array, axis=0)
111
+
112
+ prediction = image_model.predict(img_array)
113
+
114
+ confidence = float(prediction[0][0]) * 100
115
+
116
+ if prediction[0][0] > 0.5:
117
+ st.error(f"⚠️ AI GENERATED IMAGE ({confidence:.2f}%)")
118
+ else:
119
+ st.success(f"✅ REAL IMAGE ({100-confidence:.2f}%)")
120
+
121
+ st.write("### Confidence Score")
122
+ st.write(f"{confidence:.2f}%")