File size: 7,683 Bytes
fa84cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd081c5
 
 
 
fa84cfc
 
 
fd081c5
 
 
a17aa9c
 
fd081c5
 
 
 
 
 
fa84cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd081c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa84cfc
fd081c5
 
 
 
 
 
fa84cfc
fd081c5
 
 
 
fa84cfc
fd081c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa84cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# import streamlit as st
# import google.generativeai as genai
# import os
# import PyPDF2 as pdf
# from dotenv import load_dotenv

# load_dotenv()

# genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))

# #alternative key
# #genai.configure(api_key=("AIzaSyAr3d_7fp0wMxuUrnf_tATknu_TRPKDdxg"))

# # gemini function for general content generation
# def get_gemini_response(input):
#     model = genai.GenerativeModel('gemini-pro')
#     response = model.generate_content(input)
#     return response

# # convert pdf to text
# def input_pdf_text(uploaded_file):
#     reader = pdf.PdfReader(uploaded_file)
#     text = ""
#     for page in range(len(reader.pages)):
#         page = reader.pages[page]
#         text += str(page.extract_text())
#     return text

# # malware detection function
# def detect_malware(input_text):
#     malware_prompt = f"""
#     ### As a cybersecurity expert, your task is to analyze the following text for any indications of malware.
#     ### Text:
#     {input_text}
#     ### Analysis Output:
#     1. Identify any potential malware-related content.
#     2. Explain the reasoning behind your identification.
#     3. Provide recommendations for mitigating any identified risks.
#     """
#     response = get_gemini_response(malware_prompt)
#     return response

# # chatbot function
# def chatbot_response(user_input):
#     chatbot_prompt = f"""
#     ### You are an intelligent and friendly chatbot. Engage in a meaningful conversation with the user.
#     ### User Input:
#     {user_input}
#     ### Chatbot Response:
#     """
#     response = get_gemini_response(chatbot_prompt)
#     return response

# # Function to parse and display response content
# def display_response_content(response):
#     st.subheader("Response Output")
#     if response and response.candidates:
#         response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
#         sections = response_content.split('###')
#         for section in sections:
#             if section.strip():
#                 section_lines = section.split('\n')
#                 section_title = section_lines[0].strip()
#                 section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
#                 if section_title:
#                     st.markdown(f"**{section_title}**")
#                 if section_body:
#                     st.write(section_body)
#     else:
#         st.write("No response received from the model.")

# ## Streamlit App
# st.title("AI-Powered Security and Chatbot System")
# st.text("Use the AI system for malware detection and Awaring yourself.")

# # Tabs for different functionalities
# tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])

# with tab1:
#     st.header("Malware Detection")
#     uploaded_file = st.file_uploader("Upload a file for malware detection", type="pdf", help="Please upload a PDF file.")
#     submit_malware = st.button('Analyze for Malware')
    
#     if submit_malware:
#         if uploaded_file is not None:
#             text = input_pdf_text(uploaded_file)
#             response = detect_malware(text)
            
#             # Parse and display response in a structured way
#             display_response_content(response)

# with tab2:
#     st.header("Chatbot")
#     user_input = st.text_input("Type your message here")
#     submit_chat = st.button('Send')

#     if submit_chat:
#         if user_input:
#             response = chatbot_response(user_input)
            
#             # Parse and display response in a structured way
#             display_response_content(response)



import streamlit as st
import google.generativeai as genai
import os
from dotenv import load_dotenv
import PyPDF2 as pdf
import docx
import chardet

load_dotenv()

genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))

# gemini function for general content generation
def get_gemini_response(input):
    model = genai.GenerativeModel('gemini-pro')
    response = model.generate_content(input)
    return response

# Function to read text from different file types
def read_file_content(uploaded_file):
    file_type = uploaded_file.type
    if file_type == "application/pdf":
        reader = pdf.PdfReader(uploaded_file)
        text = ""
        for page in range(len(reader.pages)):
            page = reader.pages[page]
            text += str(page.extract_text())
    elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        doc = docx.Document(uploaded_file)
        text = '\n'.join([para.text for para in doc.paragraphs])
    else:
        # For other file types, assume it's a text file and try to read it as text
        text = uploaded_file.read()
        result = chardet.detect(text)
        text = text.decode(result['encoding'])

    return text

# malware detection function
def detect_malware(input_text):
    malware_prompt = f"""
    ### As a cybersecurity expert, your task is to analyze the following text for any indications of malware.
    ### Text:
    {input_text}
    ### Analysis Output:
    1. Identify any potential malware-related content.
    2. Explain the reasoning behind your identification.
    3. Provide recommendations for mitigating any identified risks.
    """
    response = get_gemini_response(malware_prompt)
    return response

# chatbot function
def chatbot_response(user_input):
    chatbot_prompt = f"""
    ### You are an intelligent and friendly chatbot. Engage in a meaningful conversation with the user.
    ### User Input:
    {user_input}
    ### Chatbot Response:
    """
    response = get_gemini_response(chatbot_prompt)
    return response

# Function to parse and display response content
def display_response_content(response):
    st.subheader("Response Output")
    if response and response.candidates:
        response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
        sections = response_content.split('###')
        for section in sections:
            if section.strip():
                section_lines = section.split('\n')
                section_title = section_lines[0].strip()
                section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
                if section_title:
                    st.markdown(f"**{section_title}**")
                if section_body:
                    st.write(section_body)
    else:
        st.write("No response received from the model.")

## Streamlit App
st.title("AI-Powered Security and Chatbot System")
st.text("Use the AI system for malware detection and friendly conversation.")

# Tabs for different functionalities
tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])

with tab1:
    st.header("Malware Detection")
    uploaded_file = st.file_uploader("Upload a file for malware detection", type=None, help="Please upload a file of any type.")
    submit_malware = st.button('Analyze for Malware')
    
    if submit_malware:
        if uploaded_file is not None:
            text = read_file_content(uploaded_file)
            response = detect_malware(text)
            
            # Parse and display response in a structured way
            display_response_content(response)

with tab2:
    st.header("Chatbot")
    user_input = st.text_input("Type your message here")
    submit_chat = st.button('Send')

    if submit_chat:
        if user_input:
            response = chatbot_response(user_input)
            
            # Parse and display response in a structured way
            display_response_content(response)