menikev commited on
Commit
8797584
·
verified ·
1 Parent(s): 9c95680

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -44
app.py CHANGED
@@ -1,60 +1,148 @@
1
- # Mock function to send an email
2
- def send_email(to_email, subject, body):
3
- # For demo purposes, we'll just print the email details
4
- st.write(f"Email sent to: {to_email}")
5
- st.write(f"Subject: {subject}")
6
- st.write(f"Body: {body}")
7
- return "Email sent successfully"
 
 
8
 
9
- # Streamlit interface
10
- st.title("AI CyberSecurity Program Advisor")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- st.write("Welcome! Please provide your details to schedule a video call to discuss our cybersecurity program.")
 
13
 
14
- # Collect user information
15
- name = st.text_input("Your Name")
16
- email = st.text_input("Your Email Address")
17
- preferred_date = st.date_input("Preferred Date for the Video Call")
 
 
 
18
 
19
- # Zoom meeting details
20
- zoom_link = """
21
- Lawrence Emenike is inviting you to a scheduled Zoom meeting.
22
 
23
- Topic: Lawrence Emenike's Zoom Meeting
24
- Time: Aug 10, 2024 05:00 PM West Central Africa
25
 
26
- Join Zoom Meeting
27
- https://us04web.zoom.us/j/73793374638?pwd=S0TEJ30da7dhQ8viOdafMzPfCVzoLJ.1
28
 
29
- Meeting ID: 737 9337 4638
30
- Passcode: 9cNPkn
31
- """
32
 
33
- if st.button("Schedule Video Call"):
34
- if name and email and preferred_date:
35
- # Compose the email
36
- subject = "Invitation to Discuss Cybersecurity Program"
37
- body = f"""
38
- Hi {name},
39
 
40
- Thank you for your interest in our cybersecurity program. We would like to invite you to a video call to discuss how our program can benefit your organization.
 
 
41
 
42
- Here are the details:
 
 
 
 
 
 
 
43
 
44
- Preferred Date: {preferred_date}
45
- Zoom Meeting Link: {zoom_link}
 
46
 
47
- Looking forward to our discussion!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- Best regards,
50
- Lawrence Emenike
51
- """
52
 
53
- # Send the email
54
- send_email(email, subject, body)
55
- st.success("Your video call has been scheduled, and an email has been sent!")
56
- else:
57
- st.error("Please fill in all the required fields.")
 
58
 
59
  st.sidebar.title("About")
60
- st.sidebar.info("This is a demo of an AI CyberSecurity Program Advisor. It's designed to help you schedule a video call to discuss our cybersecurity program.")
 
1
+ import streamlit as st
2
+ import smtplib
3
+ from email.mime.text import MIMEText
4
+ from email.mime.multipart import MIMEMultipart
5
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
6
+ from langchain_core.prompts import PromptTemplate
7
+ from langchain_huggingface import HuggingFacePipeline
8
+ from langchain.agents import create_react_agent, AgentExecutor, Tool
9
+ from langchain.memory import ConversationBufferMemory
10
 
11
+ # Set up the open-source LLM
12
+ @st.cache_resource
13
+ def load_model():
14
+ model_name = "google/flan-t5-large"
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
16
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
17
+ pipe = pipeline(
18
+ "text2text-generation",
19
+ model=model,
20
+ tokenizer=tokenizer,
21
+ max_length=512
22
+ )
23
+ return HuggingFacePipeline(pipeline=pipe)
24
+
25
+ local_llm = load_model()
26
+
27
+ # Function to send an actual email using SMTP
28
+ def send_actual_email(to_email, subject, body):
29
+ from_email = "[email protected]" # Replace with your email address
30
+ from_password = "Nneka@86" # Replace with your email password
31
+
32
+ msg = MIMEMultipart()
33
+ msg['From'] = from_email
34
+ msg['To'] = to_email
35
+ msg['Subject'] = subject
36
+
37
+ msg.attach(MIMEText(body, 'plain'))
38
+
39
+ try:
40
+ server = smtplib.SMTP('smtp.gmail.com', 587) # Replace with your SMTP server details
41
+ server.starttls()
42
+ server.login(from_email, from_password)
43
+ text = msg.as_string()
44
+ server.sendmail(from_email, to_email, text)
45
+ server.quit()
46
+ return "Email sent successfully!"
47
+ except Exception as e:
48
+ return f"Failed to send email: {str(e)}"
49
+
50
+ # Tool for sending the email
51
+ def send_email_tool(name, email, preferred_date):
52
+ zoom_link = """
53
+ Lawrence Emenike is inviting you to a scheduled Zoom meeting.
54
+
55
+ Topic: Lawrence Emenike's Zoom Meeting
56
+ Time: Aug 10, 2024 05:00 PM West Central Africa
57
 
58
+ Join Zoom Meeting
59
+ https://us04web.zoom.us/j/73793374638?pwd=S0TEJ30da7dhQ8viOdafMzPfCVzoLJ.1
60
 
61
+ Meeting ID: 737 9337 4638
62
+ Passcode: 9cNPkn
63
+ """
64
+
65
+ subject = "Invitation to Discuss Cybersecurity Program"
66
+ body = f"""
67
+ Hi {name},
68
 
69
+ Thank you for your interest in our cybersecurity program. We would like to invite you to a video call to discuss how our program can benefit your organization.
 
 
70
 
71
+ Here are the details:
 
72
 
73
+ Preferred Date: {preferred_date}
74
+ Zoom Meeting Link: {zoom_link}
75
 
76
+ Looking forward to our discussion!
 
 
77
 
78
+ Best regards,
79
+ Lawrence Emenike
80
+ """
 
 
 
81
 
82
+ # Send the actual email
83
+ result = send_actual_email(email, subject, body)
84
+ return result
85
 
86
+ # Define the tools for the agent
87
+ tools = [
88
+ Tool(
89
+ name="Send Email",
90
+ func=send_email_tool,
91
+ description="Sends an email with the Zoom meeting details"
92
+ )
93
+ ]
94
 
95
+ # Define the prompt
96
+ prompt = PromptTemplate.from_template(
97
+ """You are an AI CyberSecurity Program Advisor. Your goal is to engage with the user and collect their name, email address, and preferred date for a video call. Once collected, send an email invitation to a Zoom meeting.
98
 
99
+ You have access to the following tools:
100
+
101
+ {tools}
102
+
103
+ Use the following format:
104
+
105
+ Question: the input question you must answer
106
+ Thought: you should always think about what to do
107
+ Action: the action to take, should be one of [{tool_names}]
108
+ Action Input: the input to the action
109
+ Observation: the result of the action
110
+ ... (this Thought/Action/Action Input/Observation can repeat N times)
111
+ Thought: I now know the final answer
112
+ Final Answer: Thank you! The invitation has been sent.
113
+ Begin!
114
+
115
+ Question: {input}
116
+ Thought: Let's approach this step-by-step:
117
+ {agent_scratchpad}"""
118
+ )
119
+
120
+ # Create the React agent
121
+ agent = create_react_agent(
122
+ llm=local_llm,
123
+ tools=tools,
124
+ prompt=prompt
125
+ )
126
+
127
+ # Create the agent executor
128
+ agent_executor = AgentExecutor.from_agent_and_tools(
129
+ agent=agent,
130
+ tools=tools,
131
+ verbose=True,
132
+ memory=ConversationBufferMemory()
133
+ )
134
+
135
+ # Streamlit interface
136
+ st.title("AI CyberSecurity Program Advisor")
137
 
138
+ st.write("The AI will collect your details and send you an invitation for a video call.")
 
 
139
 
140
+ # Starting the conversation
141
+ if st.button("Start Conversation"):
142
+ initial_message = "Please provide your name, email address, and preferred date for the video call."
143
+ with st.spinner("AI is processing..."):
144
+ response = agent_executor.invoke({"input": initial_message})
145
+ st.write(response['output']) # Display the agent's final response
146
 
147
  st.sidebar.title("About")
148
+ st.sidebar.info("This is a demo of an AI CyberSecurity Program Advisor using LangChain. It helps you schedule a video call and sends an email invitation automatically.")