Spaces:
Runtime error
Runtime error
2na1ve commited on
Commit ·
de7f917
1
Parent(s): 75fa15e
fist upload
Browse files- .idea/.gitignore +8 -0
- .idea/EmoChatBot.iml +10 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- app.py +53 -0
- requirements.txt +3 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/EmoChatBot.iml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$">
|
| 5 |
+
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
| 6 |
+
</content>
|
| 7 |
+
<orderEntry type="jdk" jdkName="emo_chatbot" jdkType="Python SDK" />
|
| 8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
+
</component>
|
| 10 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="emo_chatbot" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/EmoChatBot.iml" filepath="$PROJECT_DIR$/.idea/EmoChatBot.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
## web api key = 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
|
| 6 |
+
DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant, you should answer the question correctly."
|
| 7 |
+
emotion_classify = pipeline("sentiment-analysis")
|
| 8 |
+
|
| 9 |
+
with st.sidebar:
|
| 10 |
+
openai_api_key = st.text_input("Open AI Key", key="chat_key", type="password")
|
| 11 |
+
"[Generate An OpenAI API key](https://platform.openai.com/account/api-keys)"
|
| 12 |
+
|
| 13 |
+
st.title("Emotion Chatbot")
|
| 14 |
+
if "messages" not in st.session_state:
|
| 15 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you today?"}]
|
| 16 |
+
|
| 17 |
+
for msg in st.session_state.messages:
|
| 18 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 19 |
+
|
| 20 |
+
if user_prompt := st.chat_input():
|
| 21 |
+
if not openai_api_key:
|
| 22 |
+
st.info("Please enter a valid api key to continue")
|
| 23 |
+
st.stop()
|
| 24 |
+
|
| 25 |
+
emo_label = emotion_classify(user_prompt)[0]["label"]
|
| 26 |
+
|
| 27 |
+
if emo_label == 'NEGATIVE':
|
| 28 |
+
print("negative emotion detacted")
|
| 29 |
+
adjusted_system_prompt = (
|
| 30 |
+
f'You are a helpful assistant, you should answer the question correctly,'
|
| 31 |
+
f' also provide some advice for {emo_label} emotion start with "I think you are feeling {emo_label},'
|
| 32 |
+
f'here are some suggestions for you"'
|
| 33 |
+
)
|
| 34 |
+
else:
|
| 35 |
+
adjusted_system_prompt = DEFAULT_SYSTEM_PROMPT
|
| 36 |
+
|
| 37 |
+
openai.api_key = openai_api_key
|
| 38 |
+
st.session_state.messages.append({"role": "user", "content": user_prompt})
|
| 39 |
+
st.chat_message("user").write(user_prompt)
|
| 40 |
+
|
| 41 |
+
# Construct history messages including the adjusted system prompt
|
| 42 |
+
history = [
|
| 43 |
+
{"role": msg["role"], "content": msg["content"]} for msg in st.session_state.messages
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
response = openai.ChatCompletion.create(
|
| 47 |
+
model="gpt-3.5-turbo",
|
| 48 |
+
messages=history
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
response_msg = response.choices[0].message
|
| 52 |
+
st.session_state.messages.append({"role": "assistant", "content": response_msg["content"]})
|
| 53 |
+
st.chat_message("assistant").write(response_msg["content"])
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit~=1.26.0
|
| 2 |
+
openai~=0.27.10
|
| 3 |
+
transformers~=4.32.1
|