Spaces:
Runtime error
Runtime error
File size: 6,189 Bytes
4d483b8 86fe5f4 4d483b8 68a3780 c6edc92 d6a3f78 17cb555 86fe5f4 17cb555 86fe5f4 17cb555 86fe5f4 17cb555 86fe5f4 17cb555 |
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 |
import streamlit as st
from bs4 import BeautifulSoup
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet
import random
import nltk
nltk.download('punkt')
nltk.download('wordnet')
import streamlit as st
from bs4 import BeautifulSoup
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet
from transformers import pipeline
# Text analytics
from textblob import TextBlob
from textatistic import Textatistic
# Integration with other tools
from grammarbot import GrammarBotClient
# Error handling and logging
import logging
def paraphrase_text(text, synonyms_num=5, random_synonym=True):
# Tokenize the text
tokens = word_tokenize(text)
# Create a list to hold the paraphrased words
paraphrased_tokens = []
for token in tokens:
# Check if the token is a word
if token.isalpha():
# Get the synonyms of the word
synonyms = []
for syn in wordnet.synsets(token):
for lemma in syn.lemmas():
if lemma.name() != token:
synonyms.append(lemma.name())
# If there are synonyms available, choose a random one
if synonyms:
if random_synonym:
paraphrased_word = random.choice(synonyms)
else:
paraphrased_word = ", ".join(synonyms[:synonyms_num])
# If no synonyms are available, use the original word
else:
paraphrased_word = token
# If the token is not a word, use it as-is
else:
paraphrased_word = token
# Add the paraphrased word to the list
paraphrased_tokens.append(paraphrased_word)
# Join the paraphrased tokens back into a string
paraphrased_text = ' '.join(paraphrased_tokens)
return paraphrased_text
def paraphrase_html(html_text, synonyms_num, random_synonym):
# Parse the HTML using BeautifulSoup
soup = BeautifulSoup(html_text, 'html.parser')
# Find all the text nodes in the HTML
text_nodes = soup.find_all(text=True)
# Paraphrase the text nodes
for node in text_nodes:
node.replace_with(paraphrase_text(node.string, synonyms_num, random_synonym))
# Return the paraphrased HTML
paraphrased_html = str(soup)
return paraphrased_html
def analyze_text(text):
# Text analytics using textblob and textatistic
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
text_stats = Textatistic(text)
readability_score = text_stats.flesch_score
return sentiment, subjectivity, readability_score
def grammar_check(text):
# Integration with grammarbot tool
client = GrammarBotClient()
response = client.check(text)
matches = response['matches']
if not matches:
return "No grammar errors found."
else:
message = "Grammar errors found:\n"
for match in matches:
message += f"{match['message']} at line {match['replacements'][0]['startLine']}\n"
return message
def paraphrase_with_model(text, model):
# Advanced paraphrasing using pre-trained models
paraphrase_pipeline = pipeline("text2text-generation", model=model, tokenizer=model, device=0 if st.config.experimental.get_query_params()['device'] == 'cpu' else -1)
paraphrased_text = paraphrase_pipeline(text, max_length=50, do_sample=True, temperature=0.9)[0]['generated_text']
return paraphrased_text
st.set_page_config(page_title="HTML Paraphraser and Analyzer", page_icon=":pencil2:")
st.sidebar.title("HTML Paraphraser and Analyzer")
st.title("HTML Paraphraser and Analyzer")
html_input = st.text_area("Enter HTML to paraphrase", height=250)
synonyms_num = st.slider("Number of synonyms per word", min_value=1, max_value=10, value=5, step=1)
random_synonym = st.checkbox("Use random synonym", value=True)
#Add a button to paraphrase the HTML
if st.button("Paraphrase HTML"):
try:
paraphrased_html = paraphrase_html(html_input, synonyms_num, random_synonym)
st.write(paraphrased_html, unsafe_allow_html=True)
except Exception as e:
logging.exception("Error occurred while paraphrasing HTML")
st.error("An error occurred while paraphrasing the HTML. Please try again.")
#Add a text area for the user to input the text to analyze
text_input = st.text_area("Enter text to analyze")
#Add a button to analyze the text
if st.button("Analyze Text"):
try:
sentiment, subjectivity, readability_score = analyze_text(text_input)
st.write(f"Sentiment: {sentiment:.2f}")
st.write(f"Subjectivity: {subjectivity:.2f}")
st.write(f"Readability Score: {readability_score:.2f}")
except Exception as e:
logging.exception("Error occurred while analyzing text")
st.error("An error occurred while analyzing the text. Please try again.")
#Add a button to check grammar using GrammarBot
if st.button("Check Grammar"):
try:
grammar_check_result = grammar_check(text_input)
st.write(grammar_check_result)
except Exception as e:
logging.exception("Error occurred while checking grammar")
st.error("An error occurred while checking grammar. Please try again.")
#Add a selectbox for choosing the pre-trained model for advanced paraphrasing
models = {
"GPT-2": "gpt2",
"GPT-Neo": "EleutherAI/gpt-neo-1.3B",
"T5": "t5-base",
"Pegasus": "google/pegasus-large",
}
model_name = st.selectbox("Choose a pre-trained model for advanced paraphrasing", options=list(models.keys()))
#Add a button to paraphrase the text using the selected model
if st.button("Advanced Paraphrasing"):
try:
model = models[model_name]
paraphrased_text = paraphrase_with_model(text_input, model)
st.write(paraphrased_text)
except Exception as e:
logging.exception("Error occurred while performing advanced paraphrasing")
st.error("An error occurred while performing advanced paraphrasing. Please try again.")
#Set up logging to capture any errors
logging.basicConfig(filename='error.log', level=logging.ERROR, format='%(asctime)s %(message)s') |