Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,6 +96,7 @@ with gr.Blocks() as demo:
|
|
| 96 |
prefix_box = gr.TextArea(value="Twinkle Twinkle Little Star", label="Score title / text prefix")
|
| 97 |
with gr.Row():
|
| 98 |
submit_btn = gr.Button("Generate")
|
|
|
|
| 99 |
clear_btn = gr.Button("Clear history")
|
| 100 |
with gr.Row():
|
| 101 |
get_audio_btn = gr.Button("Convert to audio")
|
|
@@ -119,16 +120,28 @@ with gr.Blocks() as demo:
|
|
| 119 |
def user_fn(user_message, history: list):
|
| 120 |
return "", history + [{"role": "user", "content": user_message}]
|
| 121 |
|
| 122 |
-
|
| 123 |
-
def
|
| 124 |
-
|
|
|
|
| 125 |
# prevent the model from continuing user's score title
|
| 126 |
if prefix != '' and '\n' not in prefix:
|
| 127 |
# prefix is a single line --> prefix is the score title
|
| 128 |
prefix += '\n'
|
| 129 |
|
| 130 |
history.append({"role": "assistant", "content": ""})
|
| 131 |
-
history[-1]["content"] += "Generating with the given prefix...\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
queue = Queue(maxsize=10)
|
| 133 |
class MyStreamer:
|
| 134 |
def put(self, tokens):
|
|
@@ -144,7 +157,7 @@ with gr.Blocks() as demo:
|
|
| 144 |
result = pipe(
|
| 145 |
prefix,
|
| 146 |
streamer=MyStreamer(),
|
| 147 |
-
|
| 148 |
top_p=0.9, temperature=0.6,
|
| 149 |
)
|
| 150 |
except queue.Full:
|
|
@@ -161,12 +174,15 @@ with gr.Blocks() as demo:
|
|
| 161 |
yield history
|
| 162 |
|
| 163 |
prefix_box.submit(user_fn, [prefix_box, chatbot_box], [prefix_box, chatbot_box], queue=False).then(
|
| 164 |
-
|
| 165 |
)
|
| 166 |
submit_event = submit_btn.click(user_fn, [prefix_box, chatbot_box], [prefix_box, chatbot_box], queue=False).then(
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
)
|
| 169 |
-
clear_btn.click(lambda: None, inputs=[], outputs=chatbot_box, cancels=[submit_event], queue=False)
|
| 170 |
|
| 171 |
def get_audio_fn(history):
|
| 172 |
i = random.randint(0, 1000_000_000)
|
|
|
|
| 96 |
prefix_box = gr.TextArea(value="Twinkle Twinkle Little Star", label="Score title / text prefix")
|
| 97 |
with gr.Row():
|
| 98 |
submit_btn = gr.Button("Generate")
|
| 99 |
+
continue_btn = gr.Button("Continue")
|
| 100 |
clear_btn = gr.Button("Clear history")
|
| 101 |
with gr.Row():
|
| 102 |
get_audio_btn = gr.Button("Convert to audio")
|
|
|
|
| 120 |
def user_fn(user_message, history: list):
|
| 121 |
return "", history + [{"role": "user", "content": user_message}]
|
| 122 |
|
| 123 |
+
|
| 124 |
+
def generate_fn(history: list):
|
| 125 |
+
# continue from user input
|
| 126 |
+
prefix = history[-1]["content"]
|
| 127 |
# prevent the model from continuing user's score title
|
| 128 |
if prefix != '' and '\n' not in prefix:
|
| 129 |
# prefix is a single line --> prefix is the score title
|
| 130 |
prefix += '\n'
|
| 131 |
|
| 132 |
history.append({"role": "assistant", "content": ""})
|
| 133 |
+
# history[-1]["content"] += "Generating with the given prefix...\n"
|
| 134 |
+
for history in model_fn(prefix):
|
| 135 |
+
yield history
|
| 136 |
+
|
| 137 |
+
def continue_fn(history: list):
|
| 138 |
+
# continue from the last model output
|
| 139 |
+
prefix = history[-1]["content"]
|
| 140 |
+
for history in model_fn(prefix):
|
| 141 |
+
yield history
|
| 142 |
+
|
| 143 |
+
@spaces.GPU
|
| 144 |
+
def model_fn(prefix):
|
| 145 |
queue = Queue(maxsize=10)
|
| 146 |
class MyStreamer:
|
| 147 |
def put(self, tokens):
|
|
|
|
| 157 |
result = pipe(
|
| 158 |
prefix,
|
| 159 |
streamer=MyStreamer(),
|
| 160 |
+
max_new_tokens=500,
|
| 161 |
top_p=0.9, temperature=0.6,
|
| 162 |
)
|
| 163 |
except queue.Full:
|
|
|
|
| 174 |
yield history
|
| 175 |
|
| 176 |
prefix_box.submit(user_fn, [prefix_box, chatbot_box], [prefix_box, chatbot_box], queue=False).then(
|
| 177 |
+
generate_fn, chatbot_box, chatbot_box
|
| 178 |
)
|
| 179 |
submit_event = submit_btn.click(user_fn, [prefix_box, chatbot_box], [prefix_box, chatbot_box], queue=False).then(
|
| 180 |
+
generate_fn, chatbot_box, chatbot_box
|
| 181 |
+
)
|
| 182 |
+
continue_event = continue_btn.click(
|
| 183 |
+
continue_fn, chatbot_box, chatbot_box
|
| 184 |
)
|
| 185 |
+
clear_btn.click(lambda: None, inputs=[], outputs=chatbot_box, cancels=[submit_event, continue_event], queue=False)
|
| 186 |
|
| 187 |
def get_audio_fn(history):
|
| 188 |
i = random.randint(0, 1000_000_000)
|