APRG commited on
Commit
6a2ebeb
·
verified ·
1 Parent(s): bd9ea2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -9
app.py CHANGED
@@ -1,6 +1,91 @@
1
  import gradio as gr
2
  from textblob import TextBlob
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def sentiment_analysis(text: str) -> dict:
5
  """
6
  Analyze the sentiment of the given text.
@@ -20,15 +105,40 @@ def sentiment_analysis(text: str) -> dict:
20
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
21
  }
22
 
23
- # Create the Gradio interface
24
- demo = gr.Interface(
25
- fn=sentiment_analysis,
26
- inputs=gr.Textbox(placeholder="Enter text to analyze..."),
27
- outputs=gr.JSON(),
28
- title="Text Sentiment Analysis",
29
- description="Analyze the sentiment of text using TextBlob"
30
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Launch the interface and MCP server
33
  if __name__ == "__main__":
34
- demo.launch(share=True, mcp_server=True)
 
1
  import gradio as gr
2
  from textblob import TextBlob
3
 
4
+ def analyze_text(text: str) -> str:
5
+ """Analyze text and return statistics.
6
+
7
+ Args:
8
+ text: The input text to analyze
9
+
10
+ Returns:
11
+ JSON string with analysis results
12
+ """
13
+ words = text.split()
14
+ chars = len(text)
15
+ chars_no_spaces = len(text.replace(" ", ""))
16
+ sentences = text.count(".") + text.count("!") + text.count("?")
17
+
18
+ avg_word_length = round(chars_no_spaces / len(words), 2) if words else 0
19
+ avg_sentence_length = round(len(words) / max(sentences, 1), 2)
20
+
21
+ return json.dumps({
22
+ "total_characters": chars,
23
+ "characters_without_spaces": chars_no_spaces,
24
+ "total_words": len(words),
25
+ "total_sentences": max(sentences, 1),
26
+ "average_word_length": avg_word_length,
27
+ "average_sentence_length": avg_sentence_length
28
+ }, indent=2)
29
+
30
+ def extract_keywords(text: str, count: int = 5) -> str:
31
+ """Extract keywords (most common words) from text.
32
+
33
+ Args:
34
+ text: The input text
35
+ count: Number of keywords to return (default 5)
36
+
37
+ Returns:
38
+ JSON string with keywords and frequencies
39
+ """
40
+ stopwords = {
41
+ "the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for",
42
+ "of", "with", "is", "are", "was", "were", "be", "been", "by", "from"
43
+ }
44
+
45
+ words = text.lower().split()
46
+ filtered = [w.strip(".,!?;:") for w in words if w.lower() not in stopwords]
47
+
48
+ from collections import Counter
49
+ word_freq = Counter(filtered)
50
+ top_words = word_freq.most_common(count)
51
+
52
+ return json.dumps({
53
+ "keywords": [{"word": w, "frequency": f} for w, f in top_words]
54
+ }, indent=2)
55
+
56
+ def check_reading_level(text: str) -> str:
57
+ """Estimate reading difficulty level.
58
+
59
+ Args:
60
+ text: The input text
61
+
62
+ Returns:
63
+ JSON string with reading level estimate
64
+ """
65
+ sentences = max(text.count(".") + text.count("!") + text.count("?"), 1)
66
+ words = len(text.split())
67
+ vowels = "aeiou"
68
+ syllables = sum(1 for c in text.lower() if c in vowels)
69
+
70
+ if words == 0:
71
+ return json.dumps({"error": "No text to analyze"})
72
+
73
+ grade = max(0, (0.39 * (words / sentences)) + (11.8 * (syllables / words)) - 15.59)
74
+
75
+ if grade < 6:
76
+ level = "Elementary School"
77
+ elif grade < 9:
78
+ level = "Middle School"
79
+ elif grade < 13:
80
+ level = "High School"
81
+ else:
82
+ level = "College/Academic"
83
+
84
+ return json.dumps({
85
+ "grade_level": round(grade, 1),
86
+ "reading_level": level
87
+ }, indent=2)
88
+
89
  def sentiment_analysis(text: str) -> dict:
90
  """
91
  Analyze the sentiment of the given text.
 
105
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
106
  }
107
 
108
+ # Create web UI
109
+ with gr.Blocks(title="Text Processor") as demo:
110
+ gr.Markdown("# Text Processing Tools")
111
+ gr.Markdown("Analyze text statistics, extract keywords, and check reading difficulty.")
112
+
113
+ with gr.Tab("Analyze Text"):
114
+ text_input1 = gr.Textbox(
115
+ label="Enter text",
116
+ lines=8,
117
+ placeholder="Paste your text here..."
118
+ )
119
+ analysis_output = gr.Textbox(label="Analysis Results", lines=8)
120
+ gr.Button("Analyze", size="lg").click(analyze_text, text_input1, analysis_output)
121
+
122
+ with gr.Tab("Extract Keywords"):
123
+ text_input2 = gr.Textbox(label="Enter text", lines=8)
124
+ count_input = gr.Slider(1, 20, value=5, step=1, label="Number of keywords")
125
+ keywords_output = gr.Textbox(label="Keywords", lines=8)
126
+ gr.Button("Extract", size="lg").click(
127
+ extract_keywords,
128
+ [text_input2, count_input],
129
+ keywords_output
130
+ )
131
+
132
+ with gr.Tab("Reading Level"):
133
+ text_input3 = gr.Textbox(label="Enter text", lines=8)
134
+ level_output = gr.Textbox(label="Reading Level Analysis", lines=5)
135
+ gr.Button("Check Level", size="lg").click(check_reading_level, text_input3, level_output)
136
+
137
+ with gr.Tab("Text Sentiment Analysis"):
138
+ text_input4 = gr.Textbox(label="Enter text", lines=8, placeholder="Enter text to analyze...")
139
+ sentiment_output = gr.Textbox(label="Text Sentiment Analysis", lines=5)
140
+ gr.Button("Check Level", size="lg").click(sentiment_analysis, text_input4, sentiment_output)
141
 
142
  # Launch the interface and MCP server
143
  if __name__ == "__main__":
144
+ demo.launch(mcp_server=True)