asmxp commited on
Commit
d647dca
β€’
1 Parent(s): d126e7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -31
app.py CHANGED
@@ -147,9 +147,9 @@ def get_chatmodel_response(user_message):
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
 
@@ -232,7 +232,7 @@ def display_home_info():
232
  )
233
 
234
  # Title with emoji
235
- st.title("🏠 Welcome to the Drug-Related Content Detector")
236
 
237
  # Section for description
238
  st.markdown(
@@ -303,7 +303,7 @@ elif selected == "Chat":
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
 
@@ -311,7 +311,7 @@ elif selected == "Chat":
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
@@ -332,6 +332,7 @@ elif selected == "Chat":
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
  }
@@ -343,24 +344,18 @@ elif selected == "Chat":
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)
@@ -369,28 +364,17 @@ elif selected == "Chat":
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)
@@ -400,6 +384,7 @@ elif selected == "Chat":
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):
@@ -420,7 +405,7 @@ elif selected == "Chat":
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
 
 
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
 
 
232
  )
233
 
234
  # Title with emoji
235
+ st.title("🏠 Welcome to TraffiTrack")
236
 
237
  # Section for description
238
  st.markdown(
 
303
 
304
  if st.button("Analyze 🚨"):
305
  # Initialize count and list for drug-related messages
306
+ drug_count = 0
307
  drug_messages = []
308
  user_data = {} # Initialize user data dictionary
309
 
 
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'] # This will change with each message
315
  phone_number = row['phone_number']
316
 
317
  # Get response from the chat model
 
332
  if sender_name not in user_data:
333
  user_data[sender_name] = {
334
  "phone_number": phone_number,
335
+ "sender_id": sender_id, # Store the sender_id here
336
  "message_count": 0,
337
  "drug_words": []
338
  }
 
344
  st.write(f"Total drug-related messages detected: {drug_count}")
345
 
346
  if drug_count > 0:
 
 
 
 
 
 
 
347
  # Prepare data for visualization
348
  user_names = list(user_data.keys())
349
  message_counts = [data["message_count"] for data in user_data.values()]
350
  phone_numbers = [data["phone_number"] for data in user_data.values()]
351
+ sender_ids = [data["sender_id"] for data in user_data.values()] # Use stored sender_id
352
 
353
  # 1. Bar chart: Messages per user
354
  st.markdown("### Number of Messages per User πŸ“Š")
355
  fig = px.bar(
356
+ x=user_names,
357
+ y=message_counts,
358
+ labels={'x': 'User Name', 'y': 'Message Count'},
359
  title="Messages Detected per User"
360
  )
361
  st.plotly_chart(fig)
 
364
  st.markdown("### Drug Distribution Among Users 🍰")
365
  drugs_detected = [drug for user in user_data.values() for drug in user["drug_words"]]
366
  fig = px.pie(
367
+ names=drugs_detected,
368
  title="Distribution of Detected Drugs"
369
  )
370
  st.plotly_chart(fig)
371
 
372
+ # 3. Display user details in a table
 
 
 
 
 
 
 
 
 
 
 
373
  st.markdown("### User Details Table πŸ“‹")
374
  user_df = pd.DataFrame({
375
  "User Name": user_names,
376
  "Phone Number": phone_numbers,
377
+ "Sender ID": sender_ids, # Display the correct sender ID
378
  "Messages Detected": message_counts
379
  })
380
  st.dataframe(user_df)
 
384
  else:
385
  st.write("No drug-related messages detected.")
386
 
387
+
388
  else:
389
  # Display chat messages for other platforms with unique keys
390
  for idx, msg in enumerate(st.session_state.messages):
 
405
  st.session_state.messages.append({"message": response, "is_user": False})
406
 
407
  # Rerun to refresh the UI with new messages
408
+ st.rerun()
409
  else:
410
  st.warning("Please enter a message.")
411