tomp61 commited on
Commit
035ccca
·
1 Parent(s): 09296db

Create a DIY text analysis app

Browse files
Files changed (2) hide show
  1. app.py +57 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from textblob import TextBlob
4
+
5
+ def analyze_text(text):
6
+ if not text:
7
+ return "Please enter some text to analyse.", 0, 0, 0
8
+
9
+ blob = TextBlob(text)
10
+
11
+ sentiment = blob.sentiment.polarity
12
+
13
+ word_count = len(split(text))
14
+ char_count = len(text)
15
+
16
+ avg_word_length = char_count / word_count
17
+
18
+ return {
19
+ "sentiment_score": round(sentiment, 2),
20
+ "word_count": word_count,
21
+ "char_count": char_count,
22
+ "avg_word_length": rount(avg_word_length, 2)
23
+ }
24
+
25
+ with gr.Blocks() as demo:
26
+ gr.Markdown("# Text Analysis App")
27
+ gr.Markdown("Enter some text to analyze its sentiment and get basic statistics.")
28
+
29
+ with gr.Row():
30
+ text_input = gr.TextBox(
31
+ label = "Input Text",
32
+ placeholder = "Type your text here...",
33
+ lines = 5,
34
+ )
35
+
36
+ with gr.Row():
37
+ analyse_button = gr..Button("Analyze")
38
+
39
+ with gr.Row():
40
+ sentiment_output = gr.Number(label="Sentiment Score (-1 to 1)")
41
+ word_count_output = gr.Number(label="Word Count")
42
+ char_count_output = gr.Number("Character Count")
43
+ avg_length_output = gr.Number(label = "Average Word Length")
44
+
45
+ analyze_button.click(
46
+ fn=analyse_text,
47
+ inputs=text_input,
48
+ outputs=[
49
+ sentiment_output,
50
+ word_count_output,
51
+ char_count_output,
52
+ avg_length_output
53
+ ]
54
+ )
55
+
56
+ if __name__ == '__main__':
57
+ demo.launch():
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ textblob
2
+ numpy