heeba001's picture
Update app.py
5234d1f verified
Raw History Blame Contribute Delete
1.02 kB
import gradio as gr
import json
from bertopic import BERTopic
import os
# Load BERTopic model
model_path = os.path.join("topic_model") # folder where you uploaded the model
topic_model = BERTopic.load(model_path)
# Load topic mapping
mapping_path = os.path.join("topic_model", "topic_final_mapping.json")
with open(mapping_path, "r", encoding="utf-8") as f:
topic_mapping = json.load(f)
# Prediction function
def predict_comment(text):
if not text.strip():
return "No input", "Other"
topics, _ = topic_model.transform([text])
topic_id = str(topics[0])
final_topic = topic_mapping.get(topic_id, "Other")
return topic_id, final_topic
# Build Gradio interface
iface = gr.Interface(
fn=predict_comment,
inputs=gr.Textbox(lines=3, placeholder="Type or paste a YouTube comment..."),
outputs=["text", "text"],
title="AFCON Comments Topic Classifier",
description="Type or paste a YouTube comment and get its predicted topic_final."
)
# Launch interface
iface.launch()