import gradio as gr import os import textstat import urllib.request import re from bs4 import BeautifulSoup CLEANR = re.compile('<.*?>') DEFAULT_SYSTEM_PROMPT = """ Algorithm for this site is based on Readability Wiki - https://en.wikipedia.org/wiki/Readability The following links are the detailed references: 1. https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests#Flesch_reading_ease 2. https://en.wikipedia.org/wiki/SMOG 3. https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index 4. https://en.wikipedia.org/wiki/Dale%E2%80%93Chall_readability_formula """ def measure_readability(message,history): if "https://" in message: response = urllib.request.urlopen(message) html = response.read().decode('utf8') cleantext = BeautifulSoup(html).text #cleantext = BeautifulSoup(html,'lxml').text text = re.sub(CLEANR,'', cleantext) else: text = message vline1 = "==== Content Info ==== " + os.linesep vline2 = "Character Count "+str(textstat.char_count(text, ignore_spaces=True)) + os.linesep vline3 = "Lexicon Count "+str(textstat.lexicon_count(text, removepunct=True)) + os.linesep vline4 = "Syllable Count "+str(textstat.syllable_count(text)) + os.linesep vline5 = "Sentence Count "+str(textstat.sentence_count(text)) + os.linesep vline6 = " " + os.linesep vline7 = "==== Result ==== " + os.linesep vline8 = "Flesch Reading Ease (0-29 = Very confusing to read, 90-100 = Easy to read) is "+str(textstat.flesch_reading_ease(text)) + os.linesep #print("Flesch-Kincaid Grade Level is "+str(textstat.flesch_reading_ease(text))) vline9 = "Smog Index (Years of education before a reader understand) is "+str(textstat.smog_index(text)) + os.linesep vline10 = "Coleman Liau Index (Grade level before a reader understand) is "+str(textstat.coleman_liau_index(text)) + os.linesep #print("Automated Readability Index (Grade level before a reader understand) is "+str(textstat.automated_readability_index(text))) vline11 = "Dale-Chall Readability(1-5 = can be understood by 4th grader, 8-9 = can be undestood by 11th to 15th grade student) Score "+str(textstat.dale_chall_readability_score(text)) + os.linesep vline12 = "Gunning Fog Index (Years of formal education before a reader understand) is "+str(textstat.gunning_fog(text)) + os.linesep #print("Grade Level Comprehension is "+str(textstat.automated_readability_index(text))) #vline13 = "Difficult Words "+str(textstat.difficult_words(text)) + os.linesep vline14 = "Reading Time is "+str(textstat.reading_time(text, ms_per_char=14.69))+" seconds"+ os.linesep answer = vline1+vline2+vline3+vline4+vline5+vline6+vline7+vline8+vline9+vline10+vline11+vline12+vline14 #answer = "Flesch Reading Ease (90-100 = Easy to read, 0-29 = Very confusing to read) is "+str(textstat.flesch_reading_ease(text)) return answer Conversing = gr.ChatInterface(measure_readability, chatbot=gr.Chatbot(height=400,label = "Enter URL or String to evaluate"), retry_btn=None,theme=gr.themes.Monochrome(), title = 'Ecommerce Content Readability Tool', description = DEFAULT_SYSTEM_PROMPT ,undo_btn = None, clear_btn = None, css='footer {visibility: hidden}').launch() #"Algorithm for this site is based on Readability Wiki - https://en.wikipedia.org/wiki/Readability " if __name__ == "__main__": Conversing.launch()