DexterSptizu's picture
Update app.py
378591a verified
import gradio as gr
from multi_rake import Rake
# Initialize RAKE
rake = Rake(
min_chars=3,
max_words=3,
min_freq=1,
language_code=None
)
# 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_keywords, min_chars, max_words):
# Configure RAKE parameters
rake.min_chars = min_chars
rake.max_words = max_words
# Extract keywords
keywords = rake.apply(text)
# Format output
result = []
for keyword in keywords[:num_keywords]:
if isinstance(keyword, tuple) and len(keyword) == 2:
phrase, score = keyword
result.append(f"Phrase: {phrase} | Score: {score}")
else:
result.append(f"Phrase: {keyword}")
return "\n".join(result)
def load_example(example_name):
return EXAMPLES.get(example_name, "")
# Create Gradio interface
with gr.Blocks(title="Keyword Extraction Tool") as demo:
gr.Markdown("# πŸ”‘ Keyword extraction using multi-rake")
gr.Markdown("**Developed by : Venugopal Adep**")
gr.Markdown("Extract key phrases from any text using RAKE algorithm")
with gr.Row():
with gr.Column():
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"
)
with gr.Column():
num_keywords = gr.Slider(
minimum=1,
maximum=20,
value=10,
step=1,
label="Number of Keywords"
)
min_chars = gr.Slider(
minimum=2,
maximum=10,
value=3,
step=1,
label="Minimum Characters per Word"
)
max_words = gr.Slider(
minimum=1,
maximum=5,
value=3,
step=1,
label="Maximum Words per Phrase"
)
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]
)
extract_btn.click(
extract_keywords,
inputs=[input_text, num_keywords, min_chars, max_words],
outputs=[output_text]
)
demo.launch()