Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- License.md +21 -0
- Procfile +1 -0
- README.md +56 -12
- app.py +132 -0
- requirements.txt +4 -0
- setup.sh +8 -0
License.md
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 DjPapzin
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: sh setup.sh && streamlit run app.py
|
README.md
CHANGED
@@ -1,12 +1,56 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AI Survey Bot Recommendation
|
2 |
+
|
3 |
+
This is a Streamlit app that allows users to answer a couple of questions and get a tailor-made response from an AI chatbot. The chatbot uses OpenAI GPT-3.5 Turbo to generate responses based on the user's input.
|
4 |
+
|
5 |
+
## Live Demo
|
6 |
+
|
7 |
+
You can test the live application here: [AI Survey Bot Recommendation](https://ai-survey-bot-recommendation-9e6213040391.herokuapp.com/)
|
8 |
+
|
9 |
+
## Key files
|
10 |
+
|
11 |
+
- **app.py** - Main Streamlit app code
|
12 |
+
- **requirements.txt** - Python dependencies
|
13 |
+
- **setup.sh** - Heroku config script
|
14 |
+
|
15 |
+
## Installation
|
16 |
+
|
17 |
+
1. Clone the repository:
|
18 |
+
|
19 |
+
```bash
|
20 |
+
git clone https://github.com/your-username/your-repo.git
|
21 |
+
```
|
22 |
+
|
23 |
+
2. Install the dependencies:
|
24 |
+
|
25 |
+
```bash
|
26 |
+
pip install -r requirements.txt
|
27 |
+
```
|
28 |
+
|
29 |
+
3. Set up the environment variables:
|
30 |
+
|
31 |
+
- Create a `.env` file in the root directory of the project.
|
32 |
+
- Add the following variables to the `.env` file:
|
33 |
+
|
34 |
+
```plaintext
|
35 |
+
OPENAI_API_KEY=your-openai-api-key
|
36 |
+
EMAIL=your-email
|
37 |
+
PASSWORD=your-password
|
38 |
+
```
|
39 |
+
|
40 |
+
4. Run the app:
|
41 |
+
|
42 |
+
```bash
|
43 |
+
streamlit run app.py
|
44 |
+
```
|
45 |
+
|
46 |
+
## Usage
|
47 |
+
|
48 |
+
1. Open the app in your browser.
|
49 |
+
2. Answer the questions presented on the app.
|
50 |
+
3. Click the "Submit" button to get the chatbot response.
|
51 |
+
4. The chatbot response will be displayed on the app.
|
52 |
+
5. The chatbot response will also be sent to the email address provided.
|
53 |
+
|
54 |
+
## License
|
55 |
+
|
56 |
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.
|
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
3 |
+
from langchain.schema import SystemMessage, HumanMessage
|
4 |
+
import openai
|
5 |
+
import smtplib
|
6 |
+
from email.mime.text import MIMEText
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
# Load environment variables from .env file
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# Set up OpenAI API credentials
|
14 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
15 |
+
email = os.getenv('EMAIL')
|
16 |
+
password = os.getenv('PASSWORD')
|
17 |
+
|
18 |
+
def main():
|
19 |
+
st.title("AI Survey Bot Recommendation")
|
20 |
+
st.write("**Answer a couple of questions to get a tailor-made response.**")
|
21 |
+
|
22 |
+
# Question 1
|
23 |
+
name = st.text_input("**Question 1: What is your name?**")
|
24 |
+
|
25 |
+
# Question 2
|
26 |
+
company_name = st.text_input("**Question 2: What is your company name?**")
|
27 |
+
|
28 |
+
# Question 3
|
29 |
+
company_location = st.text_input("**Question 3: Where is your company located?**")
|
30 |
+
|
31 |
+
# Question 4
|
32 |
+
st.write("**Question 4: Are any of these a problem in your business?**")
|
33 |
+
# Create a list of problems
|
34 |
+
problems = [
|
35 |
+
"**Getting leads**",
|
36 |
+
"**Closing sales**",
|
37 |
+
"**Retaining customers**",
|
38 |
+
"**Finding the right talent**",
|
39 |
+
"**Not having enough time**",
|
40 |
+
"**Customer support**",
|
41 |
+
"**Strategic thinking**",
|
42 |
+
"**Other**"
|
43 |
+
]
|
44 |
+
# Create two columns with equal width
|
45 |
+
col1, col2 = st.columns(2)
|
46 |
+
# Loop through the problems and create checkboxes in each column
|
47 |
+
for i, problem in enumerate(problems):
|
48 |
+
# Use the modulo operator to alternate between columns
|
49 |
+
if i % 2 == 0:
|
50 |
+
col1.checkbox(label=problem, key=problem)
|
51 |
+
else:
|
52 |
+
col2.checkbox(label=problem, key=problem)
|
53 |
+
# If Other is selected, prompt the user for more explanation
|
54 |
+
other_problem = ""
|
55 |
+
if st.session_state.get("Other"):
|
56 |
+
other_problem = st.text_input("**Can you give a further explanation of the problem?**")
|
57 |
+
|
58 |
+
# Question 5
|
59 |
+
time_consumers = st.text_area("**Question 5: What are the three biggest time consumers or deficiencies of your business?**")
|
60 |
+
|
61 |
+
# Question 6
|
62 |
+
strategy_struggles = st.text_area("**Question 6: When coming up with strategy, what are the struggles there?**")
|
63 |
+
|
64 |
+
# Question 7
|
65 |
+
email = st.text_input("**Question 7: Enter your Email to get the custom answers sent to you**")
|
66 |
+
|
67 |
+
# Submit button
|
68 |
+
if st.button("Submit"):
|
69 |
+
# Save the survey data and send it to the user
|
70 |
+
send_survey_results(name, company_name, company_location, problems, other_problem, time_consumers, strategy_struggles, email)
|
71 |
+
|
72 |
+
def send_survey_results(name, company_name, company_location, problems, other_problem, time_consumers, strategy_struggles, email):
|
73 |
+
# Generate chatbot response using OpenAI GPT-3.5 Turbo
|
74 |
+
system_message_template = SystemMessagePromptTemplate.from_template(
|
75 |
+
template="You are a helpful assistant that recommends AI tools based on user's business needs."
|
76 |
+
)
|
77 |
+
human_message_template = HumanMessagePromptTemplate.from_template(template="{text}")
|
78 |
+
|
79 |
+
chat_prompt = ChatPromptTemplate.from_messages([system_message_template, human_message_template])
|
80 |
+
|
81 |
+
messages = chat_prompt.format_prompt(text=f"I am {name}, representing {company_name} located in {company_location}. We are facing the following problems in our business: {', '.join(problems)}. {other_problem}. The three biggest time consumers or deficiencies in our business are: {time_consumers}. When coming up with strategy, we struggle with: {strategy_struggles}.").to_messages()
|
82 |
+
|
83 |
+
messages_dict = []
|
84 |
+
for message in messages:
|
85 |
+
if isinstance(message, SystemMessage):
|
86 |
+
messages_dict.append({"role": "system", "content": message.content})
|
87 |
+
elif isinstance(message, HumanMessage):
|
88 |
+
messages_dict.append({"role": "user", "content": message.content})
|
89 |
+
|
90 |
+
response = openai.ChatCompletion.create(
|
91 |
+
model="gpt-3.5-turbo",
|
92 |
+
messages=messages_dict,
|
93 |
+
max_tokens=100,
|
94 |
+
n=1,
|
95 |
+
stop=None,
|
96 |
+
temperature=0.7,
|
97 |
+
top_p=1.0,
|
98 |
+
frequency_penalty=0.0,
|
99 |
+
presence_penalty=0.0
|
100 |
+
)
|
101 |
+
|
102 |
+
# Extract the chatbot response
|
103 |
+
chatbot_response = response.choices[0].message.content.strip()
|
104 |
+
|
105 |
+
# Display the chatbot response
|
106 |
+
st.subheader("Chatbot Response")
|
107 |
+
st.write(chatbot_response)
|
108 |
+
|
109 |
+
# Send the survey results to the user via email
|
110 |
+
send_email(email, chatbot_response)
|
111 |
+
|
112 |
+
def send_email(email, message):
|
113 |
+
# Set up the email parameters
|
114 |
+
sender = "[email protected]"
|
115 |
+
receiver = email
|
116 |
+
subject = "Chatbot Response"
|
117 |
+
body = message
|
118 |
+
|
119 |
+
# Create the email message
|
120 |
+
email_message = MIMEText(body)
|
121 |
+
email_message["Subject"] = subject
|
122 |
+
email_message["From"] = sender
|
123 |
+
email_message["To"] = receiver
|
124 |
+
|
125 |
+
# Send the email
|
126 |
+
with smtplib.SMTP("smtp.gmail.com", 587) as server:
|
127 |
+
server.starttls()
|
128 |
+
server.login(email, password)
|
129 |
+
server.sendmail(sender, receiver, email_message.as_string())
|
130 |
+
|
131 |
+
if __name__ == "__main__":
|
132 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
python-dotenv
|
3 |
+
langchain
|
4 |
+
openai
|
setup.sh
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mkdir -p ~/.streamlit/
|
2 |
+
|
3 |
+
echo "\
|
4 |
+
[server]\n\
|
5 |
+
headless = true\n\
|
6 |
+
enableCORS=false\n\
|
7 |
+
port = $PORT\n\
|
8 |
+
" > ~/.streamlit/config.toml
|