Spaces:
Runtime error
Runtime error
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import gradio as grfrom bertopic import BERTopicfrom sentence_transformers import SentenceTransformer
def run_from_textfile(file): if file is None: return "Please upload a .txt file.", "", None
# Read text file text = file.read().decode("utf-8") docs = [line.strip() for line in text.split("\n") if line.strip()]
if len(docs) < 3: return "Need at least 3 documents (one per line).", "", None
# Embedding model embedder = SentenceTransformer("all-MiniLM-L6-v2")
# Topic model topic_model = BERTopic(embedding_model=embedder) topics, probs = topic_model.fit_transform(docs)
# Topic info topic_info = topic_model.get_topic_info().to_string()
# Map each doc to its topic assignments = "\n".join([f"Doc {i+1}: Topic {topics[i]}" for i in range(len(docs))])
# Visualization fig = topic_model.visualize_barchart(top_n_topics=10)
return topic_info, assignments, fig
with gr.Blocks() as demo: gr.Markdown("# 🧠 Topic Modeling from TXT File (BERTopic)") gr.Markdown( "Upload a plain text (.txt) file. Each line should contain one LLM response.\n\n" "Example format:\n" "```\n" "Response 1...\n" "Response 2...\n" "Response 3...\n" "```\n" )
file_input = gr.File(label="Upload .txt file")
run_btn = gr.Button("Run Topic Modeling")
topic_output = gr.Textbox(label="Topic Overview", lines=10) assignment_output = gr.Textbox(label="Document → Topic Assignments", lines=10) fig_output = gr.Plot(label="Topic Visualization")
run_btn.click( fn=run_from_textfile, inputs=file_input, outputs=[topic_output, assignment_output, fig_output] )
demo.launch()app.pyAdded+61