-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
160 lines (124 loc) · 6.22 KB
/
Copy pathstreamlit_app.py
File metadata and controls
160 lines (124 loc) · 6.22 KB
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
150
151
152
153
154
155
156
157
158
from pathlib import Path
import streamlit as st
from llama_index import VectorStoreIndex, ServiceContext, Document
from llama_index.llms import OpenAI
import openai
from llama_index import SimpleDirectoryReader
import re
from pprint import pprint
from dropbox_sign import \
ApiClient, ApiException, Configuration, apis, models
# Set page layout to wide mode
st.set_page_config(layout="wide", page_icon = './images/logo.png', initial_sidebar_state="collapsed")
# Three columns with different widths for logo and title
col1, col2, col3 = st.columns([3,1,1])
with col1:
st.image('./images/logo.png')
col1, col2, col3 = st.columns([3,1,1])
with col1:
st.title(st.secrets["title"])
# Access the markdown files and display them
def read_markdown_file(markdown_file):
return Path(markdown_file).read_text()
intro_markdown = read_markdown_file("./data/index.md")
st.markdown(intro_markdown, unsafe_allow_html=True)
# Three columns with same widths for centering
col1, col2, col3 = st.columns([1,1,1])
with col2:
st.header('Your Signature Matters!')
# Full Name Input
full_name = st.text_input("Full Name:")
# Basic validation for full name (checks if it's not empty and has at least two words)
if full_name:
names = full_name.split()
if len(names) < 2:
st.warning("Please enter your full name (at least first and last name).")
# Email Input
email = st.text_input("Email:")
# Basic validation for email using a regex pattern
EMAIL_REGEX = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
if email and not re.match(EMAIL_REGEX, email):
st.warning("Please enter a valid email address.")
# If the validation is successful
if full_name and email and len(full_name.split()) >= 2 and re.match(EMAIL_REGEX, email):
# Do something, e.g., save the data, send an email, etc.
if st.button("Sign campaign"):
# Dropbox Sign, Send signature request
configuration = Configuration(
# Configure HTTP basic authorization: api_key
username=st.secrets["dropbox_sign_key"],
)
with ApiClient(configuration) as api_client:
signature_request_api = apis.SignatureRequestApi(api_client)
signer_1 = models.SubSignatureRequestTemplateSigner(
role=st.secrets["signer_role"],
email_address=email,
name=full_name,
)
signing_options = models.SubSigningOptions(
draw=True,
type=True,
upload=True,
phone=False,
default_type="draw",
)
if st.secrets["template_id"]:
template_id = st.secrets["template_id"]
data = models.SignatureRequestSendWithTemplateRequest(
template_ids = [template_id],
subject=st.secrets["email_subject"],
message=st.secrets["email_message"],
signers=[signer_1],
signing_options=signing_options,
test_mode=True,
)
try:
response = signature_request_api.signature_request_send_with_template(data)
pprint(response)
st.success("Petition letter was sent to your email for signature!")
except ApiException as e:
print("Exception when calling Dropbox Sign API: %s\n" % e)
else:
st.error("Template id missing, Please set it in the environment")
st.markdown("---")
#Access the OpenAI key from secrets
openai.api_key = st.secrets["openai_key"]
if "messages" not in st.session_state.keys(): # Initialize the chat messages history
st.session_state.messages = [
{"role": "assistant", "content": st.secrets["bot_intro"], "avatar":"./images/logo.png"}
]
@st.cache_resource(show_spinner=False)
def load_data():
with st.spinner(text=st.secrets["bot_spinner"]):
reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
docs = reader.load_data()
service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt=st.secrets["bot_context"]))
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
return index
index = load_data()
# chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True, system_prompt= st.secrets["bot_context"]")
if "chat_engine" not in st.session_state.keys(): # Initialize the chat engine
st.session_state.chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
for message in st.session_state.messages: # Display the prior chat messages
if message["role"] == "assistant": #Setting logo for the chat for role assistant
with st.chat_message(message["role"],avatar="./images/logo.png"):
st.write(message["content"])
elif message["role"] == "user":
with st.chat_message(message["role"], avatar="🧑💻"):
st.write(message["content"])
# If last message is not from assistant, generate a new response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant",avatar="./images/logo.png"):
with st.spinner("Thinking..."):
response = st.session_state.chat_engine.chat(prompt)
st.write(response.response)
message = {"role": "assistant", "content": response.response}
st.session_state.messages.append(message) # Add response to message history
# Set the sidebar for navigation
st.sidebar.title("Interactive Signature Campaign")
st.sidebar.markdown("Get your own low code Interactive Signature Campaign from [GitHub](https://github.com/abishekmuthian/interactivesignaturecampaign).")
st.sidebar.write("Built by Abishek Muthian.")
st.sidebar.markdown("---")
st.sidebar.markdown("Powered by [Dropbox Sign](https://www.hellosign.com/features).")