abdullah291/AI-Content-Detector
0
1import streamlit as st2from essay_bot import generate_essay3from ai_detector import detect_ai_written_parts4from paraphraser import paraphrase_flagged_parts5 6# === Session State Init ===7if "essay" not in st.session_state:8 st.session_state.essay = ""9if "flagged_parts" not in st.session_state:10 st.session_state.flagged_parts = []11if "highlighted_essay" not in st.session_state:12 st.session_state.highlighted_essay = ""13if "strictness" not in st.session_state:14 st.session_state.strictness = "normal"15 16st.title("๐ EssayBot with AI Detection + Humanization")17 18# === Step 1: Get user prompt ===19prompt = st.text_input("๐ฏ Enter your essay prompt")20 21if st.button("โ๏ธ Generate Essay"):22 if prompt.strip():23 st.session_state.essay = generate_essay(prompt)24 st.session_state.highlighted_essay = st.session_state.essay25 st.session_state.flagged_parts = []26 else:27 st.warning("Please enter a valid prompt.")28 29# === Step 2: Display Essay ===30if st.session_state.essay:31 st.markdown("## โ๏ธ Your Essay:")32 st.markdown(st.session_state.highlighted_essay, unsafe_allow_html=True)33 34 # === Select Strictness Level ===35 strictness = st.selectbox("๐จ Choose Detection Strictness Level", ["lenient", "normal", "strict"])36 st.session_state.strictness = strictness # store it for consistency37 38 # === Step 3: Detect AI parts ===39 if st.button("๐ Detect AI"):40 detection = detect_ai_written_parts(41 st.session_state.essay,42 strictness=st.session_state.strictness43 )44 flagged = detection.get("flagged_parts", [])45 st.session_state.flagged_parts = flagged46 47 if flagged:48 highlighted = st.session_state.essay49 for phrase in flagged:50 if phrase in highlighted:51 highlighted = highlighted.replace(52 phrase,53 f"<span style='background-color:#860100'>{phrase}</span>"54 )55 st.session_state.highlighted_essay = highlighted56 st.success(f"Flagged {len(flagged)} phrase(s) as AI-generated.")57 else:58 st.success("โ
No AI-sounding parts detected.")59 60 st.rerun()61 62 # === Step 4: Humanize Flagged ===63 if st.session_state.flagged_parts and st.button("๐ง Humanize"):64 result = paraphrase_flagged_parts(65 st.session_state.essay,66 st.session_state.flagged_parts67 )68 originals = result.get("originals", [])69 rewrites = result.get("paraphrased", [])70 71 essay_text = st.session_state.essay72 highlighted_text = essay_text73 74 for orig, new in zip(originals, rewrites):75 essay_text = essay_text.replace(orig, new)76 highlighted_text = highlighted_text.replace(orig, f"<span style='background-color:#347d46'>{new}</span>")77 78 st.session_state.essay = essay_text79 st.session_state.highlighted_essay = highlighted_text80 st.session_state.flagged_parts = []81 82 st.success("๐งผ Essay has been humanized!")83 st.rerun()84 85 