File size: 1,421 Bytes
035ccca
 
 
 
 
 
 
 
 
 
 
 
c29ec05
035ccca
 
 
 
11f13f2
 
 
 
 
 
035ccca
 
 
 
 
 
c29ec05
035ccca
 
 
 
 
 
c29ec05
035ccca
 
 
 
 
 
 
 
c29ec05
035ccca
 
 
 
 
 
 
 
 
 
c29ec05
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
52
53
54
55
56
57
58
import gradio as gr
import numpy as np
from textblob import TextBlob

def analyze_text(text):
    if not text:
        return "Please enter some text to analyse.", 0, 0, 0

    blob = TextBlob(text)

    sentiment = blob.sentiment.polarity

    word_count = len(text.split())
    char_count = len(text)

    avg_word_length = char_count / word_count

    return [
        round(sentiment, 2),
        word_count,
        char_count,
        round(avg_word_length, 2),
    ]

with gr.Blocks() as demo:
    gr.Markdown("# Text Analysis App")
    gr.Markdown("Enter some text to analyze its sentiment and get basic statistics.")

    with gr.Row():
        text_input = gr.Textbox(
            label = "Input Text",
            placeholder = "Type your text here...",
            lines = 5,
        )

    with gr.Row():
        analyze_button = gr.Button("Analyze")

    with gr.Row():
        sentiment_output = gr.Number(label="Sentiment Score (-1 to 1)")
        word_count_output = gr.Number(label="Word Count")
        char_count_output = gr.Number("Character Count")
        avg_length_output = gr.Number(label = "Average Word Length")

    analyze_button.click(
        fn=analyze_text,
        inputs=text_input,
        outputs=[
            sentiment_output,
            word_count_output,
            char_count_output,
            avg_length_output
        ]
    )

if __name__ == '__main__':
    demo.launch()