Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,13 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
7 |
|
8 |
# Story genres with genre-specific example prompts
|
9 |
GENRE_EXAMPLES = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
"fantasy": [
|
11 |
"I enter the ancient forest seeking the wizard's tower",
|
12 |
"I approach the dragon cautiously with my shield raised",
|
@@ -65,10 +72,7 @@ GENRE_EXAMPLES = {
|
|
65 |
|
66 |
def get_examples_for_genre(genre):
|
67 |
"""Get example prompts specific to the selected genre"""
|
68 |
-
|
69 |
-
return GENRE_EXAMPLES[genre]
|
70 |
-
else:
|
71 |
-
return GENRE_EXAMPLES["fantasy"] # Default to fantasy
|
72 |
|
73 |
def get_enhanced_system_prompt(genre=None):
|
74 |
"""Generate a detailed system prompt with optional genre specification"""
|
@@ -103,17 +107,8 @@ def create_story_summary(chat_history):
|
|
103 |
}
|
104 |
return summary_instruction
|
105 |
|
106 |
-
# NEW HELPER: Convert (user, bot) tuples into the "role/content" dictionaries Gradio needs
|
107 |
def format_history_for_gradio(history_tuples):
|
108 |
-
"""
|
109 |
-
Convert a list of (user_msg, bot_msg) tuples
|
110 |
-
into a list of dicts for 'type="messages"':
|
111 |
-
[
|
112 |
-
{"role": "user", "content": ...},
|
113 |
-
{"role": "assistant", "content": ...},
|
114 |
-
...
|
115 |
-
]
|
116 |
-
"""
|
117 |
messages = []
|
118 |
for user_msg, bot_msg in history_tuples:
|
119 |
messages.append({"role": "user", "content": user_msg})
|
@@ -121,7 +116,7 @@ def format_history_for_gradio(history_tuples):
|
|
121 |
return messages
|
122 |
|
123 |
def respond(message, chat_history, genre=None, use_full_memory=True):
|
124 |
-
"""Generate a response based on the current message and conversation history"""
|
125 |
system_message = get_enhanced_system_prompt(genre)
|
126 |
|
127 |
# Convert your existing (user, bot) history into a format for the API request
|
@@ -155,7 +150,7 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
155 |
# Special handling for story initialization
|
156 |
if not chat_history or message.lower() in ["start", "begin", "begin my adventure"]:
|
157 |
api_messages.append({
|
158 |
-
"role": "system",
|
159 |
"content": f"Begin a new {genre or 'fantasy'} adventure with an intriguing opening scene. Introduce the protagonist without assuming too much about them."
|
160 |
})
|
161 |
|
@@ -193,10 +188,9 @@ def save_story(chat_history):
|
|
193 |
|
194 |
return story_text
|
195 |
|
196 |
-
# Create interface with additional customization options
|
197 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
198 |
-
gr.Markdown("# 🔮 Interactive Story
|
199 |
-
gr.Markdown("
|
200 |
|
201 |
with gr.Row():
|
202 |
with gr.Column(scale=3):
|
@@ -206,7 +200,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
206 |
bubble_full_width=False,
|
207 |
show_copy_button=True,
|
208 |
avatar_images=(None, "🧙"),
|
209 |
-
type="messages"
|
210 |
)
|
211 |
msg = gr.Textbox(
|
212 |
placeholder="Describe what you want to do next in the story...",
|
@@ -223,10 +217,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
223 |
genre = gr.Dropdown(
|
224 |
choices=list(GENRE_EXAMPLES.keys()),
|
225 |
label="Story Genre",
|
226 |
-
info="
|
227 |
value="fantasy"
|
228 |
)
|
229 |
-
|
230 |
full_memory = gr.Checkbox(
|
231 |
label="Full Story Memory",
|
232 |
value=True,
|
@@ -235,38 +228,34 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
235 |
|
236 |
gr.Markdown("## Story Starters")
|
237 |
|
238 |
-
#
|
239 |
starter_btn1 = gr.Button("Starter 1")
|
240 |
starter_btn2 = gr.Button("Starter 2")
|
241 |
starter_btn3 = gr.Button("Starter 3")
|
242 |
starter_btn4 = gr.Button("Starter 4")
|
243 |
starter_buttons = [starter_btn1, starter_btn2, starter_btn3, starter_btn4]
|
244 |
-
|
245 |
-
#
|
246 |
def update_starter_buttons(selected_genre):
|
247 |
-
# Grab up to 4 examples from the chosen genre
|
248 |
examples = get_examples_for_genre(selected_genre)
|
249 |
-
|
250 |
for i in range(4):
|
251 |
if i < len(examples):
|
252 |
-
|
253 |
else:
|
254 |
-
|
255 |
-
return
|
256 |
-
|
257 |
-
# Initialize
|
258 |
-
#
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
# Function that populates the msg with the chosen starter text, then calls respond
|
264 |
def pick_starter(starter_text, chat_history, selected_genre, memory_flag):
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
#
|
269 |
-
# 2) Then call 'respond' to update the chatbot
|
270 |
for starter_button in starter_buttons:
|
271 |
starter_button.click(
|
272 |
fn=pick_starter,
|
@@ -279,15 +268,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
279 |
outputs=[chatbot],
|
280 |
queue=False
|
281 |
)
|
282 |
-
|
283 |
-
#
|
284 |
genre.change(
|
285 |
fn=update_starter_buttons,
|
286 |
inputs=[genre],
|
287 |
outputs=starter_buttons
|
288 |
)
|
289 |
-
|
290 |
-
#
|
291 |
msg.submit(respond, [msg, chatbot, genre, full_memory], [chatbot])
|
292 |
submit.click(respond, [msg, chatbot, genre, full_memory], [chatbot])
|
293 |
|
@@ -295,7 +284,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
295 |
clear.click(lambda: [], None, chatbot, queue=False)
|
296 |
clear.click(lambda: "", None, msg, queue=False)
|
297 |
|
298 |
-
#
|
299 |
with gr.Row():
|
300 |
save_btn = gr.Button("Download My Story", variant="secondary")
|
301 |
story_output = gr.Markdown(visible=False)
|
@@ -309,5 +298,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
309 |
queue=False
|
310 |
)
|
311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
if __name__ == "__main__":
|
313 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
7 |
|
8 |
# Story genres with genre-specific example prompts
|
9 |
GENRE_EXAMPLES = {
|
10 |
+
"fairy_tale": [
|
11 |
+
"I follow the shimmer of fairy dust into a hidden forest"
|
12 |
+
"I meet a talking rabbit who claims to know a secret about the king’s lost crown"
|
13 |
+
"A tiny dragon appears at my window, asking for help to find its mother"
|
14 |
+
"I step into a clearing where the trees whisper ancient riddles"
|
15 |
+
"A friendly witch invites me into her cozy cottage, offering a warm cup of tea"
|
16 |
+
]
|
17 |
"fantasy": [
|
18 |
"I enter the ancient forest seeking the wizard's tower",
|
19 |
"I approach the dragon cautiously with my shield raised",
|
|
|
72 |
|
73 |
def get_examples_for_genre(genre):
|
74 |
"""Get example prompts specific to the selected genre"""
|
75 |
+
return GENRE_EXAMPLES.get(genre, GENRE_EXAMPLES["fantasy"])
|
|
|
|
|
|
|
76 |
|
77 |
def get_enhanced_system_prompt(genre=None):
|
78 |
"""Generate a detailed system prompt with optional genre specification"""
|
|
|
107 |
}
|
108 |
return summary_instruction
|
109 |
|
|
|
110 |
def format_history_for_gradio(history_tuples):
|
111 |
+
"""Convert (user, bot) tuples into Gradio 'messages' format (role/content dicts)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
messages = []
|
113 |
for user_msg, bot_msg in history_tuples:
|
114 |
messages.append({"role": "user", "content": user_msg})
|
|
|
116 |
return messages
|
117 |
|
118 |
def respond(message, chat_history, genre=None, use_full_memory=True):
|
119 |
+
"""Generate a response based on the current message and conversation history."""
|
120 |
system_message = get_enhanced_system_prompt(genre)
|
121 |
|
122 |
# Convert your existing (user, bot) history into a format for the API request
|
|
|
150 |
# Special handling for story initialization
|
151 |
if not chat_history or message.lower() in ["start", "begin", "begin my adventure"]:
|
152 |
api_messages.append({
|
153 |
+
"role": "system",
|
154 |
"content": f"Begin a new {genre or 'fantasy'} adventure with an intriguing opening scene. Introduce the protagonist without assuming too much about them."
|
155 |
})
|
156 |
|
|
|
188 |
|
189 |
return story_text
|
190 |
|
|
|
191 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
192 |
+
gr.Markdown("# 🔮 Interactive Story Time")
|
193 |
+
gr.Markdown("Create a completely unique literary world, one choice at a time. Dare to explore the unknown.")
|
194 |
|
195 |
with gr.Row():
|
196 |
with gr.Column(scale=3):
|
|
|
200 |
bubble_full_width=False,
|
201 |
show_copy_button=True,
|
202 |
avatar_images=(None, "🧙"),
|
203 |
+
type="messages"
|
204 |
)
|
205 |
msg = gr.Textbox(
|
206 |
placeholder="Describe what you want to do next in the story...",
|
|
|
217 |
genre = gr.Dropdown(
|
218 |
choices=list(GENRE_EXAMPLES.keys()),
|
219 |
label="Story Genre",
|
220 |
+
info="Choose the theme of your next adventure",
|
221 |
value="fantasy"
|
222 |
)
|
|
|
223 |
full_memory = gr.Checkbox(
|
224 |
label="Full Story Memory",
|
225 |
value=True,
|
|
|
228 |
|
229 |
gr.Markdown("## Story Starters")
|
230 |
|
231 |
+
# Create four placeholder buttons for story starters
|
232 |
starter_btn1 = gr.Button("Starter 1")
|
233 |
starter_btn2 = gr.Button("Starter 2")
|
234 |
starter_btn3 = gr.Button("Starter 3")
|
235 |
starter_btn4 = gr.Button("Starter 4")
|
236 |
starter_buttons = [starter_btn1, starter_btn2, starter_btn3, starter_btn4]
|
237 |
+
|
238 |
+
# 1) We'll return a list of 4 dicts, each dict updating 'value' & 'visible'
|
239 |
def update_starter_buttons(selected_genre):
|
|
|
240 |
examples = get_examples_for_genre(selected_genre)
|
241 |
+
results = []
|
242 |
for i in range(4):
|
243 |
if i < len(examples):
|
244 |
+
results.append({"value": examples[i], "visible": True})
|
245 |
else:
|
246 |
+
results.append({"value": "(no starter)", "visible": False})
|
247 |
+
return tuple(results) # we must return a tuple/list with 4 items for 4 outputs
|
248 |
+
|
249 |
+
# 2) Initialize them with "fantasy" so they don't stay "Starter X" on page load
|
250 |
+
# We'll just call the function and store the results in a variable, then apply them in a .load() event
|
251 |
+
initial_button_data = update_starter_buttons("fantasy") # returns 4 dicts
|
252 |
+
|
253 |
+
# 3) We'll define a "pick_starter" function that sets msg to the chosen text
|
|
|
|
|
254 |
def pick_starter(starter_text, chat_history, selected_genre, memory_flag):
|
255 |
+
# Putting 'starter_text' into the msg
|
256 |
+
return starter_text
|
257 |
+
|
258 |
+
# 4) Connect each starter button:
|
|
|
259 |
for starter_button in starter_buttons:
|
260 |
starter_button.click(
|
261 |
fn=pick_starter,
|
|
|
268 |
outputs=[chatbot],
|
269 |
queue=False
|
270 |
)
|
271 |
+
|
272 |
+
# 5) Dynamically update the 4 buttons if the user changes the genre
|
273 |
genre.change(
|
274 |
fn=update_starter_buttons,
|
275 |
inputs=[genre],
|
276 |
outputs=starter_buttons
|
277 |
)
|
278 |
+
|
279 |
+
# Handler for user input
|
280 |
msg.submit(respond, [msg, chatbot, genre, full_memory], [chatbot])
|
281 |
submit.click(respond, [msg, chatbot, genre, full_memory], [chatbot])
|
282 |
|
|
|
284 |
clear.click(lambda: [], None, chatbot, queue=False)
|
285 |
clear.click(lambda: "", None, msg, queue=False)
|
286 |
|
287 |
+
# "Download My Story" row
|
288 |
with gr.Row():
|
289 |
save_btn = gr.Button("Download My Story", variant="secondary")
|
290 |
story_output = gr.Markdown(visible=False)
|
|
|
298 |
queue=False
|
299 |
)
|
300 |
|
301 |
+
# 6) Finally, run a "load" event to apply initial_button_data to the 4 button outputs on page load
|
302 |
+
def load_initial_buttons():
|
303 |
+
# Just return our precomputed tuple of 4 dicts
|
304 |
+
return initial_button_data
|
305 |
+
|
306 |
+
demo.load(fn=load_initial_buttons, outputs=starter_buttons, queue=False)
|
307 |
+
|
308 |
+
# Run the app
|
309 |
if __name__ == "__main__":
|
310 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|