Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load a stable keyword extraction model once
|
| 5 |
+
# This model is specifically designed for keyword extraction
|
| 6 |
+
pipe = pipeline("token-classification", model="ml6team/keyphrase-extraction-distilbert-inspec", aggregation_strategy="simple")
|
| 7 |
+
|
| 8 |
+
def extract_keywords(text):
|
| 9 |
+
"""
|
| 10 |
+
This function takes text and returns a comma-separated string of keywords.
|
| 11 |
+
"""
|
| 12 |
+
try:
|
| 13 |
+
keywords = pipe(text)
|
| 14 |
+
# The model returns a list of dictionaries, let's extract the words
|
| 15 |
+
if not keywords:
|
| 16 |
+
return "No keywords found."
|
| 17 |
+
|
| 18 |
+
# Join the extracted keyword strings into a single line
|
| 19 |
+
return ", ".join([kw['word'] for kw in keywords])
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"An error occurred: {e}"
|
| 22 |
+
|
| 23 |
+
# Create the user interface for the app
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=extract_keywords,
|
| 26 |
+
inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g., The camera quality is amazing for the price, but the battery life is disappointing..."),
|
| 27 |
+
outputs=gr.Textbox(label="Extracted Keywords"),
|
| 28 |
+
title="Course Demo: AI Keyword Extractor",
|
| 29 |
+
description="A simple tool to find the most important keywords in a piece of text. Built for the 'Understanding AI' course."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch the app
|
| 33 |
+
iface.launch()
|