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()