ineskamoun123's picture
Upload 6 files
00cd388 verified
import pandas as pd
import gradio as gr
# Load dataset
df = pd.read_csv("hf_app_data.csv")
# Make sure product_name is text
df["product_name"] = df["product_name"].astype(str)
# Function to analyze product
def analyze_product(product_query):
product_query = str(product_query).strip()
if not product_query:
return "Please enter a product ID or keyword."
results = df[df["product_name"].str.contains(product_query, case=False, na=False)]
if results.empty:
return "Product not found. Try another ID or keyword."
top = results.sort_values(by="popularity_score", ascending=False).head(3)
output = f"Found {len(results)} match(es). Showing top 3:\n\n"
for _, row in top.iterrows():
output += (
f"Product: {row['product_name']}\n"
f"Popularity Score: {row['popularity_score']} ({row['popularity_class']})\n"
f"Rating: {row['avg_rating']}\n"
f"Sentiment: {row['avg_sentiment']}\n"
f"Monthly Sales: {row['avg_monthly_sales']}\n"
f"Price: EUR {row['avg_price']}\n"
f"Recommendation: {row['recommendation']}\n"
f"{'-'*40}\n"
)
return output
# Gradio UI
iface = gr.Interface(
fn=analyze_product,
inputs=gr.Textbox(
label="Enter Product ID (e.g. B003U925C4)",
placeholder="Example: B003U925C4"
),
outputs=gr.Textbox(
label="Analysis",
lines=18
),
title="Product Popularity Analyzer",
description="Analyze product popularity based on sentiment, ratings, and sales data."
)
iface.launch()