ksnkumar's picture
Create app.py
a3f77a9 verified
Raw History Blame Contribute Delete
2.78 kB
import pandas as pd
import matplotlib.pyplot as plt
import gradio as gr
import os
import zipfile
# ---------- EDA FUNCTION ----------
def run_eda(file):
df = pd.read_csv(file.name)
# Basic validation
if "text" not in df.columns or "player" not in df.columns:
return "CSV must contain 'player' and 'text' columns.", None
# Drop missing text
df = df.dropna(subset=["text"])
# Create output directory
os.makedirs("outputs", exist_ok=True)
# ---------- Dataset Summary ----------
summary = []
summary.append(f"Number of rows: {df.shape[0]}")
summary.append(f"Number of columns: {df.shape[1]}")
summary.append("\nMissing values:")
summary.append(str(df.isnull().sum()))
# ---------- Text Length Analysis ----------
df["text_length"] = df["text"].str.len()
stats = df["text_length"].describe()
summary.append("\nText Length Statistics:")
summary.append(str(stats))
# Save summary
summary_path = "outputs/eda_summary.txt"
with open(summary_path, "w") as f:
f.write("\n".join(summary))
# ---------- Plot: Text Length Distribution ----------
plt.figure()
plt.hist(df["text_length"], bins=50)
plt.xlabel("Text Length (characters)")
plt.ylabel("Frequency")
plt.title("Distribution of Reddit Comment Lengths")
text_len_plot = "outputs/text_length_distribution.png"
plt.savefig(text_len_plot)
plt.close()
# ---------- Plot: Top Players ----------
plt.figure()
df["player"].value_counts().head(10).plot(kind="bar")
plt.xlabel("Player")
plt.ylabel("Number of Mentions")
plt.title("Top 10 Most Mentioned NFL Players")
player_plot = "outputs/top_players.png"
plt.savefig(player_plot)
plt.close()
# ---------- Save Cleaned Dataset ----------
cleaned_csv = "outputs/cleaned_nfl_data.csv"
df.to_csv(cleaned_csv, index=False)
# ---------- Zip Results ----------
zip_path = "outputs/nfl_eda_results.zip"
with zipfile.ZipFile(zip_path, "w") as zipf:
zipf.write(summary_path)
zipf.write(text_len_plot)
zipf.write(player_plot)
zipf.write(cleaned_csv)
return (
"\n".join(summary),
zip_path
)
# ---------- GRADIO UI ----------
interface = gr.Interface(
fn=run_eda,
inputs=gr.File(label="Upload NFL Reddit CSV"),
outputs=[
gr.Textbox(label="EDA Summary", lines=20),
gr.File(label="Download All Results (ZIP)")
],
title="NFL Reddit EDA & NLP Lab Analyzer",
description=(
"Upload an NFL Reddit CSV file to perform exploratory data analysis. "
"This application generates dataset insights, visualizations, and "
"a downloadable ZIP with results and plots."
)
)
interface.launch()