File size: 4,715 Bytes
8797584 28552b3 8797584 28552b3 8797584 28552b3 8797584 28552b3 8797584 28552b3 8797584 28552b3 8797584 28552b3 8797584 28552b3 8797584 5ab8f67 8797584 28552b3 8797584 9c95680 8797584 28552b3 8797584 e28f41a 8797584 f4f4ef5 8797584 28552b3 8797584 |
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 |
import streamlit as st
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from langchain_core.prompts import PromptTemplate
from langchain_huggingface import HuggingFacePipeline
from langchain.agents import create_react_agent, AgentExecutor, Tool
from langchain.memory import ConversationBufferMemory
# Set up the open-source LLM
@st.cache_resource
def load_model():
model_name = "google/flan-t5-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
pipe = pipeline(
"text2text-generation",
model=model,
tokenizer=tokenizer,
max_length=512
)
return HuggingFacePipeline(pipeline=pipe)
local_llm = load_model()
# Function to send an actual email using SMTP
def send_actual_email(to_email, subject, body):
from_email = "[email protected]" # Replace with your email address
from_password = "Nneka@86" # Replace with your email password
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
server = smtplib.SMTP('smtp.gmail.com', 587) # Replace with your SMTP server details
server.starttls()
server.login(from_email, from_password)
text = msg.as_string()
server.sendmail(from_email, to_email, text)
server.quit()
return "Email sent successfully!"
except Exception as e:
return f"Failed to send email: {str(e)}"
# Tool for sending the email
def send_email_tool(name, email, preferred_date):
zoom_link = """
Lawrence Emenike is inviting you to a scheduled Zoom meeting.
Topic: Lawrence Emenike's Zoom Meeting
Time: Aug 10, 2024 05:00 PM West Central Africa
Join Zoom Meeting
https://us04web.zoom.us/j/73793374638?pwd=S0TEJ30da7dhQ8viOdafMzPfCVzoLJ.1
Meeting ID: 737 9337 4638
Passcode: 9cNPkn
"""
subject = "Invitation to Discuss Cybersecurity Program"
body = f"""
Hi {name},
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.
Here are the details:
Preferred Date: {preferred_date}
Zoom Meeting Link: {zoom_link}
Looking forward to our discussion!
Best regards,
Lawrence Emenike
"""
# Send the actual email
result = send_actual_email(email, subject, body)
return result
# Define the tools for the agent
tools = [
Tool(
name="Send Email",
func=send_email_tool,
description="Sends an email with the Zoom meeting details"
)
]
# Define the prompt
prompt = PromptTemplate.from_template(
"""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.
You have access to the following tools:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: Thank you! The invitation has been sent.
Begin!
Question: {input}
Thought: Let's approach this step-by-step:
{agent_scratchpad}"""
)
# Create the React agent
agent = create_react_agent(
llm=local_llm,
tools=tools,
prompt=prompt
)
# Create the agent executor
agent_executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
verbose=True,
memory=ConversationBufferMemory()
)
# Streamlit interface
st.title("AI CyberSecurity Program Advisor")
st.write("The AI will collect your details and send you an invitation for a video call.")
# Starting the conversation
if st.button("Start Conversation"):
initial_message = "Please provide your name, email address, and preferred date for the video call."
with st.spinner("AI is processing..."):
response = agent_executor.invoke({"input": initial_message})
st.write(response['output']) # Display the agent's final response
st.sidebar.title("About")
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.")
|