Krish30 commited on
Commit
fa84cfc
1 Parent(s): a17aa9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -14
app.py CHANGED
@@ -1,29 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import google.generativeai as genai
3
  import os
4
- import PyPDF2 as pdf
5
  from dotenv import load_dotenv
 
 
 
6
 
7
  load_dotenv()
8
 
9
  genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))
10
 
11
- #alternative key
12
- #genai.configure(api_key=("AIzaSyAr3d_7fp0wMxuUrnf_tATknu_TRPKDdxg"))
13
-
14
  # gemini function for general content generation
15
  def get_gemini_response(input):
16
  model = genai.GenerativeModel('gemini-pro')
17
  response = model.generate_content(input)
18
  return response
19
 
20
- # convert pdf to text
21
- def input_pdf_text(uploaded_file):
22
- reader = pdf.PdfReader(uploaded_file)
23
- text = ""
24
- for page in range(len(reader.pages)):
25
- page = reader.pages[page]
26
- text += str(page.extract_text())
 
 
 
 
 
 
 
 
 
 
 
27
  return text
28
 
29
  # malware detection function
@@ -71,19 +186,19 @@ def display_response_content(response):
71
 
72
  ## Streamlit App
73
  st.title("AI-Powered Security and Chatbot System")
74
- st.text("Use the AI system for malware detection and Awaring yourself.")
75
 
76
  # Tabs for different functionalities
77
  tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])
78
 
79
  with tab1:
80
  st.header("Malware Detection")
81
- uploaded_file = st.file_uploader("Upload a file for malware detection", type="pdf", help="Please upload a PDF file.")
82
  submit_malware = st.button('Analyze for Malware')
83
 
84
  if submit_malware:
85
  if uploaded_file is not None:
86
- text = input_pdf_text(uploaded_file)
87
  response = detect_malware(text)
88
 
89
  # Parse and display response in a structured way
@@ -100,3 +215,4 @@ with tab2:
100
 
101
  # Parse and display response in a structured way
102
  display_response_content(response)
 
 
1
+ # import streamlit as st
2
+ # import google.generativeai as genai
3
+ # import os
4
+ # import PyPDF2 as pdf
5
+ # from dotenv import load_dotenv
6
+
7
+ # load_dotenv()
8
+
9
+ # genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))
10
+
11
+ # #alternative key
12
+ # #genai.configure(api_key=("AIzaSyAr3d_7fp0wMxuUrnf_tATknu_TRPKDdxg"))
13
+
14
+ # # gemini function for general content generation
15
+ # def get_gemini_response(input):
16
+ # model = genai.GenerativeModel('gemini-pro')
17
+ # response = model.generate_content(input)
18
+ # return response
19
+
20
+ # # convert pdf to text
21
+ # def input_pdf_text(uploaded_file):
22
+ # reader = pdf.PdfReader(uploaded_file)
23
+ # text = ""
24
+ # for page in range(len(reader.pages)):
25
+ # page = reader.pages[page]
26
+ # text += str(page.extract_text())
27
+ # return text
28
+
29
+ # # malware detection function
30
+ # def detect_malware(input_text):
31
+ # malware_prompt = f"""
32
+ # ### As a cybersecurity expert, your task is to analyze the following text for any indications of malware.
33
+ # ### Text:
34
+ # {input_text}
35
+ # ### Analysis Output:
36
+ # 1. Identify any potential malware-related content.
37
+ # 2. Explain the reasoning behind your identification.
38
+ # 3. Provide recommendations for mitigating any identified risks.
39
+ # """
40
+ # response = get_gemini_response(malware_prompt)
41
+ # return response
42
+
43
+ # # chatbot function
44
+ # def chatbot_response(user_input):
45
+ # chatbot_prompt = f"""
46
+ # ### You are an intelligent and friendly chatbot. Engage in a meaningful conversation with the user.
47
+ # ### User Input:
48
+ # {user_input}
49
+ # ### Chatbot Response:
50
+ # """
51
+ # response = get_gemini_response(chatbot_prompt)
52
+ # return response
53
+
54
+ # # Function to parse and display response content
55
+ # def display_response_content(response):
56
+ # st.subheader("Response Output")
57
+ # if response and response.candidates:
58
+ # response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
59
+ # sections = response_content.split('###')
60
+ # for section in sections:
61
+ # if section.strip():
62
+ # section_lines = section.split('\n')
63
+ # section_title = section_lines[0].strip()
64
+ # section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
65
+ # if section_title:
66
+ # st.markdown(f"**{section_title}**")
67
+ # if section_body:
68
+ # st.write(section_body)
69
+ # else:
70
+ # st.write("No response received from the model.")
71
+
72
+ # ## Streamlit App
73
+ # st.title("AI-Powered Security and Chatbot System")
74
+ # st.text("Use the AI system for malware detection and Awaring yourself.")
75
+
76
+ # # Tabs for different functionalities
77
+ # tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])
78
+
79
+ # with tab1:
80
+ # st.header("Malware Detection")
81
+ # uploaded_file = st.file_uploader("Upload a file for malware detection", type="pdf", help="Please upload a PDF file.")
82
+ # submit_malware = st.button('Analyze for Malware')
83
+
84
+ # if submit_malware:
85
+ # if uploaded_file is not None:
86
+ # text = input_pdf_text(uploaded_file)
87
+ # response = detect_malware(text)
88
+
89
+ # # Parse and display response in a structured way
90
+ # display_response_content(response)
91
+
92
+ # with tab2:
93
+ # st.header("Chatbot")
94
+ # user_input = st.text_input("Type your message here")
95
+ # submit_chat = st.button('Send')
96
+
97
+ # if submit_chat:
98
+ # if user_input:
99
+ # response = chatbot_response(user_input)
100
+
101
+ # # Parse and display response in a structured way
102
+ # display_response_content(response)
103
+
104
+
105
+
106
  import streamlit as st
107
  import google.generativeai as genai
108
  import os
 
109
  from dotenv import load_dotenv
110
+ import PyPDF2 as pdf
111
+ import docx
112
+ import chardet
113
 
114
  load_dotenv()
115
 
116
  genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))
117
 
 
 
 
118
  # gemini function for general content generation
119
  def get_gemini_response(input):
120
  model = genai.GenerativeModel('gemini-pro')
121
  response = model.generate_content(input)
122
  return response
123
 
124
+ # Function to read text from different file types
125
+ def read_file_content(uploaded_file):
126
+ file_type = uploaded_file.type
127
+ if file_type == "application/pdf":
128
+ reader = pdf.PdfReader(uploaded_file)
129
+ text = ""
130
+ for page in range(len(reader.pages)):
131
+ page = reader.pages[page]
132
+ text += str(page.extract_text())
133
+ elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
134
+ doc = docx.Document(uploaded_file)
135
+ text = '\n'.join([para.text for para in doc.paragraphs])
136
+ else:
137
+ # For other file types, assume it's a text file and try to read it as text
138
+ text = uploaded_file.read()
139
+ result = chardet.detect(text)
140
+ text = text.decode(result['encoding'])
141
+
142
  return text
143
 
144
  # malware detection function
 
186
 
187
  ## Streamlit App
188
  st.title("AI-Powered Security and Chatbot System")
189
+ st.text("Use the AI system for malware detection and friendly conversation.")
190
 
191
  # Tabs for different functionalities
192
  tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])
193
 
194
  with tab1:
195
  st.header("Malware Detection")
196
+ uploaded_file = st.file_uploader("Upload a file for malware detection", type=None, help="Please upload a file of any type.")
197
  submit_malware = st.button('Analyze for Malware')
198
 
199
  if submit_malware:
200
  if uploaded_file is not None:
201
+ text = read_file_content(uploaded_file)
202
  response = detect_malware(text)
203
 
204
  # Parse and display response in a structured way
 
215
 
216
  # Parse and display response in a structured way
217
  display_response_content(response)
218
+