-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (43 loc) · 2.43 KB
/
app.py
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
import streamlit as st
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
from dotenv import load_dotenv
load_dotenv()
try:
with open("style.css") as css_file:
st.markdown(f"<style>{css_file.read()}</style>", unsafe_allow_html=True)
except FileNotFoundError:
st.warning("The style.css file was not found. Ensure it exists in the correct path.")
def ask_question(question):
try:
initial_prompt = f"""You are an experienced Cognitive Behavioral Therapy (CBT) therapist.
A client has shared the following belief: "{question}".
Please provide 5 alternative, healthier beliefs in a confident, affirmative form, each one written as a statement, not a question.
Respond in the same language as the question and avoid using question marks."""
response = client.chat.completions.create(model="gpt-4",
messages=[{"role": "system", "content": initial_prompt}],
max_tokens=300)
answer = response.choices[0].message.content.strip()
return answer
except Exception as e:
return f"An error occurred: {str(e)}"
st.markdown("<h1 style='text-align: center;'>Dark thought? Share it with me!</h1>", unsafe_allow_html=True)
st.markdown("<p class='minimal-spacing'>This app functions as an experienced</p>", unsafe_allow_html=True)
st.markdown("<p class='minimal-spacing'>Cognitive Behavioral Therapist</p>", unsafe_allow_html=True)
st.markdown("<p class='minimal-spacing'>Share your troubles and enjoy alternative perspectives</p>", unsafe_allow_html=True)
st.subheader("")
st.markdown("<h3 style='text-align: center;'>Enter your belief here</h3>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>Feel free to use your native language</h3>", unsafe_allow_html=True)
question = st.text_input("Enter your belief here", label_visibility="collapsed", placeholder="")
if st.button("Get support"):
if question:
answer = ask_question(question)
st.subheader("")
st.subheader("Here are some alternative thoughts:")
st.write(answer)
st.subheader("")
st.markdown("<h3 style='text-align: center;'>Which of these alternative thoughts supports you more on your journey?</h3>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>Repeate it one more time to support yourself!</h3>", unsafe_allow_html=True)
else:
st.write("Please enter a valid belief")