menikev's picture
Update app.py
8797584 verified
raw
history blame
4.72 kB
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.")