Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,59 +11,67 @@ prompt = st.secrets['PROMPT_EMAIL_REVIEW_GENERATOR']
|
|
| 11 |
# Title of the page
|
| 12 |
st.title('Email Reply Generator')
|
| 13 |
|
| 14 |
-
# Initialize or retrieve the usage count from session state
|
| 15 |
if 'usage_count' not in st.session_state:
|
| 16 |
st.session_state['usage_count'] = 0
|
|
|
|
|
|
|
| 17 |
|
| 18 |
max_uses = 3
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
en_input_sender = st.text_input("Sender (e.g., boss, client, Co-worker,etc.)", key="en_input_sender")
|
| 38 |
-
en_input_subject = st.text_input("Email Subject (e.g., About scheduling a meeting)", key="en_input_subject")
|
| 39 |
-
en_input_message = st.text_area("Content of the recipient's email: (e.g., I would like to adjust the time for tomorrow's meeting, are you available in the afternoon?)", key="en_input_message")
|
| 40 |
-
en_input_reply = st.text_input("What you want to say: (e.g., I am available after 2 PM.)", key="en_input_reply")
|
| 41 |
-
en_input_length = st.text_input("Number of characters: (e.g., Within 100 characters)", key="en_input_length")
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
submit_en = st.form_submit_button("Generate an Email Reply")
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if submit_en:
|
| 49 |
-
result = generate_reply(en_input_sender, en_input_subject, en_input_message,en_input_reply,en_input_length, "English")
|
| 50 |
-
st.write(result)
|
| 51 |
-
|
| 52 |
-
# Japanese Input Section
|
| 53 |
-
with st.form(key='ja_form'):
|
| 54 |
-
st.subheader('日本語')
|
| 55 |
-
ja_input_sender = st.text_input("送信主(例:上司、クライアント、同僚、など)", key="ja_input_sender")
|
| 56 |
-
ja_input_subject = st.text_input("メールの件名 (例:ミーティングの日程調整について)", key="ja_input_subject")
|
| 57 |
-
ja_input_message = st.text_area("相手のメールの内容: (例:明日、ミーティングの時間を調整したいのですが、午後の時間帯は空いていますか?)", key="ja_input_message")
|
| 58 |
-
ja_input_reply = st.text_input("伝えたいこと: (例:午後2時以降は空いています。)", key="ja_input_reply")
|
| 59 |
-
ja_input_length = st.text_input("文字数: (例:100文字以内)", key="ja_input_length")
|
| 60 |
-
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
else:
|
| 68 |
st.error("You have reached your maximum usage limit.")
|
| 69 |
|
|
|
|
| 11 |
# Title of the page
|
| 12 |
st.title('Email Reply Generator')
|
| 13 |
|
| 14 |
+
# Initialize or retrieve the usage count and language from session state
|
| 15 |
if 'usage_count' not in st.session_state:
|
| 16 |
st.session_state['usage_count'] = 0
|
| 17 |
+
if 'language' not in st.session_state:
|
| 18 |
+
st.session_state['language'] = 'English'
|
| 19 |
|
| 20 |
max_uses = 3
|
| 21 |
|
| 22 |
+
# Define a function to handle API requests and increase modularity
|
| 23 |
+
def generate_reply(input_sender, input_subject, input_message, input_reply, language):
|
| 24 |
+
st.session_state['usage_count'] += 1
|
| 25 |
+
with st.spinner('Loading... Please wait.'):
|
| 26 |
+
user_prompt = f"- Task: {prompt} - Output language: {language}. - Sender: {input_sender}. - Email Subject: {input_subject}. - Email Message: {input_message}. - What you want to say: {input_reply}."
|
| 27 |
+
response = openai.ChatCompletion.create(
|
| 28 |
+
model="gpt-4o",
|
| 29 |
+
messages=[{"role": "user", "content": user_prompt}],
|
| 30 |
+
max_tokens=4096
|
| 31 |
+
)
|
| 32 |
+
return response["choices"][0]["message"]["content"]
|
| 33 |
+
|
| 34 |
+
# Determine button text based on current language
|
| 35 |
+
if st.session_state['language'] == 'English':
|
| 36 |
+
switch_button_text = 'Switch Language to Japanese'
|
| 37 |
+
else:
|
| 38 |
+
switch_button_text = 'Switch Language to English'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Language switcher button
|
| 41 |
+
if st.button(switch_button_text):
|
| 42 |
+
if st.session_state['language'] == 'English':
|
| 43 |
+
st.session_state['language'] = 'Japanese'
|
| 44 |
+
else:
|
| 45 |
+
st.session_state['language'] = 'English'
|
| 46 |
+
st.experimental_rerun()
|
| 47 |
|
| 48 |
+
# Check usage count to limit API calls
|
| 49 |
+
if st.session_state['usage_count'] < max_uses:
|
| 50 |
+
# Display form based on selected language
|
| 51 |
+
if st.session_state['language'] == 'English':
|
| 52 |
+
# English Input Section
|
| 53 |
+
with st.form(key='en_form'):
|
| 54 |
+
st.subheader('English')
|
| 55 |
+
en_input_sender = st.selectbox("Sender",("Co-worker", "Boss", "Client", "Friend"),key="en_input_sender")
|
| 56 |
+
en_input_subject = st.text_input("Email Subject (e.g., About scheduling a meeting)", key="en_input_subject")
|
| 57 |
+
en_input_message = st.text_area("Content of the recipient's email: (e.g., I would like to adjust the time for tomorrow's meeting, are you available in the afternoon?)", key="en_input_message")
|
| 58 |
+
en_input_reply = st.text_input("What you want to say: (e.g., I am available after 2 PM.)", key="en_input_reply")
|
| 59 |
+
submit_en = st.form_submit_button("Generate an Email Reply")
|
| 60 |
+
if submit_en:
|
| 61 |
+
result = generate_reply(en_input_sender, en_input_subject, en_input_message, en_input_reply, en_input_length, "English")
|
| 62 |
+
st.write(result)
|
| 63 |
+
else:
|
| 64 |
+
# Japanese Input Section
|
| 65 |
+
with st.form(key='ja_form'):
|
| 66 |
+
st.subheader('日本語')
|
| 67 |
+
ja_input_sender = st.selectbox("送信主",("同僚", "上司", "クライアント", "友達"),key="ja_input_sender")
|
| 68 |
+
ja_input_subject = st.text_input("メールの件名 (例:ミーティングの日程調整について)", key="ja_input_subject")
|
| 69 |
+
ja_input_message = st.text_area("相手のメールの内容: (例:明日、ミーティングの時間を調整したいのですが、午後の時間帯は空いていますか?)", key="ja_input_message")
|
| 70 |
+
ja_input_reply = st.text_input("伝えたいこと: (例:午後2時以降は空いています。)", key="ja_input_reply")
|
| 71 |
+
submit_ja = st.form_submit_button("Eメールの返信を生成する")
|
| 72 |
+
if submit_ja:
|
| 73 |
+
result = generate_reply(ja_input_sender, ja_input_subject, ja_input_message, ja_input_reply, ja_input_length, "Japanese")
|
| 74 |
+
st.write(result)
|
| 75 |
else:
|
| 76 |
st.error("You have reached your maximum usage limit.")
|
| 77 |
|