menikev commited on
Commit
486bb07
·
verified ·
1 Parent(s): 90ac79a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -103
app.py CHANGED
@@ -3,11 +3,17 @@ 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():
@@ -24,131 +30,82 @@ def load_model():
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 with clearer instructions for the LLM
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: The invitation email has been sent successfully.
113
-
114
- If you need to send an email, use the following format exactly:
115
- Action: Send Email
116
- Action Input: {{"name": "<name>", "email": "<email>", "preferred_date": "<date>"}}
117
-
118
- Begin!
119
-
120
- Question: {input}
121
- Thought: Let's approach this step-by-step:
122
  {agent_scratchpad}"""
123
  )
124
 
125
- # Create the React agent
126
- agent = create_react_agent(
127
- llm=local_llm,
128
- tools=tools,
129
- prompt=prompt
130
- )
131
-
132
- # Create the agent executor with handle_parsing_errors=True
133
  agent_executor = AgentExecutor.from_agent_and_tools(
134
- agent=agent,
135
- tools=tools,
136
- verbose=True,
137
- memory=ConversationBufferMemory(),
138
- handle_parsing_errors=True # Added to handle parsing errors
139
  )
140
 
141
  # Streamlit interface
142
- st.title("AI CyberSecurity Program Advisor")
 
 
 
 
 
143
 
144
- st.write("The AI will collect your details and send you an invitation for a video call.")
 
 
145
 
146
- # Starting the conversation
147
- if st.button("Start Conversation"):
148
- initial_message = "Please provide your name, email address, and preferred date for the video call."
149
- with st.spinner("AI is processing..."):
150
- response = agent_executor.invoke({"input": initial_message})
151
- st.write(response['output']) # Display the agent's final response
 
 
 
 
152
 
153
  st.sidebar.title("About")
154
- 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.")
 
 
 
3
  from email.mime.text import MIMEText
4
  from email.mime.multipart import MIMEMultipart
5
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
 
6
  from langchain_huggingface import HuggingFacePipeline
7
  from langchain.agents import create_react_agent, AgentExecutor, Tool
8
+ from langchain.prompts import PromptTemplate
9
  from langchain.memory import ConversationBufferMemory
10
 
11
+ # Email configuration
12
+ SENDER_EMAIL = "[email protected]" # Replace with your email
13
+ SENDER_PASSWORD = "Achuta@86" # Replace with your email password
14
+ SMTP_SERVER = "smtp.gmail.com" # Replace with your SMTP server
15
+ SMTP_PORT = 587 # Replace with your SMTP port
16
+
17
  # Set up the open-source LLM
18
  @st.cache_resource
19
  def load_model():
 
30
 
31
  local_llm = load_model()
32
 
33
+ def send_email(to_email, subject, body):
 
 
 
 
 
 
 
 
 
 
 
34
  try:
35
+ message = MIMEMultipart()
36
+ message["From"] = SENDER_EMAIL
37
+ message["To"] = to_email
38
+ message["Subject"] = subject
39
+ message.attach(MIMEText(body, "plain"))
40
+
41
+ with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
42
+ server.starttls()
43
+ server.login(SENDER_EMAIL, SENDER_PASSWORD)
44
+ server.send_message(message)
45
+ return "Email sent successfully"
46
  except Exception as e:
47
  return f"Failed to send email: {str(e)}"
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  tools = [
50
  Tool(
51
  name="Send Email",
52
+ func=send_email,
53
+ description="Sends an email. Args: to_email, subject, body"
54
  )
55
  ]
56
 
 
57
  prompt = PromptTemplate.from_template(
58
+ """You are a helpful assistant scheduling cybersecurity program meetings.
59
+ Your task is to collect the user's name, email, and preferred meeting date, then send a meeting invitation.
60
 
61
+ Use the following format:
62
+ Human: <user's input>
63
+ Assistant: <your response>
64
+ Human: <user's input>
65
+ Assistant: <your response>
66
+ ...
67
 
68
+ Once you have all the information, use the Send Email tool to send the invitation.
69
+ The email should include this Zoom link: https://us04web.zoom.us/j/73793374638?pwd=S0TEJ30da7dhQ8viOdafMzPfCVzoLJ.1
70
+ And this Meeting ID: 737 9337 4638
71
 
72
+ Begin the conversation by asking for the user's name.
73
 
74
+ {chat_history}
75
+ Human: {input}
76
+ Assistant: Let's proceed step by step:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  {agent_scratchpad}"""
78
  )
79
 
80
+ agent = create_react_agent(local_llm, tools, prompt)
 
 
 
 
 
 
 
81
  agent_executor = AgentExecutor.from_agent_and_tools(
82
+ agent=agent, tools=tools, verbose=True, memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True)
 
 
 
 
83
  )
84
 
85
  # Streamlit interface
86
+ st.title("CyberSecurity Program Meeting Scheduler")
87
+
88
+ st.write("Chat with the AI to schedule your meeting. The AI will ask for your name, email, and preferred meeting date.")
89
+
90
+ if "messages" not in st.session_state:
91
+ st.session_state.messages = []
92
 
93
+ for message in st.session_state.messages:
94
+ with st.chat_message(message["role"]):
95
+ st.markdown(message["content"])
96
 
97
+ if prompt := st.chat_input("Your message"):
98
+ st.session_state.messages.append({"role": "user", "content": prompt})
99
+ with st.chat_message("user"):
100
+ st.markdown(prompt)
101
+
102
+ with st.chat_message("assistant"):
103
+ with st.spinner("Thinking..."):
104
+ response = agent_executor.run(prompt)
105
+ st.markdown(response)
106
+ st.session_state.messages.append({"role": "assistant", "content": response})
107
 
108
  st.sidebar.title("About")
109
+ st.sidebar.info("This is an interactive CyberSecurity Program Meeting Scheduler. Chat with the AI to schedule your meeting. It will collect your information and send a meeting invitation via email.")
110
+
111
+ # To run this script, use: streamlit run your_script_name.py