Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
|
@@ -12,36 +13,14 @@ from docx import Document
|
|
| 12 |
# --- Streamlit Page Configuration ---
|
| 13 |
st.set_page_config(page_title="AI & Plagiarism Detection", page_icon="π")
|
| 14 |
|
| 15 |
-
# ---
|
| 16 |
-
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
},
|
| 24 |
-
"forest": {
|
| 25 |
-
"primaryColor": "#38761d",
|
| 26 |
-
"backgroundColor": "#228B22",
|
| 27 |
-
"tileColor": "#3D59AB",
|
| 28 |
-
"textColor": "#E0FFFF",
|
| 29 |
-
"welcomeTextColor": "#E0FFFF",
|
| 30 |
-
},
|
| 31 |
-
"beach": {
|
| 32 |
-
"primaryColor": "#F4D03F",
|
| 33 |
-
"backgroundColor": "#A1D1E8",
|
| 34 |
-
"tileColor": "#C3B091",
|
| 35 |
-
"textColor": "#6A5149",
|
| 36 |
-
"welcomeTextColor": "#795548",
|
| 37 |
-
},
|
| 38 |
-
"sunset": {
|
| 39 |
-
"primaryColor": "#FFB347",
|
| 40 |
-
"backgroundColor": "#E07A5F",
|
| 41 |
-
"tileColor": "#3D405B",
|
| 42 |
-
"textColor": "#F4F1DE",
|
| 43 |
-
"welcomeTextColor": "#F2CC8F",
|
| 44 |
-
},
|
| 45 |
}
|
| 46 |
|
| 47 |
# --- Function to Apply Theme ---
|
|
@@ -51,10 +30,10 @@ def apply_theme(theme):
|
|
| 51 |
body {{
|
| 52 |
color: {theme["textColor"]};
|
| 53 |
background-color: {theme["backgroundColor"]};
|
| 54 |
-
font-family:
|
| 55 |
}}
|
| 56 |
.welcome-text {{
|
| 57 |
-
color: {theme["
|
| 58 |
font-size: 36px;
|
| 59 |
font-weight: bold;
|
| 60 |
text-align: center;
|
|
@@ -67,8 +46,13 @@ def apply_theme(theme):
|
|
| 67 |
border-radius: 5px;
|
| 68 |
margin-top: 20px;
|
| 69 |
}}
|
| 70 |
-
.
|
| 71 |
-
color: {theme["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}}
|
| 73 |
.stMetricLabel {{
|
| 74 |
color: {theme["textColor"]} !important;
|
|
@@ -76,10 +60,6 @@ def apply_theme(theme):
|
|
| 76 |
.stMetricValue {{
|
| 77 |
color: {theme["textColor"]} !important;
|
| 78 |
}}
|
| 79 |
-
.stFileUploader > div > div:nth-child(1) > div > button {{
|
| 80 |
-
background-color: {theme["primaryColor"]};
|
| 81 |
-
color: {theme["textColor"]};
|
| 82 |
-
}}
|
| 83 |
</style>
|
| 84 |
""", unsafe_allow_html=True)
|
| 85 |
|
|
@@ -172,18 +152,24 @@ def plagiarism_check(text_chunks, tokenizer, model):
|
|
| 172 |
|
| 173 |
# --- Main Function ---
|
| 174 |
def main():
|
| 175 |
-
# ---
|
| 176 |
-
|
| 177 |
-
apply_theme(selected_theme)
|
| 178 |
|
| 179 |
# --- Title and Welcome ---
|
| 180 |
-
st.markdown("<h1 class='welcome-text'>
|
| 181 |
|
| 182 |
-
# ---
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
if uploaded_files:
|
| 188 |
for uploaded_file in uploaded_files:
|
| 189 |
file_size = len(uploaded_file.getvalue())
|
|
@@ -193,11 +179,10 @@ def main():
|
|
| 193 |
|
| 194 |
try:
|
| 195 |
if uploaded_file.type == "application/pdf":
|
| 196 |
-
raw_text
|
| 197 |
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 198 |
-
raw_text
|
| 199 |
else:
|
| 200 |
-
raw_text = None
|
| 201 |
st.error(f"{uploaded_file.name}: Unsupported file type")
|
| 202 |
continue
|
| 203 |
|
|
@@ -205,16 +190,14 @@ def main():
|
|
| 205 |
st.error(f"Error processing {uploaded_file.name}: {e}")
|
| 206 |
continue
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
if manual_text:
|
| 216 |
-
text_chunks = split_text_into_chunks(manual_text, tokenizer)
|
| 217 |
-
process_and_display(text_chunks, "Manual Input", ai_detection_model, tokenizer, plagiarism_model)
|
| 218 |
|
| 219 |
# --- Helper function to process text and display results ---
|
| 220 |
def process_and_display(text_chunks, source_name, ai_detection_model, tokenizer, plagiarism_model):
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
import random
|
| 4 |
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
|
|
|
| 13 |
# --- Streamlit Page Configuration ---
|
| 14 |
st.set_page_config(page_title="AI & Plagiarism Detection", page_icon="π")
|
| 15 |
|
| 16 |
+
# --- ChatGPT Theme ---
|
| 17 |
+
CHATGPT_THEME = {
|
| 18 |
+
"backgroundColor": "#343541",
|
| 19 |
+
"textColor": "#D1D5DB",
|
| 20 |
+
"tileColor": "#444654",
|
| 21 |
+
"inputAreaColor": "#40414F",
|
| 22 |
+
"accentColor": "#10a37f",
|
| 23 |
+
"font": "sans-serif",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
# --- Function to Apply Theme ---
|
|
|
|
| 30 |
body {{
|
| 31 |
color: {theme["textColor"]};
|
| 32 |
background-color: {theme["backgroundColor"]};
|
| 33 |
+
font-family: {theme["font"]};
|
| 34 |
}}
|
| 35 |
.welcome-text {{
|
| 36 |
+
color: {theme["textColor"]};
|
| 37 |
font-size: 36px;
|
| 38 |
font-weight: bold;
|
| 39 |
text-align: center;
|
|
|
|
| 46 |
border-radius: 5px;
|
| 47 |
margin-top: 20px;
|
| 48 |
}}
|
| 49 |
+
.stTextArea textarea {{
|
| 50 |
+
background-color: {theme["inputAreaColor"]};
|
| 51 |
+
color: {theme["textColor"]};
|
| 52 |
+
}}
|
| 53 |
+
.stFileUploader > div > div:nth-child(1) > div > button {{
|
| 54 |
+
background-color: {theme["accentColor"]};
|
| 55 |
+
color: {theme["backgroundColor"]};
|
| 56 |
}}
|
| 57 |
.stMetricLabel {{
|
| 58 |
color: {theme["textColor"]} !important;
|
|
|
|
| 60 |
.stMetricValue {{
|
| 61 |
color: {theme["textColor"]} !important;
|
| 62 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
</style>
|
| 64 |
""", unsafe_allow_html=True)
|
| 65 |
|
|
|
|
| 152 |
|
| 153 |
# --- Main Function ---
|
| 154 |
def main():
|
| 155 |
+
# --- Apply ChatGPT Theme ---
|
| 156 |
+
apply_theme(CHATGPT_THEME)
|
|
|
|
| 157 |
|
| 158 |
# --- Title and Welcome ---
|
| 159 |
+
st.markdown("<h1 class='welcome-text'>AI & Plagiarism Detection</h1>", unsafe_allow_html=True)
|
| 160 |
|
| 161 |
+
# --- Input Area: Text Area and File Upload ---
|
| 162 |
+
input_text = st.text_area("Enter text or attach a document", "", height=200)
|
| 163 |
+
uploaded_files = st.file_uploader("Attach documents (PDF or DOCX)", type=["pdf", "docx"], accept_multiple_files=True)
|
| 164 |
|
| 165 |
+
# --- Load models ---
|
| 166 |
+
ai_detection_model, tokenizer, plagiarism_model = load_models()
|
| 167 |
+
|
| 168 |
+
# --- Process Input ---
|
| 169 |
+
if input_text or uploaded_files:
|
| 170 |
+
raw_text = ""
|
| 171 |
+
|
| 172 |
+
# --- Process Uploaded Files ---
|
| 173 |
if uploaded_files:
|
| 174 |
for uploaded_file in uploaded_files:
|
| 175 |
file_size = len(uploaded_file.getvalue())
|
|
|
|
| 179 |
|
| 180 |
try:
|
| 181 |
if uploaded_file.type == "application/pdf":
|
| 182 |
+
raw_text += extract_text_from_pdf(uploaded_file)
|
| 183 |
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 184 |
+
raw_text += extract_text_from_docx(uploaded_file)
|
| 185 |
else:
|
|
|
|
| 186 |
st.error(f"{uploaded_file.name}: Unsupported file type")
|
| 187 |
continue
|
| 188 |
|
|
|
|
| 190 |
st.error(f"Error processing {uploaded_file.name}: {e}")
|
| 191 |
continue
|
| 192 |
|
| 193 |
+
# --- Append Manual Text ---
|
| 194 |
+
raw_text += input_text
|
| 195 |
+
|
| 196 |
+
# --- Split text into manageable chunks ---
|
| 197 |
+
text_chunks = split_text_into_chunks(raw_text, tokenizer)
|
| 198 |
|
| 199 |
+
# --- Process and Display Results ---
|
| 200 |
+
process_and_display(text_chunks, "Combined Input", ai_detection_model, tokenizer, plagiarism_model)
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# --- Helper function to process text and display results ---
|
| 203 |
def process_and_display(text_chunks, source_name, ai_detection_model, tokenizer, plagiarism_model):
|