Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 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 |
+
# Create web UI
|
| 90 |
+
with gr.Blocks(title="Text Processor") as demo:
|
| 91 |
+
gr.Markdown("# Text Processing Tools")
|
| 92 |
+
gr.Markdown("Analyze text statistics, extract keywords, and check reading difficulty.")
|
| 93 |
+
|
| 94 |
+
with gr.Tab("Analyze Text"):
|
| 95 |
+
text_input1 = gr.Textbox(
|
| 96 |
+
label="Enter text",
|
| 97 |
+
lines=8,
|
| 98 |
+
placeholder="Paste your text here..."
|
| 99 |
+
)
|
| 100 |
+
analysis_output = gr.Textbox(label="Analysis Results", lines=8)
|
| 101 |
+
gr.Button("Analyze", size="lg").click(analyze_text, text_input1, analysis_output)
|
| 102 |
+
|
| 103 |
+
with gr.Tab("Extract Keywords"):
|
| 104 |
+
text_input2 = gr.Textbox(label="Enter text", lines=8)
|
| 105 |
+
count_input = gr.Slider(1, 20, value=5, step=1, label="Number of keywords")
|
| 106 |
+
keywords_output = gr.Textbox(label="Keywords", lines=8)
|
| 107 |
+
gr.Button("Extract", size="lg").click(
|
| 108 |
+
extract_keywords,
|
| 109 |
+
[text_input2, count_input],
|
| 110 |
+
keywords_output
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
with gr.Tab("Reading Level"):
|
| 114 |
+
text_input3 = gr.Textbox(label="Enter text", lines=8)
|
| 115 |
+
level_output = gr.Textbox(label="Reading Level Analysis", lines=5)
|
| 116 |
+
gr.Button("Check Level", size="lg").click(check_reading_level, text_input3, level_output)
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
demo.launch(mcp_server=True)
|