imseldrith commited on
Commit
86fe5f4
·
1 Parent(s): 0347ebc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -45
app.py CHANGED
@@ -1,48 +1,61 @@
1
  import streamlit as st
2
- import transformers
3
- from transformers import pipeline, set_seed
 
4
  import random
5
- import time
6
- import textblob
7
- from textblob import TextBlob
8
- from flask import Flask, request
9
 
10
- app = Flask(__name__)
11
-
12
- @st.cache
13
- def run_model(input_text):
14
- set_seed(42)
15
- model = pipeline("text-generation", model="bert-base-cased", tokenizer="bert-base-cased")
16
- generated_text = model(input_text, max_length=1024, num_return_sequences=1).generated_text[0]
17
- return generated_text
18
-
19
- def main():
20
- st.set_page_config(page_title="Paraphraser", page_icon=":guardsman:", layout="wide")
21
- st.title("Paraphraser")
22
-
23
- input_text = st.text_area("Input Text", "Type your text here")
24
- options = st.selectbox("Paraphrase Options", ["Correct Grammar", "Remove Special Characters"])
25
-
26
- if options == "Correct Grammar":
27
- input_text = str(TextBlob(input_text).correct())
28
-
29
- if options == "Remove Special Characters":
30
- input_text = ''.join(e for e in input_text if e.isalnum() or e.isspace())
31
-
32
- progress_bar = st.progress(0)
33
-
34
- if st.button("Paraphrase"):
35
- generated_text = run_model(input_text)
36
- st.write("Generated Text:", generated_text)
37
- st.write("Paraphrasing completed!")
38
- st.write("")
39
- st.write("Click the button below to copy the generated text to your clipboard:")
40
- st.button("Copy to Clipboard", script="document.execCommand('copy');", key="copy")
41
-
42
- @app.route("/", methods=["GET", "POST"])
43
- def index():
44
- main()
45
- return st.get_html_str()
46
-
47
- if __name__ == "__main__":
48
- app.run() #host="0.0.0.0",port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)