Spaces:
Runtime error
Runtime error
Download app.py from dgotlieb/AI-Course-Keyword-Extractor: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/dgotlieb/AI-Course-Keyword-Extractor/resolve/main/app.py
- Command line
-
hf download hf://spaces/dgotlieb/AI-Course-Keyword-Extractor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/dgotlieb/AI-Course-Keyword-Extractor/resolve/main/app.py
1.21 kB
| 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() |