Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from streamlit_option_menu import option_menu
|
|
| 4 |
import os
|
| 5 |
import plotly.express as px
|
| 6 |
from io import StringIO
|
| 7 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 9 |
from langchain.chat_models import AzureChatOpenAI, ChatOpenAI
|
| 10 |
from langchain.memory import ConversationBufferWindowMemory
|
|
@@ -19,33 +18,30 @@ import pandas as pd
|
|
| 19 |
import numpy as np
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
import re
|
| 22 |
-
|
| 23 |
-
#from auth0.v3.management import Auth0
|
| 24 |
|
| 25 |
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro",temperature=0,api_key=GOOGLE_API_KEY,convert_system_message_to_human=True)
|
| 33 |
|
| 34 |
# Streamlit page configuration
|
| 35 |
st.set_page_config(
|
| 36 |
page_title="TraffiTrack",
|
| 37 |
-
page_icon="",
|
| 38 |
layout="wide",
|
| 39 |
-
initial_sidebar_state="expanded",
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#token = GetToken(auth_domain)
|
| 43 |
-
#mgmt_api_token = token.client_credentials(
|
| 44 |
-
# client_id,
|
| 45 |
-
# client_secret,
|
| 46 |
-
# 'dev-u4kd3wndapfdk6dr.us.auth0.com'
|
| 47 |
-
#)
|
| 48 |
-
#auth0 = Auth0(auth_domain, mgmt_api_token['access_token'])
|
| 49 |
|
| 50 |
# Initialize session state for messages and banned users
|
| 51 |
if 'messages' not in st.session_state:
|
|
@@ -97,27 +93,16 @@ def check_for_drug_content(input_text):
|
|
| 97 |
ip_addresses = re.findall(ip_pattern, input_text)
|
| 98 |
|
| 99 |
return found_drugs, phone_numbers, ip_addresses
|
| 100 |
-
|
| 101 |
# Sidebar with options
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
"icon": {"color": "#468585", "font-size": "25px"},
|
| 112 |
-
"nav-link": {
|
| 113 |
-
"font-size": "16px",
|
| 114 |
-
"text-align": "left",
|
| 115 |
-
"margin": "0px",
|
| 116 |
-
"--hover-color": "#9CDBA6"
|
| 117 |
-
},
|
| 118 |
-
"nav-link-selected": {"background-color": "#50B498"},
|
| 119 |
-
}
|
| 120 |
-
)
|
| 121 |
|
| 122 |
|
| 123 |
# Function to get a response from the chat model
|
|
@@ -230,6 +215,9 @@ def get_chatmodel_response(user_message):
|
|
| 230 |
elif isinstance(message, AIMessage):
|
| 231 |
st_message(message.content, is_user=False)
|
| 232 |
|
|
|
|
|
|
|
|
|
|
| 233 |
def display_home_info():
|
| 234 |
# Set background color
|
| 235 |
st.markdown(
|
|
@@ -268,175 +256,190 @@ def display_home_info():
|
|
| 268 |
""",
|
| 269 |
unsafe_allow_html=True
|
| 270 |
)
|
| 271 |
-
# Login and Signup Buttons
|
| 272 |
-
if "auth_token" not in st.session_state:
|
| 273 |
-
st.write("#### Please login or sign up to access the application:")
|
| 274 |
-
col1, col2 = st.columns(2)
|
| 275 |
-
|
| 276 |
-
with col1:
|
| 277 |
-
if st.button("Login"):
|
| 278 |
-
login()
|
| 279 |
-
|
| 280 |
-
with col2:
|
| 281 |
-
if st.button("Sign Up"):
|
| 282 |
-
signup()
|
| 283 |
-
else:
|
| 284 |
-
st.success("You're logged in!")
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
elif selected == "Home":
|
| 290 |
-
|
| 291 |
|
| 292 |
-
elif selected == "Chat":
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
sender_name = row['sender_name']
|
| 326 |
-
message_text = row['message_text']
|
| 327 |
-
# Display each message with its corresponding sender name
|
| 328 |
-
st_message(f"{sender_name}: {message_text}", is_user=False, key=f"telegram_message_{idx}")
|
| 329 |
-
|
| 330 |
-
if st.button("Analyze π¨"):
|
| 331 |
-
# Initialize count and list for drug-related messages
|
| 332 |
-
drug_count = 0
|
| 333 |
-
drug_messages = []
|
| 334 |
-
user_data = {} # Initialize user data dictionary
|
| 335 |
-
|
| 336 |
-
# Analyze each message for drug-related content
|
| 337 |
-
for idx, row in messages_df.iterrows():
|
| 338 |
-
message_text = row['message_text']
|
| 339 |
sender_name = row['sender_name']
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
#
|
| 358 |
-
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
"phone_number": phone_number,
|
| 361 |
-
"
|
| 362 |
-
"
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
|
|
|
|
|
|
| 412 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
-
else:
|
| 415 |
-
# Display chat messages for other platforms with unique keys
|
| 416 |
-
for idx, msg in enumerate(st.session_state.messages):
|
| 417 |
-
st_message(msg["message"], is_user=msg["is_user"], key=f"message_{idx}")
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
|
| 427 |
-
|
| 428 |
-
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
| 432 |
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
|
| 438 |
-
|
| 439 |
-
|
| 440 |
|
| 441 |
|
| 442 |
# elif selected == "Statistics":
|
|
@@ -513,4 +516,4 @@ st.markdown(f"""
|
|
| 513 |
padding: 10px;
|
| 514 |
}}
|
| 515 |
</style>
|
| 516 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 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
|
|
|
|
| 18 |
import numpy as np
|
| 19 |
from dotenv import load_dotenv
|
| 20 |
import re
|
| 21 |
+
from auth0_component import login_button
|
|
|
|
| 22 |
|
| 23 |
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
| 24 |
|
| 25 |
+
load_dotenv()
|
| 26 |
+
|
| 27 |
+
# Auth0 credentials
|
| 28 |
+
clientId = os.getenv('client_id')
|
| 29 |
+
domain = os.getenv('auth_domain')
|
| 30 |
+
client_secret = os.getenv('client_secret')
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 34 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile")
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
# Streamlit page configuration
|
| 38 |
st.set_page_config(
|
| 39 |
page_title="TraffiTrack",
|
| 40 |
+
page_icon=":mag:",
|
| 41 |
layout="wide",
|
| 42 |
+
initial_sidebar_state="expanded",
|
| 43 |
)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Initialize session state for messages and banned users
|
| 47 |
if 'messages' not in st.session_state:
|
|
|
|
| 93 |
ip_addresses = re.findall(ip_pattern, input_text)
|
| 94 |
|
| 95 |
return found_drugs, phone_numbers, ip_addresses
|
|
|
|
| 96 |
# Sidebar with options
|
| 97 |
+
|
| 98 |
+
st.title("Login")
|
| 99 |
+
email = st.text_input("Enter your email...")
|
| 100 |
+
password = st.text_input("Enter your password...",type='password')
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
# Function to get a response from the chat model
|
|
|
|
| 215 |
elif isinstance(message, AIMessage):
|
| 216 |
st_message(message.content, is_user=False)
|
| 217 |
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
def display_home_info():
|
| 222 |
# Set background color
|
| 223 |
st.markdown(
|
|
|
|
| 256 |
""",
|
| 257 |
unsafe_allow_html=True
|
| 258 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
+
user_info = login_button(clientId=clientId, domain=domain)
|
| 261 |
+
if user_info:
|
| 262 |
+
st.write(f'Hi {user_info["nickname"]}')
|
| 263 |
+
# Uncomment below if you want to show more user information
|
| 264 |
+
# st.write(user_info)
|
| 265 |
+
else:
|
| 266 |
+
st.write("Please log in to continue.")
|
| 267 |
+
|
| 268 |
+
if st.button("Login / SignUp"):
|
| 269 |
+
selected = option_menu(
|
| 270 |
+
"Main Menu",
|
| 271 |
+
["Home","Registration","Chat"],
|
| 272 |
+
|
| 273 |
+
icons=['house', 'person','chat-dots'],
|
| 274 |
+
menu_icon="cast",
|
| 275 |
+
default_index=0,
|
| 276 |
+
orientation="horizontal",
|
| 277 |
+
styles={
|
| 278 |
+
"container": {"padding": "5px", "background-color": "#DEF9C4"},
|
| 279 |
+
"icon": {"color": "#468585", "font-size": "25px"},
|
| 280 |
+
"nav-link": {
|
| 281 |
+
"font-size": "16px",
|
| 282 |
+
"text-align": "left",
|
| 283 |
+
"margin": "0px",
|
| 284 |
+
"--hover-color": "#9CDBA6"
|
| 285 |
+
},
|
| 286 |
+
"nav-link-selected": {"background-color": "#50B498"},
|
| 287 |
+
}
|
| 288 |
+
)
|
| 289 |
+
if selected == "Registration":
|
| 290 |
+
registration()
|
| 291 |
|
| 292 |
+
elif selected == "Home":
|
| 293 |
+
display_home_info()
|
| 294 |
|
| 295 |
+
elif selected == "Chat":
|
| 296 |
+
|
| 297 |
+
def traffitrack_chatbot():
|
| 298 |
+
st.title('TraffiTrack π¬')
|
| 299 |
+
|
| 300 |
+
# Dropdown to select platform
|
| 301 |
+
platform = st.selectbox(
|
| 302 |
+
"Choose a platform",
|
| 303 |
+
["Live πββοΈ", "WhatsApp π±", "Instagram πΈ", "Telegram βοΈ"],
|
| 304 |
+
index=0
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
if platform == "Telegram βοΈ":
|
| 308 |
+
# Hardcoded CSV content
|
| 309 |
+
csv_content = """sender_name,sender_id,phone_number,message_text
|
| 310 |
+
Shruti,1580593004,917304814120,But I would prefer blowing a bag of Charlie
|
| 311 |
+
Shruti,1580593004,917304814120,I want to eat ice cream i am bored
|
| 312 |
+
Shruti,1580593004,917304814120,Heβs heavily into smack
|
| 313 |
+
Shruti,1580593004,917304814120,There was a bag of snow in the car
|
| 314 |
+
Shruti,1580593004,917304814120,Did you bring the Mary Jane for the party tonight?
|
| 315 |
+
Shruti,1580593004,917304814120,Mary Jane
|
| 316 |
+
Ritika,1065437474,918828000465,I WANT A BAG OF CHARLIE
|
| 317 |
+
Ritika,1065437474,918828000465,Okayy
|
| 318 |
+
Preeyaj,6649015430,,Haa bhej cocain thoda
|
| 319 |
+
Ritika,1065437474,918828000465,Maal chahiye?
|
| 320 |
+
Preeyaj,6649015430,,Llm
|
| 321 |
+
Ritika,1065437474,918828000465,Kya kar rahe ho?
|
| 322 |
+
Ritika,1065437474,918828000465,Hey"""
|
| 323 |
+
|
| 324 |
+
# Read the CSV content into a DataFrame
|
| 325 |
+
messages_df = pd.read_csv(StringIO(csv_content))
|
| 326 |
+
# Reverse the DataFrame to display messages from first to last
|
| 327 |
+
for idx, row in messages_df[::-1].iterrows(): # Reverse the DataFrame here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
sender_name = row['sender_name']
|
| 329 |
+
message_text = row['message_text']
|
| 330 |
+
# Display each message with its corresponding sender name
|
| 331 |
+
st_message(f"{sender_name}: {message_text}", is_user=False, key=f"telegram_message_{idx}")
|
| 332 |
+
|
| 333 |
+
if st.button("Analyze π¨"):
|
| 334 |
+
# Initialize count and list for drug-related messages
|
| 335 |
+
drug_count = 0
|
| 336 |
+
drug_messages = []
|
| 337 |
+
user_data = {} # Initialize user data dictionary
|
| 338 |
+
|
| 339 |
+
# Analyze each message for drug-related content
|
| 340 |
+
for idx, row in messages_df.iterrows():
|
| 341 |
+
message_text = row['message_text']
|
| 342 |
+
sender_name = row['sender_name']
|
| 343 |
+
sender_id = row['sender_id'] # This will change with each message
|
| 344 |
+
phone_number = row['phone_number']
|
| 345 |
+
|
| 346 |
+
# Get response from the chat model
|
| 347 |
+
response_content = get_chatmodel_response(message_text)
|
| 348 |
+
|
| 349 |
+
# Check for drug word detected in the response
|
| 350 |
+
if "drug word detected" in response_content and "none" not in response_content:
|
| 351 |
+
drug_word = response_content.split("drug word detected: ")[1].strip()
|
| 352 |
+
drug_count += 1
|
| 353 |
+
drug_messages.append({
|
| 354 |
+
"sender_name": sender_name,
|
| 355 |
+
"sender_id": sender_id,
|
| 356 |
"phone_number": phone_number,
|
| 357 |
+
"message_text": message_text,
|
| 358 |
+
"drug_word": drug_word
|
| 359 |
+
})
|
| 360 |
+
# Aggregate data by user
|
| 361 |
+
if sender_name not in user_data:
|
| 362 |
+
user_data[sender_name] = {
|
| 363 |
+
"phone_number": phone_number,
|
| 364 |
+
"sender_id": sender_id, # Store the sender_id here
|
| 365 |
+
"message_count": 0,
|
| 366 |
+
"drug_words": []
|
| 367 |
+
}
|
| 368 |
+
user_data[sender_name]["message_count"] += 1
|
| 369 |
+
user_data[sender_name]["drug_words"].append(drug_word)
|
| 370 |
+
|
| 371 |
+
# Display statistics
|
| 372 |
+
st.subheader("Analysis Results π")
|
| 373 |
+
st.write(f"Total drug-related messages detected: {drug_count}")
|
| 374 |
+
|
| 375 |
+
if drug_count > 0:
|
| 376 |
+
# Prepare data for visualization
|
| 377 |
+
user_names = list(user_data.keys())
|
| 378 |
+
message_counts = [data["message_count"] for data in user_data.values()]
|
| 379 |
+
phone_numbers = [data["phone_number"] for data in user_data.values()]
|
| 380 |
+
sender_ids = [data["sender_id"] for data in user_data.values()] # Use stored sender_id
|
| 381 |
+
|
| 382 |
+
# 1. Bar chart: Messages per user
|
| 383 |
+
st.markdown("### Number of Messages per User π")
|
| 384 |
+
fig = px.bar(
|
| 385 |
+
x=user_names,
|
| 386 |
+
y=message_counts,
|
| 387 |
+
labels={'x': 'User Name', 'y': 'Message Count'},
|
| 388 |
+
title="Messages Detected per User"
|
| 389 |
+
)
|
| 390 |
+
st.plotly_chart(fig)
|
| 391 |
+
|
| 392 |
+
# 2. Pie chart: Distribution of drug-related messages
|
| 393 |
+
st.markdown("### Drug Distribution Among Users π°")
|
| 394 |
+
drugs_detected = [drug for user in user_data.values() for drug in user["drug_words"]]
|
| 395 |
+
fig = px.pie(
|
| 396 |
+
names=drugs_detected,
|
| 397 |
+
title="Distribution of Detected Drugs"
|
| 398 |
+
)
|
| 399 |
+
st.plotly_chart(fig)
|
| 400 |
+
|
| 401 |
+
# 3. Display user details in a table
|
| 402 |
+
st.markdown("### User Details Table π")
|
| 403 |
+
user_df = pd.DataFrame({
|
| 404 |
+
"User Name": user_names,
|
| 405 |
+
"Phone Number": phone_numbers,
|
| 406 |
+
"Sender ID": sender_ids, # Display the correct sender ID
|
| 407 |
+
"Messages Detected": message_counts
|
| 408 |
+
})
|
| 409 |
+
st.dataframe(user_df)
|
| 410 |
|
| 411 |
+
# Optionally: Link to the statistics page
|
| 412 |
+
st.markdown("[View Statistics Page](#)")
|
| 413 |
+
else:
|
| 414 |
+
st.write("No drug-related messages detected.")
|
| 415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
+
else:
|
| 418 |
+
# Display chat messages for other platforms with unique keys
|
| 419 |
+
for idx, msg in enumerate(st.session_state.messages):
|
| 420 |
+
st_message(msg["message"], is_user=msg["is_user"], key=f"message_{idx}")
|
| 421 |
+
|
| 422 |
+
# Input for user query
|
| 423 |
+
input_text = st.text_input("Enter your text", key="user_input")
|
| 424 |
|
| 425 |
+
if st.button("Send"):
|
| 426 |
+
if input_text:
|
| 427 |
+
# Append the user's message to session state
|
| 428 |
+
st.session_state.messages.append({"message": input_text, "is_user": True})
|
| 429 |
|
| 430 |
+
# Get the response from the model
|
| 431 |
+
response = get_chatmodel_response(input_text)
|
| 432 |
|
| 433 |
+
# Append the response from the model
|
| 434 |
+
st.session_state.messages.append({"message": response, "is_user": False})
|
| 435 |
|
| 436 |
+
# Rerun to refresh the UI with new messages
|
| 437 |
+
st.rerun()
|
| 438 |
+
else:
|
| 439 |
+
st.warning("Please enter a message.")
|
| 440 |
|
| 441 |
+
# Call the chatbot function
|
| 442 |
+
traffitrack_chatbot()
|
| 443 |
|
| 444 |
|
| 445 |
# elif selected == "Statistics":
|
|
|
|
| 516 |
padding: 10px;
|
| 517 |
}}
|
| 518 |
</style>
|
| 519 |
+
""", unsafe_allow_html=True)
|