rioanggara commited on
Commit
ab753b8
·
1 Parent(s): a92f49e

new update

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -8,40 +8,36 @@ from langdetect import detect
8
  # Load a summarization model
9
  summarizer = pipeline("summarization")
10
 
11
- def word_and_char_counter(text):
12
- # Clean and split text into words and sentences
13
- words = re.findall(r'\w+', text.lower()) # Remove punctuation and make lowercase
14
- sentences = re.split(r'[.!?]+', text) # Split text into sentences
15
- num_sentences = len(sentences) - 1 # Adjusting for the last split being empty if text ends with a punctuation
16
  num_words = len(words)
17
- num_chars = len("".join(words)) # Count characters excluding spaces and punctuation
18
-
19
- # Count the frequency of each word
20
- word_freq = Counter(words)
21
- most_common_words = word_freq.most_common(3)
22
- unique_words = [word for word, count in word_freq.items() if count == 1]
23
-
24
- # Calculate Flesch Reading Ease score
25
  reading_ease = textstat.flesch_reading_ease(text)
26
-
27
- # Detect language of the text
28
  language = detect(text)
29
 
30
- # Generate summary of the text using the transformer model
31
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
32
-
33
  # Format the results
34
- freq_results = ', '.join([f"'{word}': {count}" for word, count in most_common_words])
35
- unique_words_result = ', '.join(unique_words)
36
-
37
- return f"Language: {language}. {num_sentences} sentences, {num_words} words, {num_chars} characters. Most common words: {freq_results}. Unique words: {unique_words_result}. Readability (Flesch Reading Ease): {reading_ease:.2f}. Summary: {summary}"
 
 
 
 
 
 
 
 
38
 
39
- # Define your interface
40
  interface = gr.Interface(
41
- fn=word_and_char_counter,
42
  inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
43
- outputs="text",
44
- title="Comprehensive Text Analysis with Language Detection",
45
  description="This app provides detailed analysis including language detection, sentence, word, and character counts, lists the most common and unique words, calculates readability, and provides a concise summary of the text."
46
  )
47
 
 
8
  # Load a summarization model
9
  summarizer = pipeline("summarization")
10
 
11
+ def text_analysis(text):
12
+ # Analyze text: word count, character count, language detection, and readability
13
+ words = re.findall(r'\w+', text.lower())
14
+ sentences = re.split(r'[.!?]+', text)
15
+ num_sentences = len(sentences) - 1
16
  num_words = len(words)
17
+ num_chars = len("".join(words))
 
 
 
 
 
 
 
18
  reading_ease = textstat.flesch_reading_ease(text)
 
 
19
  language = detect(text)
20
 
 
 
 
21
  # Format the results
22
+ return {
23
+ "Language": language,
24
+ "Sentences": num_sentences,
25
+ "Words": num_words,
26
+ "Characters": num_chars,
27
+ "Readability (Flesch Reading Ease)": reading_ease
28
+ }
29
+
30
+ def text_summarization(text):
31
+ # Summarize text using the transformer model
32
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
33
+ return summary
34
 
35
+ # Define your interface with two output components
36
  interface = gr.Interface(
37
+ fn=[text_analysis, text_summarization],
38
  inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
39
+ outputs=[gr.JSON(label="Text Analysis"), "text"],
40
+ title="Comprehensive Text Analysis with Language Detection and Summarization",
41
  description="This app provides detailed analysis including language detection, sentence, word, and character counts, lists the most common and unique words, calculates readability, and provides a concise summary of the text."
42
  )
43