-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
168 lines (135 loc) · 5.61 KB
/
app.py
File metadata and controls
168 lines (135 loc) · 5.61 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
159
160
161
162
163
164
165
166
167
168
"""
tglens - Telegram Analytics Dashboard
A Streamlit app for analyzing Telegram dumps
"""
import streamlit as st
# ── Page functions ───────────────────────────────────────────────────────
def analytics_page():
"""Analytics page — overview, contact insights, and group insights."""
from src.visualizations import (
render_general_overview,
render_contact_analysis,
render_group_insights,
)
messages = st.session_state.get("messages_df")
if messages is None:
st.html("""
<div style="text-align: center;">
<h1>Welcome to tglens! 👋</h1>
<p>Data is processed locally and never leaves your browser.</p>
</div>
""")
st.subheader("🚀 Getting Started")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("""
**Step 1: Export Data**
- Open Telegram Desktop (not mobile, not macOS, not web)
- If you use macOS you can install Telegram Desktop alongside the native version
- Go to Settings → Advanced and scroll down
- Export Telegram data in JSON format
""")
with col2:
st.markdown("""
**Step 2: Upload File**
- Use the file uploader in the sidebar
- Select your exported JSON file (usually `result.json`)
- Wait for processing to complete
""")
with col3:
st.markdown("""
**Step 3: Explore**
- View general overview & chat patterns
- Analyze insights per contact or group
- Chat with your data via local AI (requires [Ollama](https://ollama.com))
""")
return
private_messages = messages[
messages["chat_type"].isin(["personal_chat", "saved_messages"])
].copy()
group_messages = messages[
messages["chat_type"].isin(["private_group", "private_supergroup"])
].copy()
tab1, tab2, tab3 = st.tabs(
[
"📈 General Overview",
"🧑💻 Contact Insights",
"👥 Group Insights",
]
)
with tab1:
render_general_overview(private_messages)
with tab2:
render_contact_analysis(private_messages)
with tab3:
render_group_insights(group_messages)
def rag_page():
"""Chat with Data page — RAG-powered Q&A over Telegram history."""
from src.rag import render_rag_page
messages = st.session_state.get("messages_df")
if messages is None:
st.header("Chat with Your Data")
st.caption(
"Ask questions about your Telegram history · runs fully locally via Ollama"
)
st.info("👈 Upload a Telegram JSON export in the sidebar to get started.")
return
render_rag_page(messages)
# ── Page config ──────────────────────────────────────────────────────────
st.set_page_config(
page_title="tglens - Telegram Analytics",
page_icon="📊",
layout="wide",
)
# ── Navigation ───────────────────────────────────────────────────────────
pg = st.navigation(
[
st.Page(analytics_page, title="Analytics", icon="📊", default=True),
st.Page(rag_page, title="Chat with Data", icon="💬"),
]
)
# ── Sidebar: file upload ─────────────────────────────────────────────────
with st.sidebar:
from src.data_loader import load_into_df
uploaded_file = st.file_uploader(
"📁 Choose your Telegram JSON export file",
type="json",
help="Upload the JSON file exported from Telegram Desktop",
)
if uploaded_file is not None:
with st.spinner("🔄 Loading and processing data..."):
messages = load_into_df(uploaded_file)
if messages is not None and not messages.empty:
st.session_state.messages_df = messages
# Balloons only on first upload / file change
if (
"last_uploaded_file" not in st.session_state
or st.session_state.last_uploaded_file != uploaded_file
):
st.session_state.last_uploaded_file = uploaded_file
st.balloons()
private_count = len(
messages[
messages["chat_type"].isin(["personal_chat", "saved_messages"])
]
)
group_count = len(
messages[
messages["chat_type"].isin(
["private_group", "private_supergroup"]
)
]
)
st.toast(
f"✅ Loaded {len(messages):,} messages! "
f"({private_count:,} private, {group_count:,} group)"
)
else:
st.error(
"Something went wrong. Please ensure you uploaded a valid JSON export."
)
st.session_state.pop("messages_df", None)
else:
st.session_state.pop("messages_df", None)
# ── Run selected page ────────────────────────────────────────────────────
pg.run()