keyextract3 / app.py
memeta-a11y's picture
Update app.py
1228fbf verified
import gradio as gr
import pandas as pd
import tempfile
import os
from keyword_extractor import KeywordExtractor
extractor = KeywordExtractor()
def extract_keywords_batch(file, download_format):
try:
# Read uploaded Excel file
df = pd.read_excel(file.name)
# Check for 'abstract' column
if "Abstract" not in df.columns:
return "❌ Error: 'Abstract' column not found in uploaded Excel file.", None
results = []
for i, row in df.iterrows():
Abstract = str(row["Abstract"])
keywords = extractor.extract(Abstract)
results.append({
"Abstract": Abstract,
"keywords": ", ".join(keywords)
})
result_df = pd.DataFrame(results)
# Save result to a temporary file (CSV or XLSX)
tmp_dir = tempfile.mkdtemp()
if download_format == "CSV":
file_path = os.path.join(tmp_dir, "extracted_keywords.csv")
result_df.to_csv(file_path, index=False)
else:
file_path = os.path.join(tmp_dir, "extracted_keywords.xlsx")
result_df.to_excel(file_path, index=False)
return result_df, file_path
except Exception as e:
return f"❌ Exception occurred: {str(e)}", None
# Gradio Interface
iface = gr.Interface(
fn=extract_keywords_batch,
inputs=[
gr.File(label="Upload .xlsx File with Abstracts"),
gr.Radio(["CSV", "XLSX"], label="Choose Download Format", value="CSV")
],
outputs=[
gr.Dataframe(label="Extracted Keywords"),
gr.File(label="Download File")
],
title="Batch Abstract Keyword Extractor",
description="""
Upload an Excel (.xlsx) file with an **'abstract'** column to extract keywords from each abstract.
You can choose to download the results in CSV or XLSX format.
"""
)
if __name__ == "__main__":
iface.launch()