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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"],
93
+ icons=['house', 'person','chat-dots'],
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 = """
129
+ 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. Your task is not just to detect specific drug names but to understand the context of the conversation, even if indirect or slang terms are used.
130
+
131
+ Key substances and related slang to focus on:
132
+ - MDMA (Molly, Mandy)
133
+ - LSD (Acid, Tabs)
134
+ - Mephedrone (Meow Meow)
135
+ - Cocaine (Coke, Snow, Blow, White, Charlie)
136
+ - Heroin (Smack, H, Brown)
137
+ - Marijuana (Weed, Pot, Mary Jane, Ganja, Bud)
138
+ - Ecstasy (X, XTC, E)
139
+ - Crack (Rock, Freebase)
140
+
141
+ Common phrases to consider:
142
+ - β€œLooking for Molly” (MDMA reference)
143
+ - β€œI need some snow” (Cocaine reference)
144
+ - β€œGot any tabs?” (LSD reference)
145
+ - β€œCan you hook me up with some blow?” (Cocaine reference)
146
+ - β€œI don’t want to mess with Charlie” (Cocaine reference in a negative context)
147
+
148
+ Instructions:
149
+ 1. If you detect any mention of the above substances, slang, or any activities related to drug sales, trafficking, or use, respond with a simple confirmation of detection, including the drug name or slang term detected.
150
+ 2. Be aware of **negations** or sentences where the user may **refuse** the drugs (e.g., "I don’t want cocaine"). Do not flag these as positive detections.
151
+ 3. **Do not** include the original user message in your response.
152
+ 4. Ensure the format includes the following fields: sender's name, phone number, and the detected drug word/slang. However, if there is **no drug-related content**, respond with only the message "drug word detected: none."
153
+
154
+ Below is the required format for your response:
155
+
156
+ If a drug word is detected:
157
+ {name}
158
+ {phone_number}
159
+ drug word detected: {{drug_word_detected}}
160
+
161
+ If no drug word is detected:
162
+ drug word detected: none
163
+
164
+ Below is the user message
165
+
166
+ User message: {user_message}
167
+
168
+ Response: """
169
+
170
+
171
+
172
+ memory = ConversationBufferWindowMemory(k=5, return_messages=True)
173
+ user_message = str(user_message)
174
+
175
+ # Use the parameter user_message to format the prompt
176
+ formatted_prompt = PROMPT_TEMPLATE.format(
177
+ user_message=user_message,
178
+ name=name,
179
+ phone_number=phone_number
180
+ )
181
+ # Add the formatted prompt to the conversation history
182
+ st.session_state.flowmessages.append(HumanMessage(content=user_message))
183
+
184
+ # Generate a response from the model
185
+ response = llm([SystemMessage(content=formatted_prompt)])
186
+
187
+ # Ensure the response.content is a string
188
+ response_content = str(response.content)
189
+
190
+ # Add the AI response to the conversation history
191
+ st.session_state.flowmessages.append(AIMessage(content=response_content))
192
+
193
+
194
+ # Save the conversation context
195
+ memory.save_context({"input": user_message}, {"output": response_content})
196
+
197
+ return response_content
198
+
199
+
200
+
201
+ # User input for query
202
+
203
+ # Button to send the message
204
+ # if st.button("Send"):
205
+ # if user_input:
206
+ # response = get_chatmodel_response(user_input)
207
+ # st.session_state.messages.append({"message": response, "is_user": False})
208
+ # st.experimental_rerun()
209
+ # else:
210
+ # st.warning("Please enter a message.")
211
+
212
+ # Display the conversation history
213
+ if "flowmessages" in st.session_state:
214
+ st.subheader("Chat")
215
+ for message in st.session_state.flowmessages:
216
+ if isinstance(message, HumanMessage):
217
+ st_message(message.content, is_user=True)
218
+ elif isinstance(message, AIMessage):
219
+ st_message(message.content, is_user=False)
220
+
221
+ def display_home_info():
222
+ # Set background color
223
+ st.markdown(
224
+ """
225
+ <style>
226
+ .reportview-container {
227
+ background: #DEF9C4;
228
+ }
229
+ </style>
230
+ """,
231
+ unsafe_allow_html=True
232
+ )
233
+
234
+ # Title with emoji
235
+ st.title("🏠 Welcome to the Drug-Related Content Detector")
236
+
237
+ # Section for description
238
+ st.markdown(
239
+ """
240
+ <div style='background-color: #50B498; padding: 10px; border-radius: 5px;'>
241
+ <h3 style='color: white;'>Our software solution helps identify drug-related content across multiple platforms.</h3>
242
+ </div>
243
+ """,
244
+ unsafe_allow_html=True
245
+ )
246
+
247
+ # Features list
248
+ st.write("### Features include:")
249
+ st.markdown(
250
+ """
251
+ <ul style='list-style-type: none;'>
252
+ <li>🌐 Real-time monitoring of messages.</li>
253
+ <li>πŸ–ΌοΈ Detection of images and text related to drug trafficking.</li>
254
+ <li>πŸ“Š Comprehensive statistics and insights.</li>
255
+ </ul>
256
+ """,
257
+ unsafe_allow_html=True
258
+ )
259
+
260
+ if selected == "Registration":
261
+ registration()
262
+
263
+ elif selected == "Home":
264
+ display_home_info()
265
+
266
+ elif selected == "Chat":
267
+
268
+ def traffitrack_chatbot():
269
+ st.title('TraffiTrack πŸ’¬')
270
+
271
+ # Dropdown to select platform
272
+ platform = st.selectbox(
273
+ "Choose a platform",
274
+ ["Live πŸ’β€β™€οΈ", "WhatsApp πŸ“±", "Instagram πŸ“Έ", "Telegram βœ‰οΈ"],
275
+ index=0
276
+ )
277
+
278
+ if platform == "Telegram βœ‰οΈ":
279
+ # Hardcoded CSV content
280
+ csv_content = """sender_name,sender_id,phone_number,message_text
281
+ Shruti,1580593004,917304814120,But I would prefer blowing a bag of Charlie
282
+ Shruti,1580593004,917304814120,I want to eat ice cream i am bored
283
+ Shruti,1580593004,917304814120,He’s heavily into smack
284
+ Shruti,1580593004,917304814120,There was a bag of snow in the car
285
+ Shruti,1580593004,917304814120,Did you bring the Mary Jane for the party tonight?
286
+ Shruti,1580593004,917304814120,Mary Jane
287
+ Ritika,1065437474,918828000465,I WANT A BAG OF CHARLIE
288
+ Ritika,1065437474,918828000465,Okayy
289
+ Preeyaj,6649015430,,Haa bhej cocain thoda
290
+ Ritika,1065437474,918828000465,Maal chahiye?
291
+ Preeyaj,6649015430,,Llm
292
+ Ritika,1065437474,918828000465,Kya kar rahe ho?
293
+ Ritika,1065437474,918828000465,Hey"""
294
+
295
+ # Read the CSV content into a DataFrame
296
+ messages_df = pd.read_csv(StringIO(csv_content))
297
+ # Reverse the DataFrame to display messages from first to last
298
+ for idx, row in messages_df[::-1].iterrows(): # Reverse the DataFrame here
299
+ sender_name = row['sender_name']
300
+ message_text = row['message_text']
301
+ # Display each message with its corresponding sender name
302
+ st_message(f"{sender_name}: {message_text}", is_user=False, key=f"telegram_message_{idx}")
303
+
304
+ if st.button("Analyze 🚨"):
305
+ # Initialize count and list for drug-related messages
306
+ drug_count = 0 # Initialize drug_count here
307
+ drug_messages = []
308
+ user_data = {} # Initialize user data dictionary
309
+
310
+ # Analyze each message for drug-related content
311
+ for idx, row in messages_df.iterrows():
312
+ message_text = row['message_text']
313
+ sender_name = row['sender_name']
314
+ sender_id = row['sender_id']
315
+ phone_number = row['phone_number']
316
+
317
+ # Get response from the chat model
318
+ response_content = get_chatmodel_response(message_text)
319
+
320
+ # Check for drug word detected in the response
321
+ if "drug word detected" in response_content and "none" not in response_content:
322
+ drug_word = response_content.split("drug word detected: ")[1].strip()
323
+ drug_count += 1
324
+ drug_messages.append({
325
+ "sender_name": sender_name,
326
+ "sender_id": sender_id,
327
+ "phone_number": phone_number,
328
+ "message_text": message_text,
329
+ "drug_word": drug_word
330
+ })
331
+ # Aggregate data by user
332
+ if sender_name not in user_data:
333
+ user_data[sender_name] = {
334
+ "phone_number": phone_number,
335
+ "message_count": 0,
336
+ "drug_words": []
337
+ }
338
+ user_data[sender_name]["message_count"] += 1
339
+ user_data[sender_name]["drug_words"].append(drug_word)
340
+
341
+ # Display statistics
342
+ st.subheader("Analysis Results πŸ“Š")
343
+ st.write(f"Total drug-related messages detected: {drug_count}")
344
+
345
+ if drug_count > 0:
346
+ # st.write("Details of detected messages:")
347
+ # for message in drug_messages:
348
+ # st.markdown(f"**Phone Number**: {message['phone_number']} \
349
+ # **Sender ID**: {message['sender_id']} \
350
+ # **Message**: {message['message_text']} \
351
+ # **Drug Detected**: {message['drug_word']}")
352
+
353
+ # Prepare data for visualization
354
+ user_names = list(user_data.keys())
355
+ message_counts = [data["message_count"] for data in user_data.values()]
356
+ phone_numbers = [data["phone_number"] for data in user_data.values()]
357
+
358
+ # 1. Bar chart: Messages per user
359
+ st.markdown("### Number of Messages per User πŸ“Š")
360
+ fig = px.bar(
361
+ x=user_names,
362
+ y=message_counts,
363
+ labels={'x': 'User Name', 'y': 'Message Count'},
364
+ title="Messages Detected per User"
365
+ )
366
+ st.plotly_chart(fig)
367
+
368
+ # 2. Pie chart: Distribution of drug-related messages
369
+ st.markdown("### Drug Distribution Among Users 🍰")
370
+ drugs_detected = [drug for user in user_data.values() for drug in user["drug_words"]]
371
+ fig = px.pie(
372
+ names=drugs_detected,
373
+ title="Distribution of Detected Drugs"
374
+ )
375
+ st.plotly_chart(fig)
376
+
377
+ # 3. Horizontal bar chart: Number of drug-related messages per user
378
+ st.markdown("### Drug-related Messages per User πŸ“Š")
379
+ fig = px.bar(
380
+ y=user_names,
381
+ x=message_counts,
382
+ orientation='h',
383
+ labels={'y': 'User Name', 'x': 'Drug-related Messages Count'},
384
+ title="Drug-related Messages per User"
385
+ )
386
+ st.plotly_chart(fig)
387
+
388
+ # 4. Display user details in a table
389
+ st.markdown("### User Details Table πŸ“‹")
390
+ user_df = pd.DataFrame({
391
+ "User Name": user_names,
392
+ "Phone Number": phone_numbers,
393
+ "Message_id" : sender_id,
394
+ "Messages Detected": message_counts
395
+ })
396
+ st.dataframe(user_df)
397
+
398
+ # Optionally: Link to the statistics page
399
+ st.markdown("[View Statistics Page](#)")
400
+ else:
401
+ st.write("No drug-related messages detected.")
402
+
403
+ else:
404
+ # Display chat messages for other platforms with unique keys
405
+ for idx, msg in enumerate(st.session_state.messages):
406
+ st_message(msg["message"], is_user=msg["is_user"], key=f"message_{idx}")
407
+
408
+ # Input for user query
409
+ input_text = st.text_input("Enter your text", key="user_input")
410
+
411
+ if st.button("Send"):
412
+ if input_text:
413
+ # Append the user's message to session state
414
+ st.session_state.messages.append({"message": input_text, "is_user": True})
415
+
416
+ # Get the response from the model
417
+ response = get_chatmodel_response(input_text)
418
+
419
+ # Append the response from the model
420
+ st.session_state.messages.append({"message": response, "is_user": False})
421
+
422
+ # Rerun to refresh the UI with new messages
423
+ st.experimental_rerun()
424
+ else:
425
+ st.warning("Please enter a message.")
426
+
427
+ # Call the chatbot function
428
+ traffitrack_chatbot()
429
+
430
+
431
+ # elif selected == "Statistics":
432
+ # st.title('Drug Trafficking Statistics πŸ“Š')
433
+
434
+ # # Generate sample data
435
+ # data = generate_sample_data()
436
+
437
+ # # Display data
438
+ # st.subheader("Overview of Detected Drugs")
439
+ # st.dataframe(data)
440
+
441
+ # # Plotting the data
442
+ # st.subheader("Detected Instances of Drugs")
443
+ # fig, ax = plt.subplots(figsize=(8, 5))
444
+ # ax.bar(data["Drug Name"], data["Detected Instances"], color="#50B498")
445
+ # plt.title("Detected Instances of Drugs")
446
+ # plt.xlabel("Drug Name")
447
+ # plt.ylabel("Detected Instances")
448
+ # st.pyplot(fig)
449
+
450
+ # # Plotting flagged users
451
+ # st.subheader("Flagged Users")
452
+ # fig, ax = plt.subplots(figsize=(8, 5))
453
+ # ax.bar(data["Drug Name"], data["Flagged Users"], color="#468585")
454
+ # plt.title("Flagged Users")
455
+ # plt.xlabel("Drug Name")
456
+ # plt.ylabel("Flagged Users")
457
+ # st.pyplot(fig)
458
+
459
+ # # Plotting IP addresses
460
+ # st.subheader("Detected IP Addresses")
461
+ # fig, ax = plt.subplots(figsize=(8, 5))
462
+ # ax.bar(data["Drug Name"], data["IP Addresses"], color="#9CDBA6")
463
+ # plt.title("Detected IP Addresses")
464
+ # plt.xlabel("Drug Name")
465
+ # plt.ylabel("Detected IP Addresses")
466
+ # st.pyplot(fig)
467
+
468
+ # Custom CSS for a better user interface
469
+ st.markdown(f"""
470
+ <style>
471
+ .stApp {{
472
+ background-color: #DEF9C4;
473
+ color: #468585;
474
+ }}
475
+ .stButton>button {{
476
+ background-color: #50B498;
477
+ color: #ffffff;
478
+ border: none;
479
+ border-radius: 8px;
480
+ font-size: 16px;
481
+ padding: 10px 20px;
482
+ cursor: pointer;
483
+ }}
484
+ .stButton>button:hover {{
485
+ background-color: #9CDBA6;
486
+ }}
487
+ .stTextInput>input {{
488
+ background-color: #468585;
489
+ color: #ffffff;
490
+ border: 2px solid #50B498;
491
+ border-radius: 8px;
492
+ padding: 10px;
493
+ font-size: 16px;
494
+ }}
495
+ h1, h2, h3 {{
496
+ color: #50B498;
497
+ }}
498
+ .stDataFrame {{
499
+ background-color: #ffffff;
500
+ color: #000000;
501
+ border-radius: 10px;
502
+ padding: 10px;
503
+ }}
504
+ </style>
505
+ """, unsafe_allow_html=True)