Spaces:
Running
Running
File size: 1,732 Bytes
a5b5869 ac9031a a5b5869 |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import gradio as gr
import pandas as pd
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
from nltk.corpus import opinion_lexicon
from nltk.tokenize import word_tokenize
import nltk
from PIL import Image
import numpy as np
import io
# Ensure NLTK data is downloaded
nltk.download('opinion_lexicon')
nltk.download('punkt')
# Your existing logic for generate_word_cloud goes here
def generate_word_cloud(excel_file, column_name):
# Adapt your existing word cloud generation code to work with the input Excel file and column name.
# Instead of displaying the plot, save it to a buffer and return the image.
# Placeholder for your existing logic
# Save the plot to a buffer
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
image = Image.open(buf)
# Convert to numpy array for Gradio output
image_array = np.array(image)
return image_array
# Define Gradio interface
def process_excel(file_obj, column_name):
# Save the uploaded file to a temporary location
with open("temp_excel_file.xlsx", "wb") as f:
f.write(file_obj.read())
# Generate the word cloud
image = generate_word_cloud("temp_excel_file.xlsx", column_name)
# Return the image
return image
iface = gr.Interface(fn=process_excel,
inputs=[gr.File(file_count=1, label="Upload Excel File"), gr.Textbox(label="Column Name")],
outputs=gr.Image(type="numpy", label="Word Cloud"),
title="Word Cloud Generator",
description="Upload an Excel file and enter the column name to generate a word cloud of positive and negative words.")
if __name__ == "__main__":
iface.launch()
|