-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomep.py
195 lines (167 loc) · 5.59 KB
/
homep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import streamlit as st
from PIL import Image
import base64
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from nltk.corpus import wordnet
# Function to get synonyms of a word
def get_synonyms(word):
synonyms = set()
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
synonyms.add(lemma.name())
return list(synonyms)
# Function to expand text with synonyms
def expand_with_synonyms(text):
words = nltk.word_tokenize(text)
expanded_text = []
for word in words:
expanded_text.extend(get_synonyms(word))
return ' '.join(expanded_text)
# Progress bar using the provided function and styling
def update_progress(percentage):
if percentage < 16:
color = "green"
elif 16 <= percentage < 35:
color = "yellow"
else:
color = "red"
st.markdown(f"""
<style>
.custom-progress-container {{
width: 100%;
background-color: #eee;
border-radius: 10px;
padding: 5px;
}}
.custom-progress-bar {{
width: {percentage}%;
height: 25px;
border-radius: 10px;
background-color: {color};
text-align: center;
line-height: 25px;
color: black;
}}
</style>
<div class="custom-progress-container">
<div class="custom-progress-bar">{percentage}%</div>
</div>
""", unsafe_allow_html=True)
# LOGO_IMAGE = "PladaAI.png"
LOGO_IMAGE = "pladaainew.png"
img = Image.open('pladaainew.png')
st.set_page_config(
page_title='PladaAI',
page_icon=img,
layout="wide",
)
st.markdown(
"""
<style>
body {
margin: 0;
padding: 0;
}
.logo-text {
font-weight: 500
font-size: 30px !important;
text-align: center;
margin-bottom: 10px;
}
.logotext{
font-weight: 800 !important;
font-size: 25px !important;
text-align: center;
margin-bottom: 10px;
margin-left: -30px;
}
.container {
display: flex;
align-items: center; /* Align items vertically in the container */
justify-content: center; /* Align items horizontally in the container */
gap: 0px; /* Add a gap between the image and text */
}
.logo-img {
width: 200px; /* Set width for the image */
height: 200px;
}
</style>
""",
unsafe_allow_html=True
)
st.markdown(
f"""
<div class="container">
<img class="logo-img" src="data:image/png;base64,{base64.b64encode(open(LOGO_IMAGE, "rb").read()).decode()} ">
<p class="logotext">Welcome to Plada AI</p>
</div>
""",
unsafe_allow_html=True
)
st.markdown(
"""
<h4 class="logo-text">
How can I help you today?
</h4>
""",
unsafe_allow_html=True
)
st.markdown("\n\n")
st.markdown("\n\n")
st.markdown("\n\n")
# User instructions
st.markdown(
"""
### User Instructions:
1. Choose the upload type.
2. If you select "Upload File," you can upload one or more files, and then click the button below.
3. If you select "Upload Text," enter your text in the provided text areas and click the button below.
4. The results will be displayed below.
"""
)
upload_type = st.radio("Select upload type", ("Upload File", "Upload Text"))
if upload_type == "Upload File":
with st.form("File Upload"):
uploaded_files = st.file_uploader(accept_multiple_files=True, label='Upload your files here to check for plagiarism')
submit_button = st.form_submit_button("Check for plagiarism")
# Process the uploaded files and remove them from display
if submit_button:
if uploaded_files:
st.write("Uploaded files saved successfully.")
else:
st.error('Please upload files to check for plagiarism.')
# uploaded_files = None
elif upload_type == "Upload Text":
with st.form("text_upload_form"):
text1 = st.text_area('Enter Your First Text')
text2 = st.text_area('Enter Your Second Text')
submit_button = st.form_submit_button("Check for plagiarism")
# Progress bar initialization
progress_bar_container = st.empty()
# Inside the if submit_button block
if submit_button:
if text1 and text2:
# Combine texts
preprocessed_docs = [text1, text2]
# Expand vocabulary with synonyms
expanded_docs = [expand_with_synonyms(doc) for doc in preprocessed_docs]
# Compute TF-IDF with n-grams
tfidf_vectorizer = TfidfVectorizer(tokenizer=nltk.word_tokenize, stop_words='english', ngram_range=(1, 3))
tfidf_matrix = tfidf_vectorizer.fit_transform(expanded_docs)
# Compute cosine similarity
similarity_matrix = cosine_similarity(tfidf_matrix, tfidf_matrix)
similarity = similarity_matrix[0, 1]
# Update progress bar value based on similarity
progress_value = int(similarity * 100)
# Display custom progress bar
update_progress(progress_value)
# Check if there is a similarity, then display the percentage
if similarity > 0:
st.write(f"Percentage of similarity between the two texts: {int(similarity*100)}")
else:
st.write("No similarity found between the two texts.")
else:
st.error("Please enter text to check for plagiarism.")