heysho commited on
Commit
31739d3
·
verified ·
1 Parent(s): 73242fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="Rap Generator", page_icon=":bar_chart:")
9
+
10
+ openai.api_key = st.secrets['OPENAI_API_KEY']
11
+ st.title('Rap Generator')
12
+
13
+
14
+
15
+
16
+ # Initialize the session state for 'authenticated' key
17
+ if 'authenticated' not in st.session_state:
18
+ st.session_state['authenticated'] = False
19
+
20
+ if 'usage_count' not in st.session_state:
21
+ st.session_state['usage_count'] = 0 # Usage counter
22
+
23
+ password_key = st.secrets['PASSWORD']
24
+
25
+ # Only show the login form if the user is not authenticated
26
+ if not st.session_state['authenticated']:
27
+ password_placeholder = st.empty()
28
+ login_button_placeholder = st.empty()
29
+
30
+ password = password_placeholder.text_input("Enter your password", type="password")
31
+
32
+ if login_button_placeholder.button('Login'):
33
+ if password == password_key:
34
+ st.session_state['authenticated'] = True
35
+ st.session_state['usage_count'] = 0 # Reset the usage count upon new login
36
+ password_placeholder.empty()
37
+ login_button_placeholder.empty()
38
+ st.success("Login successful!")
39
+ else:
40
+ st.error('Wrong password')
41
+
42
+ # If authenticated, show the main page content
43
+ if st.session_state['authenticated']:
44
+ # Define a maximum number of uses
45
+ max_uses = 3
46
+
47
+ if st.session_state['usage_count'] < max_uses:
48
+ # Main Contents Start from here -------------------------------
49
+
50
+ st.subheader('English')
51
+ en_input_topic = st.text_input("Topic(e.g. Sunday)", key="en_input_topic")
52
+ en_input_occupation = st.text_input("Your Occupation(e.g. Data Scientist)", key="en_input_occupation")
53
+ en_input_message = st.text_input("What do you want to say? (e.g. prepare for tomorrow)", key="en_input_message")
54
+
55
+ if st.button("Generate a rap", key="en_generate_rap"):
56
+ # Create a prompt based on the user input
57
+ en_prompt = f"""
58
+ - Task: Generate a rap in English
59
+ - Length: a rap with 8 lines, each line having 8 beats.
60
+ - Topic:{en_input_topic}。
61
+ - Occupation: {en_input_occupation}。
62
+ - What you want to say: {en_input_message}。
63
+ """
64
+ # Make a request to the API to generate text
65
+ en_response = openai.ChatCompletion.create(
66
+ model=ai_model, # Use the engine of your choice
67
+ messages=[{"role": "user", "content": en_prompt}],
68
+ max_tokens=token
69
+ )
70
+ st.write(en_response["choices"][0]["message"]["content"])
71
+
72
+ st.text(" ")
73
+ st.text(" ")
74
+
75
+
76
+ st.subheader('日本語')
77
+ ja_input_topic = st.text_input("トピック(例:日曜日)", key="ja_input_topic")
78
+ ja_input_occupation = st.text_input("あなたの仕事(例:データサイエンティスト)", key="ja_input_occupation")
79
+ ja_input_message = st.text_input("言いたいこと(例:明日に向けて気持ちを準備したい)", key="ja_input_message")
80
+
81
+ if st.button("Generate a rap", key="ja_generate_rap"):
82
+ # Create a prompt based on the user input
83
+ ja_prompt = f"""
84
+ - Task: Generate a rap in Japanese
85
+ - Length: a rap with 8 lines, each line having 8 beats.
86
+ - Topic:{ja_input_topic}。
87
+ - Occupation: {ja_input_occupation}。
88
+ - What you want to say: {ja_input_message}。
89
+ """
90
+ # Make a request to the API to generate text
91
+ ja_response = openai.ChatCompletion.create(
92
+ model=ai_model, # Use the engine of your choice
93
+ messages=[{"role": "user", "content": ja_prompt}],
94
+ max_tokens=token
95
+ )
96
+ st.write(ja_response["choices"][0]["message"]["content"])
97
+ else:
98
+ st.error("You have reached your maximum usage limit.")