Spaces:
Sleeping
Sleeping
File size: 3,463 Bytes
31739d3 4faf535 f486f53 4faf535 2870ed2 4faf535 7d8e0ce 4faf535 31739d3 4faf535 7d8e0ce be1ae74 4faf535 40faa60 4faf535 31739d3 4faf535 2d71a9f cd5d489 4faf535 055b967 4faf535 2d71a9f 4faf535 2d71a9f 5b62891 cd5d489 4faf535 cd5d489 2d71a9f d49c002 2d71a9f d49c002 cd5d489 2d71a9f cd5d489 2d71a9f 1917890 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import streamlit as st
import openai
# Constants
AI_MODEL = "gpt-4o"
TOKEN_COUNT = 4096
MAX_USES = 3
# Set the page configuration
st.set_page_config(page_title="Rap Generator", page_icon=":bar_chart:")
# Load the API key and prompt from secrets
openai.api_key = st.secrets['OPENAI_API_KEY']
prompt_template = st.secrets['PROMPT_RAP_GENERATOR']
# Style adjustments (optional, remove if not needed)
st.markdown(
"""
<style>
/* Custom style adjustments */
.st-emotion-cache-iiif1v { display: none !important; }
.st-emotion-cache-gh2jqd {padding: 6rem 1rem 0rem;}
@media (max-width: 50.5rem) {
.st-emotion-cache-gh2jqd {
max-width: calc(0rem + 100vw);
}
}
</style>
""",
unsafe_allow_html=True,
)
# Page title
st.title('Rap Generator')
# Initialize or retrieve the usage count from session state
if 'usage_count' not in st.session_state:
st.session_state['usage_count'] = 0
if 'language' not in st.session_state:
st.session_state['language'] = 'English'
def generate_rap(language, topic, occupation, message):
"""Generate a rap based on the user's inputs."""
if st.session_state['usage_count'] < MAX_USES:
st.session_state['usage_count'] += 1 # Increment the usage counter
rap_prompt = f"""
- Task: {prompt_template}.
- Output language: {language}.
- Topic: {topic}.
- Occupation: {occupation}.
- What you want to say: {message}.
- Don't exceed more than 8 lines.
"""
with st.spinner('Generating your rap... Please wait.'):
response = openai.ChatCompletion.create(
model=AI_MODEL,
messages=[{"role": "user", "content": rap_prompt}],
max_tokens=TOKEN_COUNT
)
return response["choices"][0]["message"]["content"]
else:
st.error("You have reached your maximum usage limit.")
return None
# Determine button text based on current language
if st.session_state['language'] == 'English':
switch_button_text = 'Japanese(日本語)'
else:
switch_button_text = 'English'
# Language switcher button
if st.button(switch_button_text):
if st.session_state['language'] == 'English':
st.session_state['language'] = 'Japanese'
else:
st.session_state['language'] = 'English'
st.experimental_rerun()
# Display form based on selected language
if st.session_state['language'] == 'English':
st.subheader('English')
en_topic = st.text_input("Topic (e.g., Sunday)", key="en_topic")
en_occupation = st.text_input("Your Occupation (e.g., Data Scientist)", key="en_occupation")
en_message = st.text_input("What do you want to say? (e.g., prepare for tomorrow)", key="en_message")
if st.button("Generate a rap", key="en_generate_rap"):
result = generate_rap("English", en_topic, en_occupation, en_message)
if result:
st.write(result)
else:
st.subheader('日本語')
ja_topic = st.text_input("トピック(例:日曜日)", key="ja_topic")
ja_occupation = st.text_input("あなたの仕事(例:データサイエンティスト)", key="ja_occupation")
ja_message = st.text_input("言いたいこと(例:明日に向けて気持ちを準備したい)", key="ja_message")
if st.button("ラップを生成する", key="ja_generate_rap"):
result = generate_rap("Japanese", ja_topic, ja_occupation, ja_message)
if result:
st.write(result)
|