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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message as st_message
3
+ from streamlit_option_menu import option_menu
4
+ import os
5
+ import plotly.express as px
6
+ from io import StringIO
7
+ from langchain.schema import HumanMessage, SystemMessage, AIMessage
8
+ from langchain.chat_models import AzureChatOpenAI, ChatOpenAI
9
+ from langchain.memory import ConversationBufferWindowMemory
10
+ from langchain.prompts import PromptTemplate
11
+ import warnings
12
+ import time
13
+ from sqlalchemy import create_engine, Column, Integer, String, Text, Table, MetaData
14
+ from sqlalchemy.orm import sessionmaker
15
+ import matplotlib.pyplot as plt
16
+ from langchain_groq import ChatGroq
17
+ import pandas as pd
18
+ import numpy as np
19
+ from dotenv import load_dotenv
20
+ import re
21
+
22
+ warnings.filterwarnings("ignore", category=DeprecationWarning)
23
+
24
+ load_dotenv()
25
+
26
+ os.environ['GROQ_API_KEY'] = os.getenv("GROQ_API_KEY")
27
+ llm = ChatGroq(model="llama-3.1-70b-versatile")
28
+
29
+
30
+ # Streamlit page configuration
31
+ st.set_page_config(
32
+ page_title="TraffiTrack",
33
+ page_icon="",
34
+ layout="wide",
35
+ initial_sidebar_state="expanded",
36
+ )
37
+
38
+ # Initialize session state for messages and banned users
39
+ if 'messages' not in st.session_state:
40
+ st.session_state.messages = [{"message": "Hi! How can I assist you today?", "is_user": False}]
41
+ if 'banned_users' not in st.session_state:
42
+ st.session_state.banned_users = []
43
+ if 'flowmessages' not in st.session_state:
44
+ st.session_state.flowmessages = []
45
+
46
+
47
+ # Function to handle registration
48
+ def registration():
49
+ st.title("User Registration")
50
+
51
+ # Ensure session state is initialized
52
+ if "user_data" not in st.session_state:
53
+ st.session_state.user_data = []
54
+
55
+ name = st.text_input("Enter your name")
56
+ phone_number = st.text_input("Enter your phone number")
57
+
58
+ if st.button("Register"):
59
+ if name and phone_number:
60
+ # Append user data to session state as a dictionary
61
+ st.session_state.user_data.append({"name": name, "phone_number": phone_number})
62
+ st.success("Registration successful!")
63
+ else:
64
+ st.warning("Please fill in all fields.")
65
+
66
+
67
+ # Function to simulate drug tracking data
68
+ def generate_sample_data():
69
+ data = {
70
+ "Drug Name": ["MDMA", "LSD", "Mephedrone", "Cocaine", "Heroin"],
71
+ "Detected Instances": [10, 15, 7, 12, 5],
72
+ "Flagged Users": [5, 10, 4, 7, 3],
73
+ "IP Addresses": [3, 8, 2, 6, 2]
74
+ }
75
+ return pd.DataFrame(data)
76
+
77
+ # Function to check for drug-related content and extract info
78
+ def check_for_drug_content(input_text):
79
+ drug_keywords = ["MDMA", "LSD", "Mephedrone", "Cocaine", "Heroin"]
80
+ pattern = r'(\+?\d{1,3}[-. ]?)?\(?\d{1,4}?\)?[-. ]?\d{1,4}[-. ]?\d{1,4}[-. ]?\d{1,9}' # Regex for phone numbers
81
+ ip_pattern = r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' # Regex for IP addresses
82
+
83
+ found_drugs = [keyword for keyword in drug_keywords if keyword.lower() in input_text.lower()]
84
+ phone_numbers = re.findall(pattern, input_text)
85
+ ip_addresses = re.findall(ip_pattern, input_text)
86
+
87
+ return found_drugs, phone_numbers, ip_addresses
88
+
89
+ # Sidebar with options
90
+ selected = option_menu(
91
+ "Main Menu",
92
+ ["Home", "Registration","Chat", "Statistics"],
93
+ icons=['house', 'person','chat-dots', 'bar-chart'],
94
+ menu_icon="cast",
95
+ default_index=0,
96
+ orientation="horizontal",
97
+ styles={
98
+ "container": {"padding": "5px", "background-color": "#DEF9C4"},
99
+ "icon": {"color": "#468585", "font-size": "25px"},
100
+ "nav-link": {
101
+ "font-size": "16px",
102
+ "text-align": "left",
103
+ "margin": "0px",
104
+ "--hover-color": "#9CDBA6"
105
+ },
106
+ "nav-link-selected": {"background-color": "#50B498"},
107
+ }
108
+ )
109
+
110
+
111
+ # Function to get a response from the chat model
112
+ def get_chatmodel_response(user_message):
113
+ # Ensure user_message is a string
114
+ if "user_data" in st.session_state and st.session_state.user_data:
115
+ user_info = st.session_state.user_data[-1] # Get the most recent registered user
116
+ name = user_info.get("name", "Unknown")
117
+ phone_number = user_info.get("phone_number", "Unknown")
118
+ else:
119
+ name = "Unknown"
120
+ phone_number = "Unknown"
121
+
122
+ #better the prompt more formatting
123
+ #chat types -> human message
124
+ #response -> no drug message detected
125
+ #from registration
126
+ #remove debugging
127
+
128
+ PROMPT_TEMPLATE = """You are an expert assistant specializing in detecting drug-related messages for monitoring illegal drug activities. Your role is to analyze user messages carefully to identify mentions of illegal substances or any activity related to drug trafficking, sales, or use.
129
+
130
+ Key substances to focus on:
131
+ - MDMA
132
+ - LSD
133
+ - Mephedrone
134
+ - Cocaine
135
+ - Heroin
136
+ - Marijuana
137
+ - Ecstasy
138
+ - Crack
139
+
140
+ Instructions:
141
+ 1. If you detect any mention of the above substances or any activities related to drug sales, trafficking, or use, respond with a simple confirmation of detection, including the drug name.
142
+ 2. **Do not** include the original user message in your response.
143
+ 3. Ensure the format includes the following fields: sender's name, phone number, and the detected drug word.
144
+ 4. If there is **no drug-related content**, respond with "drug word detected: none."
145
+
146
+ Below is the required format for your response:
147
+
148
+ {name}
149
+ {phone_number}
150
+ drug word detected: {{drug_word_detected}}
151
+
152
+ Below is the user message
153
+
154
+ User message: {user_message}
155
+
156
+ Response:
157
+ """
158
+
159
+
160
+ memory = ConversationBufferWindowMemory(k=5, return_messages=True)
161
+ user_message = str(user_message)
162
+
163
+ # Use the parameter user_message to format the prompt
164
+ formatted_prompt = PROMPT_TEMPLATE.format(
165
+ user_message=user_message,
166
+ name=name,
167
+ phone_number=phone_number
168
+ )
169
+ # Add the formatted prompt to the conversation history
170
+ st.session_state.flowmessages.append(HumanMessage(content=user_message))
171
+
172
+ # Generate a response from the model
173
+ response = llm([SystemMessage(content=formatted_prompt)])
174
+
175
+ # Ensure the response.content is a string
176
+ response_content = str(response.content)
177
+
178
+ # Add the AI response to the conversation history
179
+ st.session_state.flowmessages.append(AIMessage(content=response_content))
180
+
181
+
182
+ # Save the conversation context
183
+ memory.save_context({"input": user_message}, {"output": response_content})
184
+
185
+ return response_content
186
+
187
+
188
+
189
+ # User input for query
190
+
191
+ # Button to send the message
192
+ # if st.button("Send"):
193
+ # if user_input:
194
+ # response = get_chatmodel_response(user_input)
195
+ # st.session_state.messages.append({"message": response, "is_user": False})
196
+ # st.experimental_rerun()
197
+ # else:
198
+ # st.warning("Please enter a message.")
199
+
200
+ # Display the conversation history
201
+ if "flowmessages" in st.session_state:
202
+ st.subheader("Chat")
203
+ for message in st.session_state.flowmessages:
204
+ if isinstance(message, HumanMessage):
205
+ st_message(message.content, is_user=True)
206
+ elif isinstance(message, AIMessage):
207
+ st_message(message.content, is_user=False)
208
+
209
+ def display_home_info():
210
+ # Set background color
211
+ st.markdown(
212
+ """
213
+ <style>
214
+ .reportview-container {
215
+ background: #DEF9C4;
216
+ }
217
+ </style>
218
+ """,
219
+ unsafe_allow_html=True
220
+ )
221
+
222
+ # Title with emoji
223
+ st.title("🏠 Welcome to the Drug-Related Content Detector")
224
+
225
+ # Section for description
226
+ st.markdown(
227
+ """
228
+ <div style='background-color: #50B498; padding: 10px; border-radius: 5px;'>
229
+ <h3 style='color: white;'>Our software solution helps identify drug-related content across multiple platforms.</h3>
230
+ </div>
231
+ """,
232
+ unsafe_allow_html=True
233
+ )
234
+
235
+ # Features list
236
+ st.write("### Features include:")
237
+ st.markdown(
238
+ """
239
+ <ul style='list-style-type: none;'>
240
+ <li>🌐 Real-time monitoring of messages.</li>
241
+ <li>πŸ–ΌοΈ Detection of images and text related to drug trafficking.</li>
242
+ <li>πŸ“Š Comprehensive statistics and insights.</li>
243
+ </ul>
244
+ """,
245
+ unsafe_allow_html=True
246
+ )
247
+
248
+ if selected == "Registration":
249
+ registration()
250
+
251
+ elif selected == "Home":
252
+ display_home_info()
253
+
254
+ elif selected == "Chat":
255
+
256
+ def traffitrack_chatbot():
257
+ st.title('TraffiTrack πŸ’¬')
258
+
259
+ # Dropdown to select platform
260
+ platform = st.selectbox(
261
+ "Choose a platform",
262
+ ["Live πŸ’β€β™€οΈ", "WhatsApp πŸ“±", "Instagram πŸ“Έ", "Telegram βœ‰οΈ"],
263
+ index=0
264
+ )
265
+
266
+ if platform == "Telegram βœ‰οΈ":
267
+ # Hardcoded CSV content
268
+ csv_content = """sender_name,sender_id,phone_number,message_text
269
+ Shruti,1580593004,917304814120,But I would prefer blowing a bag of Charlie
270
+ Shruti,1580593004,917304814120,I want to eat ice cream i am bored
271
+ Shruti,1580593004,917304814120,He’s heavily into smack
272
+ Shruti,1580593004,917304814120,There was a bag of snow in the car
273
+ Shruti,1580593004,917304814120,Did you bring the Mary Jane for the party tonight?
274
+ Shruti,1580593004,917304814120,Mary Jane
275
+ Ritika,1065437474,918828000465,I WANT A BAG OF CHARLIE
276
+ Ritika,1065437474,918828000465,Okayy
277
+ Preeyaj,6649015430,,Haa bhej cocain thoda
278
+ Ritika,1065437474,918828000465,Maal chahiye?
279
+ Preeyaj,6649015430,,Llm
280
+ Ritika,1065437474,918828000465,Kya kar rahe ho?
281
+ Ritika,1065437474,918828000465,Hey"""
282
+
283
+ # Read the CSV content into a DataFrame
284
+ messages_df = pd.read_csv(StringIO(csv_content))
285
+ # Reverse the DataFrame to display messages from first to last
286
+ for idx, row in messages_df[::-1].iterrows(): # Reverse the DataFrame here
287
+ sender_name = row['sender_name']
288
+ message_text = row['message_text']
289
+ # Display each message with its corresponding sender name
290
+ st_message(f"{sender_name}: {message_text}", is_user=False, key=f"telegram_message_{idx}")
291
+
292
+ if st.button("Analyze 🚨"):
293
+ # Initialize count and list for drug-related messages
294
+ drug_count = 0 # Initialize drug_count here
295
+ drug_messages = []
296
+ user_data = {} # Initialize user data dictionary
297
+
298
+ # Analyze each message for drug-related content
299
+ for idx, row in messages_df.iterrows():
300
+ message_text = row['message_text']
301
+ sender_name = row['sender_name']
302
+ sender_id = row['sender_id']
303
+ phone_number = row['phone_number']
304
+
305
+ # Get response from the chat model
306
+ response_content = get_chatmodel_response(message_text)
307
+
308
+ # Check for drug word detected in the response
309
+ if "drug word detected" in response_content and "none" not in response_content:
310
+ drug_word = response_content.split("drug word detected: ")[1].strip()
311
+ drug_count += 1
312
+ drug_messages.append({
313
+ "sender_name": sender_name,
314
+ "sender_id": sender_id,
315
+ "phone_number": phone_number,
316
+ "message_text": message_text,
317
+ "drug_word": drug_word
318
+ })
319
+ # Aggregate data by user
320
+ if sender_name not in user_data:
321
+ user_data[sender_name] = {
322
+ "phone_number": phone_number,
323
+ "message_count": 0,
324
+ "drug_words": []
325
+ }
326
+ user_data[sender_name]["message_count"] += 1
327
+ user_data[sender_name]["drug_words"].append(drug_word)
328
+
329
+ # Display statistics
330
+ st.subheader("Analysis Results πŸ“Š")
331
+ st.write(f"Total drug-related messages detected: {drug_count}")
332
+
333
+ if drug_count > 0:
334
+ # st.write("Details of detected messages:")
335
+ # for message in drug_messages:
336
+ # st.markdown(f"**Phone Number**: {message['phone_number']} \
337
+ # **Sender ID**: {message['sender_id']} \
338
+ # **Message**: {message['message_text']} \
339
+ # **Drug Detected**: {message['drug_word']}")
340
+
341
+ # Prepare data for visualization
342
+ user_names = list(user_data.keys())
343
+ message_counts = [data["message_count"] for data in user_data.values()]
344
+ phone_numbers = [data["phone_number"] for data in user_data.values()]
345
+
346
+ # 1. Bar chart: Messages per user
347
+ st.markdown("### Number of Messages per User πŸ“Š")
348
+ fig = px.bar(
349
+ x=user_names,
350
+ y=message_counts,
351
+ labels={'x': 'User Name', 'y': 'Message Count'},
352
+ title="Messages Detected per User"
353
+ )
354
+ st.plotly_chart(fig)
355
+
356
+ # 2. Pie chart: Distribution of drug-related messages
357
+ st.markdown("### Drug Distribution Among Users 🍰")
358
+ drugs_detected = [drug for user in user_data.values() for drug in user["drug_words"]]
359
+ fig = px.pie(
360
+ names=drugs_detected,
361
+ title="Distribution of Detected Drugs"
362
+ )
363
+ st.plotly_chart(fig)
364
+
365
+ # 3. Horizontal bar chart: Number of drug-related messages per user
366
+ st.markdown("### Drug-related Messages per User πŸ“Š")
367
+ fig = px.bar(
368
+ y=user_names,
369
+ x=message_counts,
370
+ orientation='h',
371
+ labels={'y': 'User Name', 'x': 'Drug-related Messages Count'},
372
+ title="Drug-related Messages per User"
373
+ )
374
+ st.plotly_chart(fig)
375
+
376
+ # 4. Display user details in a table
377
+ st.markdown("### User Details Table πŸ“‹")
378
+ user_df = pd.DataFrame({
379
+ "User Name": user_names,
380
+ "Phone Number": phone_numbers,
381
+ "Message_id" : sender_id,
382
+ "Messages Detected": message_counts
383
+ })
384
+ st.dataframe(user_df)
385
+
386
+ # Optionally: Link to the statistics page
387
+ st.markdown("[View Statistics Page](#)")
388
+ else:
389
+ st.write("No drug-related messages detected.")
390
+
391
+ else:
392
+ # Display chat messages for other platforms with unique keys
393
+ for idx, msg in enumerate(st.session_state.messages):
394
+ st_message(msg["message"], is_user=msg["is_user"], key=f"message_{idx}")
395
+
396
+ # Input for user query
397
+ input_text = st.text_input("Enter your text", key="user_input")
398
+
399
+ if st.button("Send"):
400
+ if input_text:
401
+ # Append the user's message to session state
402
+ st.session_state.messages.append({"message": input_text, "is_user": True})
403
+
404
+ # Get the response from the model
405
+ response = get_chatmodel_response(input_text)
406
+
407
+ # Append the response from the model
408
+ st.session_state.messages.append({"message": response, "is_user": False})
409
+
410
+ # Rerun to refresh the UI with new messages
411
+ st.experimental_rerun()
412
+ else:
413
+ st.warning("Please enter a message.")
414
+
415
+ # Call the chatbot function
416
+ traffitrack_chatbot()
417
+
418
+
419
+ elif selected == "Statistics":
420
+ st.title('Drug Trafficking Statistics πŸ“Š')
421
+
422
+ # Generate sample data
423
+ data = generate_sample_data()
424
+
425
+ # Display data
426
+ st.subheader("Overview of Detected Drugs")
427
+ st.dataframe(data)
428
+
429
+ # Plotting the data
430
+ st.subheader("Detected Instances of Drugs")
431
+ fig, ax = plt.subplots(figsize=(8, 5))
432
+ ax.bar(data["Drug Name"], data["Detected Instances"], color="#50B498")
433
+ plt.title("Detected Instances of Drugs")
434
+ plt.xlabel("Drug Name")
435
+ plt.ylabel("Detected Instances")
436
+ st.pyplot(fig)
437
+
438
+ # Plotting flagged users
439
+ st.subheader("Flagged Users")
440
+ fig, ax = plt.subplots(figsize=(8, 5))
441
+ ax.bar(data["Drug Name"], data["Flagged Users"], color="#468585")
442
+ plt.title("Flagged Users")
443
+ plt.xlabel("Drug Name")
444
+ plt.ylabel("Flagged Users")
445
+ st.pyplot(fig)
446
+
447
+ # Plotting IP addresses
448
+ st.subheader("Detected IP Addresses")
449
+ fig, ax = plt.subplots(figsize=(8, 5))
450
+ ax.bar(data["Drug Name"], data["IP Addresses"], color="#9CDBA6")
451
+ plt.title("Detected IP Addresses")
452
+ plt.xlabel("Drug Name")
453
+ plt.ylabel("Detected IP Addresses")
454
+ st.pyplot(fig)
455
+
456
+ # Custom CSS for a better user interface
457
+ st.markdown(f"""
458
+ <style>
459
+ .stApp {{
460
+ background-color: #DEF9C4;
461
+ color: #468585;
462
+ }}
463
+ .stButton>button {{
464
+ background-color: #50B498;
465
+ color: #ffffff;
466
+ border: none;
467
+ border-radius: 8px;
468
+ font-size: 16px;
469
+ padding: 10px 20px;
470
+ cursor: pointer;
471
+ }}
472
+ .stButton>button:hover {{
473
+ background-color: #9CDBA6;
474
+ }}
475
+ .stTextInput>input {{
476
+ background-color: #468585;
477
+ color: #ffffff;
478
+ border: 2px solid #50B498;
479
+ border-radius: 8px;
480
+ padding: 10px;
481
+ font-size: 16px;
482
+ }}
483
+ h1, h2, h3 {{
484
+ color: #50B498;
485
+ }}
486
+ .stDataFrame {{
487
+ background-color: #ffffff;
488
+ color: #000000;
489
+ border-radius: 10px;
490
+ padding: 10px;
491
+ }}
492
+ </style>
493
+ """, unsafe_allow_html=True)