Chan Meng
commited on
Commit
·
ed9b0ce
1
Parent(s):
d750212
update
Browse files- app.py +49 -11
- stories/story_20241212_173910.txt +24 -0
app.py
CHANGED
@@ -328,8 +328,13 @@ def create_demo():
|
|
328 |
# Save story dialogue
|
329 |
save_btn.click(
|
330 |
save_story,
|
331 |
-
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
333 |
)
|
334 |
|
335 |
# User input processing
|
@@ -427,26 +432,59 @@ def create_demo():
|
|
427 |
return demo
|
428 |
|
429 |
|
430 |
-
def save_story(chatbot):
|
431 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
if not chatbot:
|
433 |
return "Story is empty, cannot save"
|
434 |
|
435 |
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
436 |
-
filename = f"stories/story_{timestamp}.txt"
|
437 |
|
438 |
-
|
|
|
|
|
|
|
|
|
|
|
439 |
|
440 |
try:
|
441 |
with open(filename, "w", encoding="utf-8") as f:
|
442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
if user_msg:
|
444 |
f.write(f"User: {user_msg}\n")
|
445 |
-
if
|
446 |
-
f.write(f"AI: {
|
447 |
-
|
|
|
|
|
|
|
|
|
448 |
except Exception as e:
|
449 |
-
|
|
|
450 |
|
451 |
if __name__ == "__main__":
|
452 |
demo = create_demo()
|
|
|
328 |
# Save story dialogue
|
329 |
save_btn.click(
|
330 |
save_story,
|
331 |
+
inputs=[
|
332 |
+
chatbot,
|
333 |
+
style_select,
|
334 |
+
theme_select,
|
335 |
+
character_desc
|
336 |
+
],
|
337 |
+
outputs=status_msg
|
338 |
)
|
339 |
|
340 |
# User input processing
|
|
|
432 |
return demo
|
433 |
|
434 |
|
435 |
+
def save_story(chatbot, style=None, theme=None, character_desc=None):
|
436 |
+
"""
|
437 |
+
Save story dialogue record with metadata
|
438 |
+
Args:
|
439 |
+
chatbot: Chat history containing user and AI messages
|
440 |
+
style: Story style selected by user
|
441 |
+
theme: Story theme selected by user
|
442 |
+
character_desc: Character description
|
443 |
+
Returns:
|
444 |
+
Status message indicating success or failure
|
445 |
+
"""
|
446 |
if not chatbot:
|
447 |
return "Story is empty, cannot save"
|
448 |
|
449 |
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
|
|
450 |
|
451 |
+
# Create stories directory if it doesn't exist
|
452 |
+
# Use absolute path for Hugging Face Space
|
453 |
+
stories_dir = os.path.join(os.getcwd(), "stories")
|
454 |
+
os.makedirs(stories_dir, exist_ok=True)
|
455 |
+
|
456 |
+
filename = os.path.join(stories_dir, f"story_{timestamp}.txt")
|
457 |
|
458 |
try:
|
459 |
with open(filename, "w", encoding="utf-8") as f:
|
460 |
+
# Write header with metadata
|
461 |
+
f.write("=== Interactive Story ===\n")
|
462 |
+
f.write(f"Created: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
463 |
+
|
464 |
+
if style:
|
465 |
+
f.write(f"Style: {style}\n")
|
466 |
+
if theme:
|
467 |
+
f.write(f"Theme: {theme}\n")
|
468 |
+
if character_desc:
|
469 |
+
f.write(f"Character: {character_desc}\n")
|
470 |
+
|
471 |
+
f.write("\n=== Story Content ===\n\n")
|
472 |
+
|
473 |
+
# Write conversation
|
474 |
+
for i, (user_msg, ai_msg) in enumerate(chatbot, 1):
|
475 |
+
f.write(f"--- Turn {i} ---\n")
|
476 |
if user_msg:
|
477 |
f.write(f"User: {user_msg}\n")
|
478 |
+
if ai_msg:
|
479 |
+
f.write(f"AI: {ai_msg}\n")
|
480 |
+
f.write("\n")
|
481 |
+
|
482 |
+
# Return success message with filename
|
483 |
+
return gr.Markdown(f"✅ Story saved successfully to: {os.path.basename(filename)}")
|
484 |
+
|
485 |
except Exception as e:
|
486 |
+
logger.error(f"Error saving story: {str(e)}")
|
487 |
+
return gr.Markdown(f"❌ Failed to save story: {str(e)}")
|
488 |
|
489 |
if __name__ == "__main__":
|
490 |
demo = create_demo()
|
stories/story_20241212_173910.txt
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Interactive Story ===
|
2 |
+
Created: 2024-12-12 17:39:10
|
3 |
+
Style: Fantasy
|
4 |
+
Theme: Adventure
|
5 |
+
Character: A brave and fearless explorer who loves adventure and challenges.
|
6 |
+
|
7 |
+
=== Story Content ===
|
8 |
+
|
9 |
+
--- Turn 1 ---
|
10 |
+
User: A lake deep in the forest
|
11 |
+
AI: Deep in the heart of the forest, nestled among towering trees and lush greenery, there was a serene lake that glimmered in the sunlight. It was said that the lake held secrets and treasures beyond imagination, but few dared to venture into its depths. That was until a brave and fearless explorer named Aria set her sights on the lake.
|
12 |
+
|
13 |
+
Aria had spent her entire life exploring the unknown and seeking out adventure. Her love for challenges had led her to this very spot, where she stood now, gazing into the shimmering waters. She could feel the excitement building inside of her, and her heart raced with anticipation.
|
14 |
+
|
15 |
+
Without hesitation, Aria plunged into the lake, her body slicing through the water with ease. The lake was crystal clear, and she could see the bottom, which was filled with lush plants and colorful fish. As she swam deeper and deeper, Aria's senses heightened. She could hear the gentle lapping of the water against the shore and the rustling of leaves in the wind.
|
16 |
+
|
17 |
+
Suddenly, Aria's hand brushed against something cold and hard. Her heart skipped a beat as she reached out and grabbed onto a small object. It was a map, intricately drawn and filled with strange symbols. Aria's eyes widened as she realized the significance of what she had found. This map led to a hidden treasure, and she was the first to discover it.
|
18 |
+
|
19 |
+
But Aria knew that she couldn't keep the treasure to herself. It was her duty to share it with the world. With newfound determination, Aria swam back to the shore, the map clutched tightly in her hand. She knew that her adventure was just beginning, and she couldn't wait to see what lay ahead.
|
20 |
+
|
21 |
+
As Aria emerged from the lake, she noticed a group of men lurking in the shadows. They were watching her intently, and she knew that they too had set their sights on the treasure. Aria knew that she had to act quickly. She gathered her supplies and set off on a dangerous journey, determined to beat the men to the treasure.
|
22 |
+
|
23 |
+
The journey was filled with challenges and obstacles, but Aria was undeterred. She encountered fierce creatures and treacherous terrain, but she pushed on, driven by her love for adventure and the desire to uncover
|
24 |
+
|