Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
def
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
if not
|
| 15 |
-
return "No
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Remove any duplicates
|
| 22 |
-
unique_results = list(dict.fromkeys(results))
|
| 23 |
-
|
| 24 |
-
return ", ".join(unique_results)
|
| 25 |
-
|
| 26 |
except Exception as e:
|
| 27 |
return f"An error occurred: {str(e)}"
|
| 28 |
|
| 29 |
# Create the user interface for the app
|
| 30 |
iface = gr.Interface(
|
| 31 |
-
fn=
|
| 32 |
-
inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g.,
|
| 33 |
-
outputs=gr.Textbox(label="Identified
|
| 34 |
-
title="Course Demo:
|
| 35 |
-
description="A simple tool to find
|
| 36 |
)
|
| 37 |
|
| 38 |
# Launch the app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import yake
|
| 3 |
|
| 4 |
+
# Initialize the YAKE keyword extractor. This is a lightweight statistical model.
|
| 5 |
+
# We'll ask it for the top 5 keywords.
|
| 6 |
+
kw_extractor = yake.KeywordExtractor(top=5, n=3)
|
| 7 |
|
| 8 |
+
def find_keywords(text):
|
| 9 |
"""
|
| 10 |
+
Uses the YAKE algorithm to extract keywords from text.
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
+
keywords = kw_extractor.extract_keywords(text)
|
| 14 |
+
if not keywords:
|
| 15 |
+
return "No keywords found."
|
| 16 |
|
| 17 |
+
# YAKE returns a list of tuples (keyword, score). We just want the keyword.
|
| 18 |
+
result = [kw[0] for kw in keywords]
|
| 19 |
+
return ", ".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"An error occurred: {str(e)}"
|
| 22 |
|
| 23 |
# Create the user interface for the app
|
| 24 |
iface = gr.Interface(
|
| 25 |
+
fn=find_keywords,
|
| 26 |
+
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."),
|
| 27 |
+
outputs=gr.Textbox(label="Identified Keywords"),
|
| 28 |
+
title="Course Demo: Keyword Extractor",
|
| 29 |
+
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."
|
| 30 |
)
|
| 31 |
|
| 32 |
# Launch the app
|