Mayureshd commited on
Commit
29f762a
1 Parent(s): 36f116e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -44
app.py CHANGED
@@ -1,19 +1,15 @@
1
  import pandas as pd
2
  import numpy as np
3
  import streamlit as st
4
-
5
  from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
6
 
 
7
  model_name = "deepset/roberta-base-squad2"
8
-
9
- # a) Get predictions
10
  nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
11
-
12
- # b) Load model & tokenizer
13
  model = AutoModelForQuestionAnswering.from_pretrained(model_name)
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
 
16
-
17
  suspicious_words = [
18
  "robbery", "crime", "exchange", "extortion", "threat", "suspicious", "fraud", "laundering",
19
  "illegal", "contraband", "smuggling", "burglary", "assault", "hijacking", "kidnapping", "ransom",
@@ -29,47 +25,36 @@ suspicious_words = [
29
  "saboteur", "suicide", "discreet", "hide", "action", "profile", "alert", "vigilant", "clandestine", "riot", "arms", "deal"
30
  ]
31
 
 
32
 
33
- q = ["","",""]
34
- a = ["","",""]
35
-
36
-
37
- q[0] = "What event is going to take place?"
38
- q[1] = "Where is it going to happen"
39
- q[2] = "What time is it going to happen?"
40
-
41
-
42
- QA_input = [{} for i in range(3)]
43
- res = [{} for i in range(3)]
44
 
 
45
  df = pd.read_excel('senti.xlsx')
46
-
47
  parsed_column = df['sentences'].to_list()
48
 
49
- print(parsed_column)
 
 
50
  for sentence in parsed_column:
51
- for i in range(3):
52
- QA_input[i] = {
53
- 'question': q[i],
54
- 'context': sentence
55
- }
56
- res[i] = nlp(QA_input[i])
57
- a[i] = res[i]['answer']
58
-
59
- a1 = a[0].lower()
60
- a1s = set(a1.split())
61
- sus = set(suspicious_words)
62
- cw = a1s.intersection(sus)
63
-
64
- if len(cw) != 0:
65
- st.write("The crime detected is: ",a[0])
66
- if len(a[1]) != 0:
67
- st.write("The location of crime detected is: ",a[1])
68
- elif len(a[1]) == 0:
69
- st.write("No location detected")
70
- if len(a[2]) != 0:
71
- st.write("The time of crime detected is: ",a[2])
72
- elif len(a[2]) == 0:
73
- st.write("No time detected")
74
- elif len(cw) == 0:
75
- st.write("No crime detected")
 
1
  import pandas as pd
2
  import numpy as np
3
  import streamlit as st
 
4
  from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
5
 
6
+ # Initialize transformers
7
  model_name = "deepset/roberta-base-squad2"
 
 
8
  nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
 
 
9
  model = AutoModelForQuestionAnswering.from_pretrained(model_name)
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
 
12
+ # Define suspicious words and questions
13
  suspicious_words = [
14
  "robbery", "crime", "exchange", "extortion", "threat", "suspicious", "fraud", "laundering",
15
  "illegal", "contraband", "smuggling", "burglary", "assault", "hijacking", "kidnapping", "ransom",
 
25
  "saboteur", "suicide", "discreet", "hide", "action", "profile", "alert", "vigilant", "clandestine", "riot", "arms", "deal"
26
  ]
27
 
28
+ questions = ["What event is going to take place?", "Where is it going to happen", "What time is it going to happen?"]
29
 
30
+ # Initialize Streamlit app
31
+ st.title("Crime Detection App")
 
 
 
 
 
 
 
 
 
32
 
33
+ # Load data
34
  df = pd.read_excel('senti.xlsx')
 
35
  parsed_column = df['sentences'].to_list()
36
 
37
+ # Process sentences and store results
38
+ output_data = {'Crime Detected': [], 'Location Detected': [], 'Time Detected': []}
39
+
40
  for sentence in parsed_column:
41
+ answers = nlp(questions, sentence)
42
+ cw = set(answers[0]['answer'].lower().split()) & set(suspicious_words)
43
+
44
+ if cw:
45
+ output_data['Crime Detected'].append(answers[0]['answer'])
46
+ output_data['Location Detected'].append(answers[1]['answer'] if answers[1]['answer'] else 'No location detected')
47
+ output_data['Time Detected'].append(answers[2]['answer'] if answers[2]['answer'] else 'No time detected')
48
+ else:
49
+ output_data['Crime Detected'].append('No crime detected')
50
+ output_data['Location Detected'].append('No location detected')
51
+ output_data['Time Detected'].append('No time detected')
52
+
53
+ # Convert data to DataFrame
54
+ output_df = pd.DataFrame(output_data)
55
+
56
+ # Display results
57
+ st.write(output_df)
58
+
59
+ # Download button for Excel file
60
+ st.download_button(label="Download Excel", data=output_df.to_excel(), file_name='crime_data_output.xlsx', mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')