File size: 2,159 Bytes
afc7996
58ef530
 
8595152
 
 
 
 
 
afc7996
f8e0912
8595152
58ef530
8595152
 
afc7996
58ef530
 
 
8595152
 
 
 
 
 
 
 
 
 
 
 
58ef530
8595152
 
 
378cc86
 
afc7996
 
 
 
58ef530
401d95e
8595152
378cc86
afc7996
 
 
401d95e
afc7996
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
import gradio as gr
from collections import Counter
import re
import textstat
from transformers import pipeline
from langdetect import detect

# Load a summarization model
summarizer = pipeline("summarization")

def word_and_char_counter(text):
    # Clean and split text into words and sentences
    words = re.findall(r'\w+', text.lower())  # Remove punctuation and make lowercase
    sentences = re.split(r'[.!?]+', text)  # Split text into sentences
    num_sentences = len(sentences) - 1  # Adjusting for the last split being empty if text ends with a punctuation
    num_words = len(words)
    num_chars = len("".join(words))  # Count characters excluding spaces and punctuation

    # Count the frequency of each word
    word_freq = Counter(words)
    most_common_words = word_freq.most_common(3)
    unique_words = [word for word, count in word_freq.items() if count == 1]

    # Calculate Flesch Reading Ease score
    reading_ease = textstat.flesch_reading_ease(text)

    # Detect language of the text
    language = detect(text)

    # Generate summary of the text using the transformer model
    summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']

    # Format the results
    freq_results = ', '.join([f"'{word}': {count}" for word, count in most_common_words])
    unique_words_result = ', '.join(unique_words)

    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}"

# Define your interface
interface = gr.Interface(
    fn=word_and_char_counter,
    inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
    outputs="text",
    title="Comprehensive Text Analysis with Language Detection",
    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."
)

# Launch the app
if __name__ == "__main__":
    interface.launch()