DexterSptizu's picture
Create app.py
05477c7 verified
raw
history blame
3.57 kB
import gradio as gr
from summa import keywords
# Example texts
EXAMPLES = {
"Scientific Abstract": """
Compatibility of systems of linear constraints over the set of natural numbers.
Criteria of compatibility of a system of linear Diophantine equations, strict inequations,
and nonstrict inequations are considered. Upper bounds for components of a minimal set of solutions
and algorithms of construction of minimal generating sets of solutions for all types of systems are given.
""",
"News Article": """
Machine learning is revolutionizing the way we interact with technology.
Artificial intelligence systems are becoming more sophisticated, enabling automated decision making
and pattern recognition at unprecedented scales. Deep learning algorithms continue to improve,
making breakthroughs in natural language processing and computer vision.
""",
"Technical Documentation": """
The user interface provides intuitive navigation through contextual menus and adaptive layouts.
System responses are optimized for performance while maintaining high reliability standards.
Database connections are pooled to minimize resource overhead and maximize throughput.
"""
}
def extract_keywords(text, num_words, ratio=None):
if ratio:
# Use ratio method
extracted = keywords.keywords(text, ratio=ratio/100.0, split=True)
else:
# Use words count method
extracted = keywords.keywords(text, words=num_words, split=True)
# Format output
result = []
for idx, keyword in enumerate(extracted, 1):
result.append(f"{idx}. {keyword}")
return "\n".join(result) if result else "No keywords found."
def load_example(example_name):
return EXAMPLES.get(example_name, "")
# Create Gradio interface
with gr.Blocks(title="Keyword Extraction Tool") as demo:
gr.Markdown("# 🎯 Smart Keyword Extraction")
gr.Markdown("Extract key phrases from text using TextRank algorithm")
with gr.Row():
with gr.Column(scale=2):
input_text = gr.Textbox(
label="Input Text",
placeholder="Enter your text here...",
lines=8
)
example_dropdown = gr.Dropdown(
choices=list(EXAMPLES.keys()),
label="Load Example Text"
)
with gr.Column(scale=1):
with gr.Tab("Word Count Method"):
num_words = gr.Slider(
minimum=1,
maximum=20,
value=5,
step=1,
label="Number of Keywords"
)
with gr.Tab("Ratio Method"):
ratio = gr.Slider(
minimum=1,
maximum=100,
value=20,
step=1,
label="Percentage of Text to Extract (%)"
)
extract_btn = gr.Button("Extract Keywords", variant="primary")
output_text = gr.Textbox(
label="Extracted Keywords",
lines=10,
interactive=False
)
# Set up event handlers
example_dropdown.change(
load_example,
inputs=[example_dropdown],
outputs=[input_text]
)
# Handle both word count and ratio methods
extract_btn.click(
extract_keywords,
inputs=[input_text, num_words, ratio],
outputs=[output_text]
)
# Launch the app
demo.launch()