File size: 1,013 Bytes
afc7996
58ef530
 
afc7996
f8e0912
58ef530
 
afc7996
58ef530
 
 
 
 
 
 
 
afc7996
 
 
 
58ef530
401d95e
 
58ef530
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
import gradio as gr
from collections import Counter
import re

def word_and_char_counter(text):
    # Clean and split text into words
    words = re.findall(r'\w+', text.lower())  # Remove punctuation and make lowercase
    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(3)

    # Format the word frequency results
    freq_results = ', '.join([f"'{word}': {count}" for word, count in word_freq])
    return f"{num_words} words, {num_chars} characters. Most common words: {freq_results}"

# Define your interface
interface = gr.Interface(
    fn=word_and_char_counter,
    inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
    outputs="text",
    title="Word and Character Counter",
    description="Counts the words and characters in your text and shows the most common words."
)

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