-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
158 lines (130 loc) · 5.32 KB
/
Copy pathapp.py
File metadata and controls
158 lines (130 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# https://aistudio.google.com/app/u/1/apikey <--- Get Your Google GenAI API Key Here.
import os
import time
import pandas as pd
import streamlit as st
from google import genai
import plotly.express as px
from dotenv import load_dotenv
# Load environment
load_dotenv()
api_key = os.getenv("GOOGLE_GENAI_API_KEY")
client = genai.Client(api_key = api_key)
# Page config
st.set_page_config(page_title = "GenAI-TopicModeling")
# Title section
st.markdown("""
<style>
.centered-title {
text-align: center;
font-size: 36px;
font-weight: bold;
}
</style>
<h1 class="centered-title">
<span style="color:#00843D;">GenAI Topic</span>
<span style="color:black;">Modeling Tool.</span>
</h1>
<br>
""", unsafe_allow_html = True)
# Upload CSV
uploaded_file = st.file_uploader(
"Upload CSV for analysis. (NOTE: each row representing a new document)", type = ["csv"])
if uploaded_file:
df = pd.read_csv(uploaded_file)
st.write("Data Preview:")
st.dataframe(df.head(), use_container_width = True)
# Column selection
text_column = st.selectbox("Select the column for analysis:", df.columns)
# Category selection
category_mode = st.radio(
"Would you like to work with the default categories or provide custom categories?",
["Default Categories", "Custom Categories"],
help = "Default categories: pro-police, pro-protester, anti-police, anti-protester, neutral")
if category_mode == "Custom Categories":
custom_input = st.text_area(
"Enter your custom categories (comma-separated), THEN click anywhere outside the text box:",
placeholder = "e.g. category1, category2, category3")
categories = [c.strip() for c in custom_input.split(",") if c.strip()]
else:
categories = ["pro-police", "pro-protester", "anti-police", "anti-protester", "neutral"]
# Check input validity
if not categories:
st.warning("Please enter at least one valid category.")
st.stop()
if df[text_column].isnull().any():
st.error("Selected column contains missing values.")
st.stop()
# Begin Analysis
st.markdown("### Document Classification")
with st.spinner("Classifying documents using Gemini 2.0 Flash-Lite..."):
texts = df[text_column].astype(str).tolist()
processed_labels = []
batch_size = 30
for batch_start in range(0, len(texts), batch_size):
batch_texts = texts[batch_start:batch_start + batch_size]
joined_texts = "\n\n".join(
[f"{i + 1}. {t}" for i, t in enumerate(batch_texts)])
prompt = (
f"You are a classifier. For each of the following {len(batch_texts)} documents, "
f"assign **one label** from this list: {categories}.\n\n"
f"Documents:\n{joined_texts}\n\n"
f"Return a numbered list of {len(batch_texts)} labels in the same order. "
f"Example output format:\n1. label1\n2. label2\n...\n{len(batch_texts)}. label{len(batch_texts)}")
try:
response = client.models.generate_content(
model = "gemini-2.0-flash-lite",
contents = prompt)
response_text = response.text.strip()
batch_labels = []
for line in response_text.splitlines():
if "." in line:
_, label = line.split(".", 1)
batch_labels.append(label.strip())
if len(batch_labels) != len(batch_texts):
raise ValueError(
f"Expected {len(batch_texts)} labels, got {len(batch_labels)}")
processed_labels.extend(batch_labels)
time.sleep(1)
except Exception as e:
st.error(f"Error processing batch {batch_start}-{batch_start + batch_size}: {str(e)}")
st.stop()
# Combine results
df["Topics"] = processed_labels
topic_counts = df["Topics"].value_counts().reset_index()
topic_counts.columns = ["Topic", "Frequency"]
st.success("Analysis completed successfully!")
# Results Section
st.divider()
st.markdown("""
<style>
.centered-result {
text-align: center;
font-size: 36px;
font-weight: bold;
}
</style>
<h1 class="centered-result">
<span style="color:#00843D;">Analysis</span>
<span style="color:black;">Results.</span>
</h1>
<br>
""", unsafe_allow_html = True)
# Plot
st.markdown("### Topic Frequency Plot")
fig = px.bar(
topic_counts,
x = "Topic", y = "Frequency",
color = "Topic",
title = "Distribution of Topics",
template = "plotly_white")
st.plotly_chart(fig, use_container_width = True)
# Document Search
st.markdown(f"### Search `{text_column}` Content")
search_term = st.text_input("Search in document texts:").strip().lower()
if search_term:
filtered_df = df[df[text_column].str.lower().str.contains(search_term)]
else:
filtered_df = df
st.markdown("### Labeled Documents")
st.dataframe(filtered_df[[text_column, "Topics"]], use_container_width = True, height = 300)