Spaces:
Running
Running
add api key text input
Browse files
app.py
CHANGED
@@ -4,6 +4,10 @@ import numpy as np
|
|
4 |
import pretty_midi
|
5 |
import re
|
6 |
import numpy as np
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# sample data
|
9 |
markdown_table_sample = """4
|
@@ -45,8 +49,7 @@ MIDI_NOTENUM = {
|
|
45 |
}
|
46 |
SR = 44100
|
47 |
|
48 |
-
|
49 |
-
MAX_QUERY = 100
|
50 |
|
51 |
def convert_table_to_audio(markdown_table, resolution=8, bpm = 120.0):
|
52 |
# convert table to array
|
@@ -92,11 +95,10 @@ def get_answer(question):
|
|
92 |
)
|
93 |
return response["choices"][0]["message"]["content"]
|
94 |
|
95 |
-
def generate_rhythm(query):
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
return [None, "Now you can try up to %d times" % MAX_QUERY]
|
100 |
|
101 |
# get respance from ChatGPT
|
102 |
text_output = get_answer(query)
|
@@ -119,17 +121,25 @@ def generate_rhythm(query):
|
|
119 |
return [(SR, audio_data), text_output]
|
120 |
# %%
|
121 |
|
122 |
-
|
|
|
|
|
123 |
|
124 |
with gr.Blocks() as demo:
|
|
|
|
|
125 |
gr.Markdown("Ask ChatGPT to generate rhythm patterns")
|
126 |
with gr.Row():
|
127 |
-
|
|
|
|
|
128 |
with gr.Column():
|
129 |
out_audio = gr.Audio()
|
130 |
out_text = gr.Textbox(placeholder="ChatGPT output")
|
131 |
-
|
132 |
-
|
|
|
|
|
133 |
demo.launch()
|
134 |
# demo = gr.Interface(
|
135 |
# fn=generate_rhythm,
|
|
|
4 |
import pretty_midi
|
5 |
import re
|
6 |
import numpy as np
|
7 |
+
import os
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
11 |
|
12 |
# sample data
|
13 |
markdown_table_sample = """4
|
|
|
49 |
}
|
50 |
SR = 44100
|
51 |
|
52 |
+
MAX_QUERY = 5
|
|
|
53 |
|
54 |
def convert_table_to_audio(markdown_table, resolution=8, bpm = 120.0):
|
55 |
# convert table to array
|
|
|
95 |
)
|
96 |
return response["choices"][0]["message"]["content"]
|
97 |
|
98 |
+
def generate_rhythm(query, state):
|
99 |
+
if state["gen_count"] > MAX_QUERY:
|
100 |
+
return [None, "You need to set your ChatGPT API Key to try more than %d times" % MAX_QUERY]
|
101 |
+
state["gen_count"] = state["gen_count"] + 1
|
|
|
102 |
|
103 |
# get respance from ChatGPT
|
104 |
text_output = get_answer(query)
|
|
|
121 |
return [(SR, audio_data), text_output]
|
122 |
# %%
|
123 |
|
124 |
+
def on_token_change(user_token):
|
125 |
+
openai.api_key = user_token or os.environ.get("OPENAI_API_KEY")
|
126 |
+
|
127 |
|
128 |
with gr.Blocks() as demo:
|
129 |
+
state = gr.State({"gen_count": 0})
|
130 |
+
|
131 |
gr.Markdown("Ask ChatGPT to generate rhythm patterns")
|
132 |
with gr.Row():
|
133 |
+
with gr.Column():
|
134 |
+
inp = gr.Textbox(placeholder="Give me a Hiphop rhythm pattern with some reggae twist!")
|
135 |
+
btn = gr.Button("Generate")
|
136 |
with gr.Column():
|
137 |
out_audio = gr.Audio()
|
138 |
out_text = gr.Textbox(placeholder="ChatGPT output")
|
139 |
+
gr.Markdown("Enter your own OpenAI API Key to try out more than 5 times. You can get it [here](https://platform.openai.com/account/api-keys).", elem_id="label")
|
140 |
+
user_token = gr.Textbox(placeholder="OpenAI API Key", type="password", show_label=False)
|
141 |
+
btn.click(fn=generate_rhythm, inputs=[inp, state], outputs=[out_audio, out_text])
|
142 |
+
user_token.change(on_token_change, inputs=[user_token], outputs=[])
|
143 |
demo.launch()
|
144 |
# demo = gr.Interface(
|
145 |
# fn=generate_rhythm,
|