heysho commited on
Commit
c6539e3
1 Parent(s): 2451b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -96
app.py CHANGED
@@ -1,111 +1,71 @@
1
  import streamlit as st
2
  import openai
3
 
4
- ai_model = "gpt-4-turbo"
5
- token = 4096
 
 
6
 
7
  # Set the page title and favicon
8
  st.set_page_config(page_title="Daily Reminder", page_icon=":bar_chart:")
9
 
 
10
  openai.api_key = st.secrets['OPENAI_API_KEY']
11
- prompt = st.secrets['PROMPT_DAILY_REMINDER']
12
 
 
13
  st.title('Daily Reminder')
14
 
 
15
  if 'usage_count' not in st.session_state:
16
- st.session_state['usage_count'] = 0 # Usage counter
17
 
18
- max_uses = 3
19
-
20
- if st.session_state['usage_count'] < max_uses:
21
- # Main Contents Start from here -------------------------------
22
-
23
- st.subheader('English')
24
- en_input_topic = st.selectbox(
25
- "How are you feeling today?",
26
- ("I want to be more motivated", "I'm feeling sad", "I'm angry", "I just want to relax today"),
27
- index=0, # Setting a default index if desired, or use None for no default selection
28
- key="en_input_topic"
29
- )
30
-
31
- en_input_reason = st.text_input(
32
- "What's the reason for this feeling? (e.g., 'I lost my motivation')",
33
- key="en_input_reason"
34
- )
35
-
36
- en_input_plan = st.text_input(
37
- "What's your plan for today? (e.g., 'I'm going to meet my friends.')",
38
- key="en_input_plan"
39
- )
40
-
41
- if st.button("Generate a daily reminder", key="en_generate_quote"):
42
- st.session_state['usage_count'] += 1 # Increment the usage counter
43
- # Create a prompt based on the user input
44
- en_prompt = f"""
45
- - Task: {prompt}.
46
- - Output language: Japanese.
47
- - Feeling: {en_input_topic}.
48
- - Reason for the feeling: {en_input_reason}.
49
- - Plan for the day: {en_input_plan}.
50
- """
51
- # Make a request to the API to generate text
52
- en_response = openai.ChatCompletion.create(
53
- model=ai_model, # Use the engine of your choice
54
- messages=[{"role": "user", "content": en_prompt}],
55
- max_tokens=token
56
- )
57
- st.write(en_response["choices"][0]["message"]["content"])
58
-
59
- st.text(" ")
60
- st.text(" ")
61
-
62
-
63
- st.subheader('日本語')
64
- ja_input_topic = st.selectbox(
65
- "今日の気分を教えてください?",
66
- ("モチベーションを上げたい", "悲しい", "怒っている", "今はリラックスしたい"),
67
- index=0, # Setting a default index if desired, or use None for no default selection
68
- key="ja_input_topic"
69
- )
70
-
71
- ja_input_reason = st.text_input(
72
- "その理由は何ですか?(例:「モチベーションが上がらない」)",
73
- key="ja_input_reason"
74
- )
75
-
76
- ja_input_plan = st.text_input(
77
- "今日の予定は何ですか?(例:「友達と会う予定です」)",
78
- key="ja_input_plan"
79
- )
80
-
81
- if st.button("今日の名言を生成する", key="ja_generate_quote"):
82
  st.session_state['usage_count'] += 1 # Increment the usage counter
83
- # Create a prompt based on the user input
84
- ja_prompt = f"""
85
- - Task: {prompt}
86
- - Output language: Japanese.
87
- - Feeling: {ja_input_topic}.
88
- - Reason for the feeling: {ja_input_reason}.
89
- - Plan for the day: {ja_input_plan}.
90
  """
91
- # Make a request to the API to generate text
92
- ja_response = openai.ChatCompletion.create(
93
- model=ai_model, # Use the engine of your choice
94
- messages=[{"role": "user", "content": ja_prompt}],
95
- max_tokens=token
96
- )
97
- st.write(ja_response["choices"][0]["message"]["content"])
98
- else:
99
- st.error("You have reached your maximum usage limit.")
100
-
101
- st.markdown(
102
- """
103
- <style>
104
- /* ---- Padding on header ---- */
105
- .st-emotion-cache-iiif1v {
106
- display: none !important;
107
- }
108
- </style>
109
- """,
110
- unsafe_allow_html=True,
111
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import openai
3
 
4
+ # Constants
5
+ AI_MODEL = "gpt-4-turbo"
6
+ TOKEN_COUNT = 4096
7
+ MAX_USES = 3
8
 
9
  # Set the page title and favicon
10
  st.set_page_config(page_title="Daily Reminder", page_icon=":bar_chart:")
11
 
12
+ # Load API key from secrets
13
  openai.api_key = st.secrets['OPENAI_API_KEY']
14
+ prompt_template = st.secrets['PROMPT_DAILY_REMINDER']
15
 
16
+ # Page title
17
  st.title('Daily Reminder')
18
 
19
+ # Initialize or retrieve the usage count from session state
20
  if 'usage_count' not in st.session_state:
21
+ st.session_state['usage_count'] = 0
22
 
23
+ def generate_reminder(language, feeling, reason, plan):
24
+ """Generates a daily reminder based on the user's mood and plans."""
25
+ if st.session_state['usage_count'] < MAX_USES:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  st.session_state['usage_count'] += 1 # Increment the usage counter
27
+ reminder_prompt = f"""
28
+ - Task: {prompt_template}.
29
+ - Output language: {language}.
30
+ - Feeling: {feeling}.
31
+ - Reason for the feeling: {reason}.
32
+ - Plan for the day: {plan}.
 
33
  """
34
+ with st.spinner('Generating your daily reminder...'):
35
+ response = openai.ChatCompletion.create(
36
+ model=AI_MODEL,
37
+ messages=[{"role": "user", "content": reminder_prompt}],
38
+ max_tokens=TOKEN_COUNT
39
+ )
40
+ return response["choices"][0]["message"]["content"]
41
+ else:
42
+ st.error("You have reached your maximum usage limit.")
43
+ return None
44
+
45
+ # Form for English input
46
+ st.subheader('English')
47
+ en_feeling = st.selectbox(
48
+ "How are you feeling today?",
49
+ ("I want to be more motivated", "I'm feeling sad", "I'm angry", "I just want to relax today", "I feel excited", "I'm feeling anxious"),
50
+ key="en_feeling"
51
+ )
52
+ en_reason = st.text_input("What's the reason for this feeling?", key="en_reason")
53
+ en_plan = st.text_input("What's your plan for today?", key="en_plan")
54
+ if st.button("Generate a daily reminder", key="en_generate"):
55
+ result = generate_reminder("English", en_feeling, en_reason, en_plan)
56
+ if result:
57
+ st.write(result)
58
+
59
+ # Form for Japanese input
60
+ st.subheader('日本語')
61
+ ja_feeling = st.selectbox(
62
+ "今日の気分を教えてください?",
63
+ ("モチベーションを上げたい", "悲しい", "怒っている", "今はリラックスしたい", "ワクワクしている", "不安を感じている"),
64
+ key="ja_feeling"
65
+ )
66
+ ja_reason = st.text_input("その理由は何ですか?", key="ja_reason")
67
+ ja_plan = st.text_input("今日の予定は何ですか?", key="ja_plan")
68
+ if st.button("今日の名言を生成する", key="ja_generate"):
69
+ result = generate_reminder("Japanese", ja_feeling, ja_reason, ja_plan)
70
+ if result:
71
+ st.write(result)