Update app.py
Browse files
app.py
CHANGED
@@ -8,25 +8,20 @@ from langchain.agents import create_react_agent, AgentExecutor, Tool
|
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
from langchain.memory import ConversationBufferMemory
|
10 |
from langchain.schema import AgentAction, AgentFinish
|
|
|
11 |
|
12 |
# Email configuration
|
13 |
-
SENDER_EMAIL = "[email protected]"
|
14 |
-
SENDER_PASSWORD = "Achuta@86"
|
15 |
-
SMTP_SERVER = "smtp.gmail.com"
|
16 |
-
SMTP_PORT = 587
|
17 |
|
18 |
-
# Set up the open-source LLM
|
19 |
@st.cache_resource
|
20 |
def load_model():
|
21 |
-
model_name = "google/flan-t5-small"
|
22 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
24 |
-
pipe = pipeline(
|
25 |
-
"text2text-generation",
|
26 |
-
model=model,
|
27 |
-
tokenizer=tokenizer,
|
28 |
-
max_length=512
|
29 |
-
)
|
30 |
return HuggingFacePipeline(pipeline=pipe)
|
31 |
|
32 |
local_llm = load_model()
|
@@ -38,7 +33,6 @@ def send_email(to_email, subject, body):
|
|
38 |
message["To"] = to_email
|
39 |
message["Subject"] = subject
|
40 |
message.attach(MIMEText(body, "plain"))
|
41 |
-
|
42 |
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
43 |
server.starttls()
|
44 |
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
@@ -47,41 +41,18 @@ def send_email(to_email, subject, body):
|
|
47 |
except Exception as e:
|
48 |
return f"Failed to send email: {str(e)}"
|
49 |
|
50 |
-
tools = [
|
51 |
-
Tool(
|
52 |
-
name="Send Email",
|
53 |
-
func=send_email,
|
54 |
-
description="Sends an email. Args: to_email, subject, body"
|
55 |
-
)
|
56 |
-
]
|
57 |
|
58 |
prompt = PromptTemplate.from_template(
|
59 |
"""You are a helpful assistant scheduling cybersecurity program meetings.
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
Assistant: <your response>
|
65 |
-
Human: <user's input>
|
66 |
-
Assistant: <your response>
|
67 |
-
...
|
68 |
-
|
69 |
-
Once you have all the information, use the Send Email tool to send the invitation.
|
70 |
-
The email should include this Zoom link: https://us04web.zoom.us/j/73793374638?pwd=S0TEJ30da7dhQ8viOdafMzPfCVzoLJ.1
|
71 |
-
And this Meeting ID: 737 9337 4638
|
72 |
-
|
73 |
-
Begin the conversation by asking for the user's name.
|
74 |
|
75 |
{chat_history}
|
76 |
Human: {input}
|
77 |
-
Assistant:
|
78 |
-
{agent_scratchpad}
|
79 |
-
|
80 |
-
Tools available:
|
81 |
-
{tools}
|
82 |
-
|
83 |
-
Tool names:
|
84 |
-
{tool_names}
|
85 |
"""
|
86 |
)
|
87 |
|
@@ -91,12 +62,12 @@ agent_executor = AgentExecutor.from_agent_and_tools(
|
|
91 |
tools=tools,
|
92 |
verbose=True,
|
93 |
memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
|
94 |
-
handle_parsing_errors=True
|
|
|
|
|
95 |
)
|
96 |
|
97 |
-
# Streamlit interface
|
98 |
st.title("CyberSecurity Program Meeting Scheduler")
|
99 |
-
|
100 |
st.write("Chat with the AI to schedule your meeting. The AI will ask for your name, email, and preferred meeting date.")
|
101 |
|
102 |
if "messages" not in st.session_state:
|
@@ -114,7 +85,8 @@ if prompt := st.chat_input("Your message"):
|
|
114 |
with st.chat_message("assistant"):
|
115 |
with st.spinner("Thinking..."):
|
116 |
try:
|
117 |
-
|
|
|
118 |
if isinstance(response, dict) and "output" in response:
|
119 |
assistant_response = response["output"]
|
120 |
elif isinstance(response, (AgentAction, AgentFinish)):
|
@@ -124,6 +96,8 @@ if prompt := st.chat_input("Your message"):
|
|
124 |
|
125 |
st.markdown(assistant_response)
|
126 |
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
|
|
|
|
127 |
except Exception as e:
|
128 |
st.error(f"An error occurred: {str(e)}")
|
129 |
|
|
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
from langchain.memory import ConversationBufferMemory
|
10 |
from langchain.schema import AgentAction, AgentFinish
|
11 |
+
import time
|
12 |
|
13 |
# Email configuration
|
14 |
+
SENDER_EMAIL = "[email protected]"
|
15 |
+
SENDER_PASSWORD = "Achuta@86"
|
16 |
+
SMTP_SERVER = "smtp.gmail.com"
|
17 |
+
SMTP_PORT = 587
|
18 |
|
|
|
19 |
@st.cache_resource
|
20 |
def load_model():
|
21 |
+
model_name = "google/flan-t5-small"
|
22 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
24 |
+
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer, max_length=512)
|
|
|
|
|
|
|
|
|
|
|
25 |
return HuggingFacePipeline(pipeline=pipe)
|
26 |
|
27 |
local_llm = load_model()
|
|
|
33 |
message["To"] = to_email
|
34 |
message["Subject"] = subject
|
35 |
message.attach(MIMEText(body, "plain"))
|
|
|
36 |
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
37 |
server.starttls()
|
38 |
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
|
|
41 |
except Exception as e:
|
42 |
return f"Failed to send email: {str(e)}"
|
43 |
|
44 |
+
tools = [Tool(name="Send Email", func=send_email, description="Sends an email. Args: to_email, subject, body")]
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
prompt = PromptTemplate.from_template(
|
47 |
"""You are a helpful assistant scheduling cybersecurity program meetings.
|
48 |
+
Collect the user's name, email, and preferred meeting date, then send a meeting invitation.
|
49 |
+
Begin by asking for the user's name if you don't have it.
|
50 |
+
Zoom link: https://us04web.zoom.us/j/73793374638?pwd=S0TEJ30da7dhQ8viOdafMzPfCVzoLJ.1
|
51 |
+
Meeting ID: 737 9337 4638
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
{chat_history}
|
54 |
Human: {input}
|
55 |
+
Assistant: {agent_scratchpad}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
"""
|
57 |
)
|
58 |
|
|
|
62 |
tools=tools,
|
63 |
verbose=True,
|
64 |
memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
|
65 |
+
handle_parsing_errors=True,
|
66 |
+
max_iterations=3, # Limit the number of iterations
|
67 |
+
early_stopping_method="generate" # Stop if the agent starts looping
|
68 |
)
|
69 |
|
|
|
70 |
st.title("CyberSecurity Program Meeting Scheduler")
|
|
|
71 |
st.write("Chat with the AI to schedule your meeting. The AI will ask for your name, email, and preferred meeting date.")
|
72 |
|
73 |
if "messages" not in st.session_state:
|
|
|
85 |
with st.chat_message("assistant"):
|
86 |
with st.spinner("Thinking..."):
|
87 |
try:
|
88 |
+
start_time = time.time()
|
89 |
+
response = agent_executor({"input": prompt}, timeout=10) # 10-second timeout
|
90 |
if isinstance(response, dict) and "output" in response:
|
91 |
assistant_response = response["output"]
|
92 |
elif isinstance(response, (AgentAction, AgentFinish)):
|
|
|
96 |
|
97 |
st.markdown(assistant_response)
|
98 |
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
99 |
+
except TimeoutError:
|
100 |
+
st.error("I apologize, but I'm having trouble processing your request at the moment. Could you please try asking your question again, or rephrase it?")
|
101 |
except Exception as e:
|
102 |
st.error(f"An error occurred: {str(e)}")
|
103 |
|