Spaces:
Sleeping
Sleeping
Commit ·
8595152
1
Parent(s): 58ef530
Your commit message
Browse files
app.py
CHANGED
|
@@ -1,27 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from collections import Counter
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def word_and_char_counter(text):
|
| 6 |
-
# Clean and split text into words
|
| 7 |
words = re.findall(r'\w+', text.lower()) # Remove punctuation and make lowercase
|
|
|
|
|
|
|
| 8 |
num_words = len(words)
|
| 9 |
num_chars = len("".join(words)) # Count characters excluding spaces and punctuation
|
| 10 |
|
| 11 |
# Count the frequency of each word
|
| 12 |
-
word_freq = Counter(words)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Format the
|
| 15 |
-
freq_results = ', '.join([f"'{word}': {count}" for word, count in
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Define your interface
|
| 19 |
interface = gr.Interface(
|
| 20 |
fn=word_and_char_counter,
|
| 21 |
inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
|
| 22 |
outputs="text",
|
| 23 |
-
title="
|
| 24 |
-
description="
|
| 25 |
)
|
| 26 |
|
| 27 |
# Launch the app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from collections import Counter
|
| 3 |
import re
|
| 4 |
+
from textblob import TextBlob
|
| 5 |
+
import textstat
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from langdetect import detect
|
| 8 |
+
|
| 9 |
+
# Load a summarization model
|
| 10 |
+
summarizer = pipeline("summarization")
|
| 11 |
|
| 12 |
def word_and_char_counter(text):
|
| 13 |
+
# Clean and split text into words and sentences
|
| 14 |
words = re.findall(r'\w+', text.lower()) # Remove punctuation and make lowercase
|
| 15 |
+
sentences = re.split(r'[.!?]+', text) # Split text into sentences
|
| 16 |
+
num_sentences = len(sentences) - 1 # Adjusting for the last split being empty if text ends with a punctuation
|
| 17 |
num_words = len(words)
|
| 18 |
num_chars = len("".join(words)) # Count characters excluding spaces and punctuation
|
| 19 |
|
| 20 |
# Count the frequency of each word
|
| 21 |
+
word_freq = Counter(words)
|
| 22 |
+
most_common_words = word_freq.most_common(3)
|
| 23 |
+
unique_words = [word for word, count in word_freq.items() if count == 1]
|
| 24 |
+
|
| 25 |
+
# Analyze sentiment
|
| 26 |
+
sentiment = TextBlob(text).sentiment
|
| 27 |
+
|
| 28 |
+
# Calculate Flesch Reading Ease score
|
| 29 |
+
reading_ease = textstat.flesch_reading_ease(text)
|
| 30 |
+
|
| 31 |
+
# Detect language of the text
|
| 32 |
+
language = detect(text)
|
| 33 |
+
|
| 34 |
+
# Generate summary of the text using the transformer model
|
| 35 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
| 36 |
|
| 37 |
+
# Format the results
|
| 38 |
+
freq_results = ', '.join([f"'{word}': {count}" for word, count in most_common_words])
|
| 39 |
+
unique_words_result = ', '.join(unique_words)
|
| 40 |
+
sentiment_text = 'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'
|
| 41 |
+
return f"Language: {language}. {num_sentences} sentences, {num_words} words, {num_chars} characters. Most common words: {freq_results}. Unique words: {unique_words_result}. Sentiment: {sentiment_text}. Readability (Flesch Reading Ease): {reading_ease:.2f}. Summary: {summary}"
|
| 42 |
|
| 43 |
# Define your interface
|
| 44 |
interface = gr.Interface(
|
| 45 |
fn=word_and_char_counter,
|
| 46 |
inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
|
| 47 |
outputs="text",
|
| 48 |
+
title="Comprehensive Text Analysis with Language Detection",
|
| 49 |
+
description="This app provides detailed analysis including language detection, sentence, word, and character counts, lists the most common and unique words, analyzes sentiment, calculates readability, and provides a concise summary of the text."
|
| 50 |
)
|
| 51 |
|
| 52 |
# Launch the app
|