Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from summa import keywords
|
3 |
+
|
4 |
+
# Example texts
|
5 |
+
EXAMPLES = {
|
6 |
+
"Scientific Abstract": """
|
7 |
+
Compatibility of systems of linear constraints over the set of natural numbers.
|
8 |
+
Criteria of compatibility of a system of linear Diophantine equations, strict inequations,
|
9 |
+
and nonstrict inequations are considered. Upper bounds for components of a minimal set of solutions
|
10 |
+
and algorithms of construction of minimal generating sets of solutions for all types of systems are given.
|
11 |
+
""",
|
12 |
+
"News Article": """
|
13 |
+
Machine learning is revolutionizing the way we interact with technology.
|
14 |
+
Artificial intelligence systems are becoming more sophisticated, enabling automated decision making
|
15 |
+
and pattern recognition at unprecedented scales. Deep learning algorithms continue to improve,
|
16 |
+
making breakthroughs in natural language processing and computer vision.
|
17 |
+
""",
|
18 |
+
"Technical Documentation": """
|
19 |
+
The user interface provides intuitive navigation through contextual menus and adaptive layouts.
|
20 |
+
System responses are optimized for performance while maintaining high reliability standards.
|
21 |
+
Database connections are pooled to minimize resource overhead and maximize throughput.
|
22 |
+
"""
|
23 |
+
}
|
24 |
+
|
25 |
+
def extract_keywords(text, num_words, ratio=None):
|
26 |
+
if ratio:
|
27 |
+
# Use ratio method
|
28 |
+
extracted = keywords.keywords(text, ratio=ratio/100.0, split=True)
|
29 |
+
else:
|
30 |
+
# Use words count method
|
31 |
+
extracted = keywords.keywords(text, words=num_words, split=True)
|
32 |
+
|
33 |
+
# Format output
|
34 |
+
result = []
|
35 |
+
for idx, keyword in enumerate(extracted, 1):
|
36 |
+
result.append(f"{idx}. {keyword}")
|
37 |
+
|
38 |
+
return "\n".join(result) if result else "No keywords found."
|
39 |
+
|
40 |
+
def load_example(example_name):
|
41 |
+
return EXAMPLES.get(example_name, "")
|
42 |
+
|
43 |
+
# Create Gradio interface
|
44 |
+
with gr.Blocks(title="Keyword Extraction Tool") as demo:
|
45 |
+
gr.Markdown("# 🎯 Smart Keyword Extraction")
|
46 |
+
gr.Markdown("Extract key phrases from text using TextRank algorithm")
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column(scale=2):
|
50 |
+
input_text = gr.Textbox(
|
51 |
+
label="Input Text",
|
52 |
+
placeholder="Enter your text here...",
|
53 |
+
lines=8
|
54 |
+
)
|
55 |
+
example_dropdown = gr.Dropdown(
|
56 |
+
choices=list(EXAMPLES.keys()),
|
57 |
+
label="Load Example Text"
|
58 |
+
)
|
59 |
+
|
60 |
+
with gr.Column(scale=1):
|
61 |
+
with gr.Tab("Word Count Method"):
|
62 |
+
num_words = gr.Slider(
|
63 |
+
minimum=1,
|
64 |
+
maximum=20,
|
65 |
+
value=5,
|
66 |
+
step=1,
|
67 |
+
label="Number of Keywords"
|
68 |
+
)
|
69 |
+
|
70 |
+
with gr.Tab("Ratio Method"):
|
71 |
+
ratio = gr.Slider(
|
72 |
+
minimum=1,
|
73 |
+
maximum=100,
|
74 |
+
value=20,
|
75 |
+
step=1,
|
76 |
+
label="Percentage of Text to Extract (%)"
|
77 |
+
)
|
78 |
+
|
79 |
+
extract_btn = gr.Button("Extract Keywords", variant="primary")
|
80 |
+
|
81 |
+
output_text = gr.Textbox(
|
82 |
+
label="Extracted Keywords",
|
83 |
+
lines=10,
|
84 |
+
interactive=False
|
85 |
+
)
|
86 |
+
|
87 |
+
# Set up event handlers
|
88 |
+
example_dropdown.change(
|
89 |
+
load_example,
|
90 |
+
inputs=[example_dropdown],
|
91 |
+
outputs=[input_text]
|
92 |
+
)
|
93 |
+
|
94 |
+
# Handle both word count and ratio methods
|
95 |
+
extract_btn.click(
|
96 |
+
extract_keywords,
|
97 |
+
inputs=[input_text, num_words, ratio],
|
98 |
+
outputs=[output_text]
|
99 |
+
)
|
100 |
+
|
101 |
+
# Launch the app
|
102 |
+
demo.launch()
|