pravjet commited on
Commit
9254e9e
·
verified ·
1 Parent(s): 9282024

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +53 -54
streamlit_app.py CHANGED
@@ -43,68 +43,68 @@ st.sidebar.markdown("### About\nThis tool uses pretrained models to detect text
43
  if module == "Text Misinformation":
44
  st.title("📰 Misinformation Detection")
45
 
46
- user_input = st.text_area("Enter a news statement or claim:", height=150)
47
-
48
- if st.button("Analyze Text"):
49
- if not user_input.strip():
50
- st.warning("Please enter some text to analyze.")
51
- else:
52
- model = load_text_model()
53
- with st.spinner("Running model inference..."):
54
- result = model(user_input, truncation=True)[0]
55
-
56
- label = result.get("label", "N/A")
57
- score = float(result.get("score", 0.0))
58
-
59
- # Map labels to human-readable format
60
- label_map = {"LABEL_0": "REAL", "LABEL_1": "FAKE", "REAL": "REAL", "FAKE": "FAKE"}
61
- readable_label = label_map.get(label, label)
62
-
63
- st.success(f"Prediction: {readable_label}")
64
- st.metric("Confidence", f"{score*100:.2f}%")
65
-
66
- # Show raw model output for debugging
67
- with st.expander("🔍 Raw Model Output"):
68
- st.json(result)
69
-
70
- st.session_state.setdefault("history", [])
71
- if len(st.session_state.history) < 20:
72
- st.session_state.history.append({
73
- "type": "text",
74
- "input": user_input,
75
- "label": readable_label,
76
- "score": score,
77
- "time": datetime.utcnow().isoformat()
78
- })
79
 
80
  # ------------------- Image Deepfake -------------------
81
  elif module == "Image Deepfake":
82
  st.title("🖼 Deepfake Image Detection")
83
 
84
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
85
 
86
- if uploaded_file:
87
- image = Image.open(uploaded_file).convert("RGB")
88
- st.image(image, caption="Uploaded Image", use_container_width=True)
89
 
90
- if st.button("Analyze Image"):
91
- model = load_image_model()
92
- with st.spinner("Running image model..."):
93
- results = model(image, top_k=3)
94
 
95
- df = pd.DataFrame([{"label": r.get("label"), "score": r.get("score")} for r in results])
96
- st.dataframe(df)
97
 
98
- best = results[0]
99
- st.session_state.setdefault("history", [])
100
- if len(st.session_state.history) < 20:
101
- st.session_state.history.append({
102
- "type": "image",
103
- "filename": getattr(uploaded_file, "name", "upload"),
104
- "label": best.get("label"),
105
- "score": float(best.get("score", 0.0)),
106
- "time": datetime.utcnow().isoformat()
107
- })
108
 
109
  # ------------------- History -------------------
110
  if "history" in st.session_state and st.session_state.history:
@@ -113,4 +113,3 @@ if "history" in st.session_state and st.session_state.history:
113
  st.dataframe(df_hist)
114
  csv = df_hist.to_csv(index=False).encode("utf-8")
115
  st.download_button("Download History (CSV)", data=csv, file_name="analysis_history.csv", mime="text/csv")
116
-
 
43
  if module == "Text Misinformation":
44
  st.title("📰 Misinformation Detection")
45
 
46
+ with st.container():
47
+ user_input = st.text_area("Enter a news statement or claim:", height=150)
48
+
49
+ if st.button("Analyze Text"):
50
+ if not user_input.strip():
51
+ st.warning("Please enter some text to analyze.")
52
+ else:
53
+ model = load_text_model()
54
+ with st.spinner("Running model inference..."):
55
+ result = model(user_input, truncation=True)[0]
56
+
57
+ label = result.get("label", "N/A")
58
+ score = float(result.get("score", 0.0))
59
+
60
+ label_map = {"LABEL_0": "REAL", "LABEL_1": "FAKE", "REAL": "REAL", "FAKE": "FAKE"}
61
+ readable_label = label_map.get(label, label)
62
+
63
+ st.success(f"Prediction: {readable_label}")
64
+ st.metric("Confidence", f"{score*100:.2f}%")
65
+
66
+ with st.expander("🔍 Raw Model Output"):
67
+ st.json(result)
68
+
69
+ st.session_state.setdefault("history", [])
70
+ if len(st.session_state.history) < 20:
71
+ st.session_state.history.append({
72
+ "type": "text",
73
+ "input": user_input,
74
+ "label": readable_label,
75
+ "score": score,
76
+ "time": datetime.utcnow().isoformat()
77
+ })
 
78
 
79
  # ------------------- Image Deepfake -------------------
80
  elif module == "Image Deepfake":
81
  st.title("🖼 Deepfake Image Detection")
82
 
83
+ with st.container():
84
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
85
 
86
+ if uploaded_file:
87
+ image = Image.open(uploaded_file).convert("RGB")
88
+ st.image(image, caption="Uploaded Image", use_container_width=True)
89
 
90
+ if st.button("Analyze Image"):
91
+ model = load_image_model()
92
+ with st.spinner("Running image model..."):
93
+ results = model(image, top_k=3)
94
 
95
+ df = pd.DataFrame([{"label": r.get("label"), "score": r.get("score")} for r in results])
96
+ st.dataframe(df)
97
 
98
+ best = results[0]
99
+ st.session_state.setdefault("history", [])
100
+ if len(st.session_state.history) < 20:
101
+ st.session_state.history.append({
102
+ "type": "image",
103
+ "filename": getattr(uploaded_file, "name", "upload"),
104
+ "label": best.get("label"),
105
+ "score": float(best.get("score", 0.0)),
106
+ "time": datetime.utcnow().isoformat()
107
+ })
108
 
109
  # ------------------- History -------------------
110
  if "history" in st.session_state and st.session_state.history:
 
113
  st.dataframe(df_hist)
114
  csv = df_hist.to_csv(index=False).encode("utf-8")
115
  st.download_button("Download History (CSV)", data=csv, file_name="analysis_history.csv", mime="text/csv")