ENLYAS's picture
Upload folder using huggingface_hub
dee1326 verified
Raw History Blame Contribute Delete
4.97 kB
import gradio as gr
import re
from collections import Counter
def analyze_text(text):
if not text or not text.strip():
return "Please enter some text to analyze.", "", "", "", ""
words = re.findall(r'\b\w+\b', text.lower())
sentences = re.split(r'[.!?]+', text)
sentences = [s.strip() for s in sentences if s.strip()]
word_count = len(words)
char_count = len(text)
char_no_space = len(text.replace(' ', ''))
sentence_count = len(sentences)
avg_word_length = sum(len(w) for w in words) / len(words) if words else 0
avg_sentence_length = sum(len(s.split()) for s in sentences) / len(sentences) if sentences else 0
word_freq = Counter(words)
top_words = word_freq.most_common(5)
positive_words = {'good', 'great', 'excellent', 'amazing', 'wonderful', 'fantastic', 'love', 'happy', 'joy', 'best'}
negative_words = {'bad', 'terrible', 'awful', 'horrible', 'hate', 'sad', 'angry', 'worst', 'poor', 'disappointing'}
pos_count = sum(1 for w in words if w in positive_words)
neg_count = sum(1 for w in words if w in negative_words)
if pos_count > neg_count:
sentiment = "Positive"
elif neg_count > pos_count:
sentiment = "Negative"
else:
sentiment = "Neutral"
summary = f"""Text Statistics:
β€’ Words: {word_count}
β€’ Characters: {char_count} (no spaces: {char_no_space})
β€’ Sentences: {sentence_count}
β€’ Avg word length: {avg_word_length:.1f}
β€’ Avg sentence length: {avg_sentence_length:.1f} words
Sentiment: {sentiment}
Top 5 words: {', '.join(f"{w} ({c})" for w, c in top_words)}"""
return summary, str(word_count), str(char_count), sentiment, str(sentence_count)
def reverse_text(text):
return text[::-1] if text else ""
def count_pattern(text, pattern):
if not text or not pattern:
return "0"
return str(len(re.findall(pattern, text)))
def load_sample(sample_num):
samples = {
1: "The quick brown fox jumps over the lazy dog. This is a great example of text analysis. The fox is very quick and agile.",
2: "I love programming! Python is amazing and wonderful. The best language ever created.",
3: "This is terrible and awful. I hate this so much. The worst experience of my life."
}
return samples.get(sample_num, "")
with gr.Blocks(title="Text Analyzer", theme=gr.themes.Soft()) as demo:
gr.Markdown("# πŸ“ Text Analyzer & Processing Tool")
gr.Markdown("Analyze, process, and extract insights from your text.")
with gr.Row():
with gr.Column(scale=2):
text_input = gr.Textbox(
label="Enter your text",
placeholder="Paste or type your text here...",
lines=8
)
analyze_btn = gr.Button("πŸ” Analyze Text", variant="primary")
with gr.Column(scale=1):
gr.Markdown("### Quick Stats")
word_output = gr.Textbox(label="Word Count", interactive=False)
char_output = gr.Textbox(label="Character Count", interactive=False)
sentiment_output = gr.Textbox(label="Sentiment", interactive=False)
sentence_output = gr.Textbox(label="Sentence Count", interactive=False)
gr.Markdown("---")
with gr.Row():
with gr.Column():
gr.Markdown("### πŸ“Š Detailed Analysis")
analysis_output = gr.Textbox(label="Analysis Results", lines=10, interactive=False)
with gr.Column():
gr.Markdown("### πŸ”§ Text Tools")
reverse_btn = gr.Button("πŸ”„ Reverse Text")
reversed_output = gr.Textbox(label="Reversed Text", interactive=False)
pattern_input = gr.Textbox(label="Regex Pattern", placeholder="e.g., \\d+ for numbers")
pattern_btn = gr.Button("πŸ”’ Count Pattern Matches")
pattern_output = gr.Textbox(label="Match Count", interactive=False)
gr.Markdown("---")
gr.Markdown("### πŸ“‹ Sample Texts")
with gr.Row():
sample_btn1 = gr.Button("Load Sample 1")
sample_btn2 = gr.Button("Load Sample 2")
sample_btn3 = gr.Button("Load Sample 3")
analyze_btn.click(
fn=analyze_text,
inputs=[text_input],
outputs=[analysis_output, word_output, char_output, sentiment_output, sentence_output]
)
reverse_btn.click(
fn=reverse_text,
inputs=[text_input],
outputs=[reversed_output]
)
pattern_btn.click(
fn=count_pattern,
inputs=[text_input, pattern_input],
outputs=[pattern_output]
)
sample_btn1.click(fn=lambda: load_sample(1), outputs=[text_input])
sample_btn2.click(fn=lambda: load_sample(2), outputs=[text_input])
sample_btn3.click(fn=lambda: load_sample(3), outputs=[text_input])
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)