Spaces:
Runtime error
Runtime error
File size: 1,205 Bytes
c7953bb 2e28f66 c7953bb 2e28f66 c7953bb 2e28f66 c7953bb 2e28f66 c7953bb 2e28f66 c7953bb 2e28f66 c7953bb 0600bd7 c7953bb 2e28f66 c7953bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import gradio as gr
import yake
# Initialize the YAKE keyword extractor. This is a lightweight statistical model.
# We'll ask it for the top 5 keywords.
kw_extractor = yake.KeywordExtractor(top=5, n=3)
def find_keywords(text):
"""
Uses the YAKE algorithm to extract keywords from text.
"""
try:
keywords = kw_extractor.extract_keywords(text)
if not keywords:
return "No keywords found."
# YAKE returns a list of tuples (keyword, score). We just want the keyword.
result = [kw[0] for kw in keywords]
return ", ".join(result)
except Exception as e:
return f"An error occurred: {str(e)}"
# Create the user interface for the app
iface = gr.Interface(
fn=find_keywords,
inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g., The app is good but the location is not as good as I expected."),
outputs=gr.Textbox(label="Identified Keywords"),
title="Course Demo: Keyword Extractor",
description="A simple tool to find the most important keywords in a piece of text using a classic statistical algorithm. Built for the 'Understanding AI' course."
)
# Launch the app
iface.launch() |