Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,17 +19,19 @@ st.title('Rap Generator')
|
|
| 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_rap(language, topic, occupation, message):
|
| 24 |
"""Generate a rap based on the user's inputs."""
|
| 25 |
if st.session_state['usage_count'] < MAX_USES:
|
| 26 |
st.session_state['usage_count'] += 1 # Increment the usage counter
|
| 27 |
rap_prompt = f"""
|
| 28 |
-
- Task
|
| 29 |
- Output language: {language}.
|
| 30 |
-
- Topic
|
| 31 |
-
- Occupation
|
| 32 |
-
- What you want to say
|
| 33 |
"""
|
| 34 |
with st.spinner('Generating your rap... Please wait.'):
|
| 35 |
response = openai.ChatCompletion.create(
|
|
@@ -42,27 +44,39 @@ def generate_rap(language, topic, occupation, message):
|
|
| 42 |
st.error("You have reached your maximum usage limit.")
|
| 43 |
return None
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
st.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if st.button("Generate a rap", key="en_generate_rap"):
|
| 51 |
-
result = generate_rap("English", en_topic, en_occupation, en_message)
|
| 52 |
-
if result:
|
| 53 |
-
st.write(result)
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
st.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Style adjustments (optional, remove if not needed)
|
| 68 |
st.markdown(
|
|
@@ -73,4 +87,4 @@ st.markdown(
|
|
| 73 |
</style>
|
| 74 |
""",
|
| 75 |
unsafe_allow_html=True,
|
| 76 |
-
)
|
|
|
|
| 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 |
+
if 'language' not in st.session_state:
|
| 23 |
+
st.session_state['language'] = 'English'
|
| 24 |
|
| 25 |
def generate_rap(language, topic, occupation, message):
|
| 26 |
"""Generate a rap based on the user's inputs."""
|
| 27 |
if st.session_state['usage_count'] < MAX_USES:
|
| 28 |
st.session_state['usage_count'] += 1 # Increment the usage counter
|
| 29 |
rap_prompt = f"""
|
| 30 |
+
- Task: {prompt_template}.
|
| 31 |
- Output language: {language}.
|
| 32 |
+
- Topic: {topic}.
|
| 33 |
+
- Occupation: {occupation}.
|
| 34 |
+
- What you want to say: {message}.
|
| 35 |
"""
|
| 36 |
with st.spinner('Generating your rap... Please wait.'):
|
| 37 |
response = openai.ChatCompletion.create(
|
|
|
|
| 44 |
st.error("You have reached your maximum usage limit.")
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
# Determine button text based on current language
|
| 48 |
+
if st.session_state['language'] == 'English':
|
| 49 |
+
switch_button_text = 'Switch Language to Japanese'
|
| 50 |
+
else:
|
| 51 |
+
switch_button_text = 'Switch Language to English'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Language switcher button
|
| 54 |
+
if st.button(switch_button_text):
|
| 55 |
+
if st.session_state['language'] == 'English':
|
| 56 |
+
st.session_state['language'] = 'Japanese'
|
| 57 |
+
else:
|
| 58 |
+
st.session_state['language'] = 'English'
|
| 59 |
+
st.experimental_rerun()
|
| 60 |
|
| 61 |
+
# Display form based on selected language
|
| 62 |
+
if st.session_state['language'] == 'English':
|
| 63 |
+
st.subheader('English')
|
| 64 |
+
en_topic = st.text_input("Topic (e.g., Sunday)", key="en_topic")
|
| 65 |
+
en_occupation = st.text_input("Your Occupation (e.g., Data Scientist)", key="en_occupation")
|
| 66 |
+
en_message = st.text_input("What do you want to say? (e.g., prepare for tomorrow)", key="en_message")
|
| 67 |
+
if st.button("Generate a rap", key="en_generate_rap"):
|
| 68 |
+
result = generate_rap("English", en_topic, en_occupation, en_message)
|
| 69 |
+
if result:
|
| 70 |
+
st.write(result)
|
| 71 |
+
else:
|
| 72 |
+
st.subheader('日本語')
|
| 73 |
+
ja_topic = st.text_input("トピック(例:日曜日)", key="ja_topic")
|
| 74 |
+
ja_occupation = st.text_input("あなたの仕事(例:データサイエンティスト)", key="ja_occupation")
|
| 75 |
+
ja_message = st.text_input("言いたいこと(例:明日に向けて気持ちを準備したい)", key="ja_message")
|
| 76 |
+
if st.button("ラップを生成する", key="ja_generate_rap"):
|
| 77 |
+
result = generate_rap("Japanese", ja_topic, ja_occupation, ja_message)
|
| 78 |
+
if result:
|
| 79 |
+
st.write(result)
|
| 80 |
|
| 81 |
# Style adjustments (optional, remove if not needed)
|
| 82 |
st.markdown(
|
|
|
|
| 87 |
</style>
|
| 88 |
""",
|
| 89 |
unsafe_allow_html=True,
|
| 90 |
+
)
|