Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,99 @@
|
|
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=("
|
10 |
-
|
11 |
-
# gemini function for general content generation
|
12 |
-
def get_gemini_response(input):
|
13 |
-
model = genai.GenerativeModel('gemini-pro')
|
14 |
-
response = model.generate_content(input)
|
15 |
-
return response
|
16 |
-
|
17 |
-
# convert pdf to text
|
18 |
-
def input_pdf_text(uploaded_file):
|
19 |
-
reader = pdf.PdfReader(uploaded_file)
|
20 |
-
text = ""
|
21 |
-
for page in range(len(reader.pages)):
|
22 |
-
page = reader.pages[page]
|
23 |
-
text += str(page.extract_text())
|
24 |
-
return text
|
25 |
-
|
26 |
-
# malware detection function
|
27 |
-
def detect_malware(input_text):
|
28 |
-
malware_prompt = f"""
|
29 |
-
### As a cybersecurity expert, your task is to analyze the following text for any indications of malware.
|
30 |
-
### Text:
|
31 |
-
{input_text}
|
32 |
-
### Analysis Output:
|
33 |
-
1. Identify any potential malware-related content.
|
34 |
-
2. Explain the reasoning behind your identification.
|
35 |
-
3. Provide recommendations for mitigating any identified risks.
|
36 |
-
"""
|
37 |
-
response = get_gemini_response(malware_prompt)
|
38 |
-
return response
|
39 |
-
|
40 |
-
# chatbot function
|
41 |
-
def chatbot_response(user_input):
|
42 |
-
chatbot_prompt = f"""
|
43 |
-
### You are an intelligent and friendly chatbot. Engage in a meaningful conversation with the user.
|
44 |
-
### User Input:
|
45 |
-
{user_input}
|
46 |
-
### Chatbot Response:
|
47 |
-
"""
|
48 |
-
response = get_gemini_response(chatbot_prompt)
|
49 |
-
return response
|
50 |
-
|
51 |
-
# Function to parse and display response content
|
52 |
-
def display_response_content(response):
|
53 |
-
st.subheader("Response Output")
|
54 |
-
if response and response.candidates:
|
55 |
-
response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
|
56 |
-
sections = response_content.split('###')
|
57 |
-
for section in sections:
|
58 |
-
if section.strip():
|
59 |
-
section_lines = section.split('\n')
|
60 |
-
section_title = section_lines[0].strip()
|
61 |
-
section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
|
62 |
-
if section_title:
|
63 |
-
st.markdown(f"**{section_title}**")
|
64 |
-
if section_body:
|
65 |
-
st.write(section_body)
|
66 |
-
else:
|
67 |
-
st.write("No response received from the model.")
|
68 |
-
|
69 |
-
## Streamlit App
|
70 |
-
st.title("AI-Powered Security and Chatbot System")
|
71 |
-
st.text("Use the AI system for malware detection and Awaring yourself.")
|
72 |
-
|
73 |
-
# Tabs for different functionalities
|
74 |
-
tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])
|
75 |
-
|
76 |
-
with tab1:
|
77 |
-
st.header("Malware Detection")
|
78 |
-
uploaded_file = st.file_uploader("Upload a file for malware detection", type="pdf", help="Please upload a PDF file.")
|
79 |
-
submit_malware = st.button('Analyze for Malware')
|
80 |
-
|
81 |
-
if submit_malware:
|
82 |
-
if uploaded_file is not None:
|
83 |
-
text = input_pdf_text(uploaded_file)
|
84 |
-
response = detect_malware(text)
|
85 |
-
|
86 |
-
# Parse and display response in a structured way
|
87 |
-
display_response_content(response)
|
88 |
-
|
89 |
-
with tab2:
|
90 |
-
st.header("Chatbot")
|
91 |
-
user_input = st.text_input("Type your message here")
|
92 |
-
submit_chat = st.button('Send')
|
93 |
-
|
94 |
-
if submit_chat:
|
95 |
-
if user_input:
|
96 |
-
response = chatbot_response(user_input)
|
97 |
-
|
98 |
-
# Parse and display response in a structured way
|
99 |
-
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=("AIzaSyAr3d_7fp0wMxuUrnf_tATknu_TRPKDdxg"))
|
10 |
+
|
11 |
+
# gemini function for general content generation
|
12 |
+
def get_gemini_response(input):
|
13 |
+
model = genai.GenerativeModel('gemini-pro')
|
14 |
+
response = model.generate_content(input)
|
15 |
+
return response
|
16 |
+
|
17 |
+
# convert pdf to text
|
18 |
+
def input_pdf_text(uploaded_file):
|
19 |
+
reader = pdf.PdfReader(uploaded_file)
|
20 |
+
text = ""
|
21 |
+
for page in range(len(reader.pages)):
|
22 |
+
page = reader.pages[page]
|
23 |
+
text += str(page.extract_text())
|
24 |
+
return text
|
25 |
+
|
26 |
+
# malware detection function
|
27 |
+
def detect_malware(input_text):
|
28 |
+
malware_prompt = f"""
|
29 |
+
### As a cybersecurity expert, your task is to analyze the following text for any indications of malware.
|
30 |
+
### Text:
|
31 |
+
{input_text}
|
32 |
+
### Analysis Output:
|
33 |
+
1. Identify any potential malware-related content.
|
34 |
+
2. Explain the reasoning behind your identification.
|
35 |
+
3. Provide recommendations for mitigating any identified risks.
|
36 |
+
"""
|
37 |
+
response = get_gemini_response(malware_prompt)
|
38 |
+
return response
|
39 |
+
|
40 |
+
# chatbot function
|
41 |
+
def chatbot_response(user_input):
|
42 |
+
chatbot_prompt = f"""
|
43 |
+
### You are an intelligent and friendly chatbot. Engage in a meaningful conversation with the user.
|
44 |
+
### User Input:
|
45 |
+
{user_input}
|
46 |
+
### Chatbot Response:
|
47 |
+
"""
|
48 |
+
response = get_gemini_response(chatbot_prompt)
|
49 |
+
return response
|
50 |
+
|
51 |
+
# Function to parse and display response content
|
52 |
+
def display_response_content(response):
|
53 |
+
st.subheader("Response Output")
|
54 |
+
if response and response.candidates:
|
55 |
+
response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
|
56 |
+
sections = response_content.split('###')
|
57 |
+
for section in sections:
|
58 |
+
if section.strip():
|
59 |
+
section_lines = section.split('\n')
|
60 |
+
section_title = section_lines[0].strip()
|
61 |
+
section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
|
62 |
+
if section_title:
|
63 |
+
st.markdown(f"**{section_title}**")
|
64 |
+
if section_body:
|
65 |
+
st.write(section_body)
|
66 |
+
else:
|
67 |
+
st.write("No response received from the model.")
|
68 |
+
|
69 |
+
## Streamlit App
|
70 |
+
st.title("AI-Powered Security and Chatbot System")
|
71 |
+
st.text("Use the AI system for malware detection and Awaring yourself.")
|
72 |
+
|
73 |
+
# Tabs for different functionalities
|
74 |
+
tab1, tab2 = st.tabs(["Malware Detection", "Chatbot"])
|
75 |
+
|
76 |
+
with tab1:
|
77 |
+
st.header("Malware Detection")
|
78 |
+
uploaded_file = st.file_uploader("Upload a file for malware detection", type="pdf", help="Please upload a PDF file.")
|
79 |
+
submit_malware = st.button('Analyze for Malware')
|
80 |
+
|
81 |
+
if submit_malware:
|
82 |
+
if uploaded_file is not None:
|
83 |
+
text = input_pdf_text(uploaded_file)
|
84 |
+
response = detect_malware(text)
|
85 |
+
|
86 |
+
# Parse and display response in a structured way
|
87 |
+
display_response_content(response)
|
88 |
+
|
89 |
+
with tab2:
|
90 |
+
st.header("Chatbot")
|
91 |
+
user_input = st.text_input("Type your message here")
|
92 |
+
submit_chat = st.button('Send')
|
93 |
+
|
94 |
+
if submit_chat:
|
95 |
+
if user_input:
|
96 |
+
response = chatbot_response(user_input)
|
97 |
+
|
98 |
+
# Parse and display response in a structured way
|
99 |
+
display_response_content(response)
|