Spaces:
Runtime error
Runtime error
| 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') |