Spaces:
Runtime error
Runtime error
Commit
·
86fe5f4
1
Parent(s):
0347ebc
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,61 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
from
|
|
|
4 |
import random
|
5 |
-
import time
|
6 |
-
import textblob
|
7 |
-
from textblob import TextBlob
|
8 |
-
from flask import Flask, request
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
from nltk.tokenize import word_tokenize
|
4 |
+
from nltk.corpus import wordnet
|
5 |
import random
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def paraphrase_text(text, synonyms_num=5, random_synonym=True):
|
8 |
+
# Tokenize the text
|
9 |
+
tokens = word_tokenize(text)
|
10 |
+
# Create a list to hold the paraphrased words
|
11 |
+
paraphrased_tokens = []
|
12 |
+
for token in tokens:
|
13 |
+
# Check if the token is a word
|
14 |
+
if token.isalpha():
|
15 |
+
# Get the synonyms of the word
|
16 |
+
synonyms = []
|
17 |
+
for syn in wordnet.synsets(token):
|
18 |
+
for lemma in syn.lemmas():
|
19 |
+
if lemma.name() != token:
|
20 |
+
synonyms.append(lemma.name())
|
21 |
+
# If there are synonyms available, choose a random one
|
22 |
+
if synonyms:
|
23 |
+
if random_synonym:
|
24 |
+
paraphrased_word = random.choice(synonyms)
|
25 |
+
else:
|
26 |
+
paraphrased_word = ", ".join(synonyms[:synonyms_num])
|
27 |
+
# If no synonyms are available, use the original word
|
28 |
+
else:
|
29 |
+
paraphrased_word = token
|
30 |
+
# If the token is not a word, use it as-is
|
31 |
+
else:
|
32 |
+
paraphrased_word = token
|
33 |
+
# Add the paraphrased word to the list
|
34 |
+
paraphrased_tokens.append(paraphrased_word)
|
35 |
+
# Join the paraphrased tokens back into a string
|
36 |
+
paraphrased_text = ' '.join(paraphrased_tokens)
|
37 |
+
return paraphrased_text
|
38 |
+
|
39 |
+
def paraphrase_html(html_text, synonyms_num, random_synonym):
|
40 |
+
# Parse the HTML using BeautifulSoup
|
41 |
+
soup = BeautifulSoup(html_text, 'html.parser')
|
42 |
+
# Find all the text nodes in the HTML
|
43 |
+
text_nodes = soup.find_all(text=True)
|
44 |
+
# Paraphrase the text nodes
|
45 |
+
for node in text_nodes:
|
46 |
+
node.replace_with(paraphrase_text(node.string, synonyms_num, random_synonym))
|
47 |
+
# Return the paraphrased HTML
|
48 |
+
paraphrased_html = str(soup)
|
49 |
+
return paraphrased_html
|
50 |
+
|
51 |
+
st.set_page_config(page_title="HTML Paraphraser", page_icon=":pencil2:")
|
52 |
+
st.title("HTML Paraphraser")
|
53 |
+
|
54 |
+
synonyms_num = st.sidebar.slider("Number of Synonyms", min_value=1, max_value=10, value=5, step=1)
|
55 |
+
random_synonym = st.sidebar.checkbox("Use Random Synonym", value=True)
|
56 |
+
|
57 |
+
html_text = st.text_area("Enter HTML text to paraphrase", height=300)
|
58 |
+
|
59 |
+
if st.button("Paraphrase"):
|
60 |
+
paraphrased_html = paraphrase_html(html_text, synonyms_num, random_synonym)
|
61 |
+
st.write(paraphrased_html, unsafe_allow_html=True)
|