Spaces:
Running
Running
Update backup2.history.app.py
Browse files- backup2.history.app.py +37 -20
backup2.history.app.py
CHANGED
@@ -15,13 +15,18 @@ from gradio_client import Client
|
|
15 |
|
16 |
warnings.filterwarnings('ignore')
|
17 |
|
18 |
-
# Initialize story starters
|
19 |
STORY_STARTERS = [
|
20 |
['Adventure', 'In a hidden temple deep in the Amazon...'],
|
21 |
['Mystery', 'The detective found an unusual note...'],
|
22 |
['Romance', 'Two strangers meet on a rainy evening...'],
|
23 |
['Sci-Fi', 'The space station received an unexpected signal...'],
|
24 |
-
['Fantasy', 'A magical portal appeared in the garden...']
|
|
|
|
|
|
|
|
|
|
|
25 |
]
|
26 |
|
27 |
# Initialize client outside of interface definition
|
@@ -34,58 +39,70 @@ def init_client():
|
|
34 |
return arxiv_client
|
35 |
|
36 |
def save_story(story, audio_path):
|
37 |
-
"""Save story and audio to gallery"""
|
38 |
try:
|
39 |
# Create gallery directory if it doesn't exist
|
40 |
gallery_dir = Path("gallery")
|
41 |
gallery_dir.mkdir(exist_ok=True)
|
42 |
|
43 |
-
# Generate timestamp for
|
44 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
|
45 |
|
46 |
-
# Save story text
|
47 |
-
story_path = gallery_dir / f"story_{timestamp}.
|
48 |
with open(story_path, "w") as f:
|
49 |
-
f.write(story)
|
50 |
|
51 |
-
# Copy audio file to gallery
|
|
|
52 |
if audio_path:
|
53 |
-
new_audio_path = gallery_dir / f"audio_{timestamp}.mp3"
|
54 |
os.system(f"cp {audio_path} {str(new_audio_path)}")
|
55 |
|
56 |
-
return str(story_path), str(new_audio_path)
|
57 |
except Exception as e:
|
58 |
print(f"Error saving to gallery: {str(e)}")
|
59 |
return None, None
|
60 |
|
61 |
def load_gallery():
|
62 |
-
"""Load all stories and audio from gallery"""
|
63 |
try:
|
64 |
gallery_dir = Path("gallery")
|
65 |
if not gallery_dir.exists():
|
66 |
return []
|
67 |
|
68 |
files = []
|
69 |
-
for story_file in gallery_dir.glob("story_*.
|
70 |
-
timestamp
|
71 |
-
|
|
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
with open(story_file) as f:
|
74 |
-
|
|
|
|
|
75 |
|
76 |
-
# Format as list instead of dict for Gradio Dataframe
|
77 |
files.append([
|
78 |
timestamp,
|
79 |
-
|
80 |
str(story_file),
|
81 |
-
str(audio_file) if audio_file
|
82 |
])
|
83 |
|
84 |
-
return
|
85 |
except Exception as e:
|
86 |
print(f"Error loading gallery: {str(e)}")
|
87 |
return []
|
88 |
|
|
|
89 |
def generate_story(prompt, model_choice):
|
90 |
"""Generate story using specified model"""
|
91 |
try:
|
@@ -145,7 +162,7 @@ def play_gallery_audio(evt: gr.SelectData, gallery_data):
|
|
145 |
print(f"Error playing gallery audio: {str(e)}")
|
146 |
return None
|
147 |
|
148 |
-
# Create the Gradio interface
|
149 |
with gr.Blocks(title="AI Story Generator") as demo:
|
150 |
gr.Markdown("""
|
151 |
# ๐ญ AI Story Generator & Narrator
|
|
|
15 |
|
16 |
warnings.filterwarnings('ignore')
|
17 |
|
18 |
+
# Initialize story starters with added comedy section
|
19 |
STORY_STARTERS = [
|
20 |
['Adventure', 'In a hidden temple deep in the Amazon...'],
|
21 |
['Mystery', 'The detective found an unusual note...'],
|
22 |
['Romance', 'Two strangers meet on a rainy evening...'],
|
23 |
['Sci-Fi', 'The space station received an unexpected signal...'],
|
24 |
+
['Fantasy', 'A magical portal appeared in the garden...'],
|
25 |
+
['Comedy-Sitcom', 'The new roommate arrived with seven emotional support animals...'],
|
26 |
+
['Comedy-Workplace', 'The office printer started sending mysterious messages...'],
|
27 |
+
['Comedy-Family', 'Grandma decided to become a social media influencer...'],
|
28 |
+
['Comedy-Supernatural', 'The ghost haunting the house was absolutely terrible at scaring people...'],
|
29 |
+
['Comedy-Travel', 'The GPS insisted on giving directions in interpretive dance descriptions...']
|
30 |
]
|
31 |
|
32 |
# Initialize client outside of interface definition
|
|
|
39 |
return arxiv_client
|
40 |
|
41 |
def save_story(story, audio_path):
|
42 |
+
"""Save story and audio to gallery with markdown formatting"""
|
43 |
try:
|
44 |
# Create gallery directory if it doesn't exist
|
45 |
gallery_dir = Path("gallery")
|
46 |
gallery_dir.mkdir(exist_ok=True)
|
47 |
|
48 |
+
# Generate timestamp and sanitize first line for filename
|
49 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
50 |
+
first_line = story.split('\n')[0].strip()
|
51 |
+
safe_name = re.sub(r'[^\w\s-]', '', first_line)[:50] # First 50 chars, sanitized
|
52 |
|
53 |
+
# Save story text as markdown
|
54 |
+
story_path = gallery_dir / f"story_{timestamp}_{safe_name}.md"
|
55 |
with open(story_path, "w") as f:
|
56 |
+
f.write(f"# {first_line}\n\n{story}")
|
57 |
|
58 |
+
# Copy audio file to gallery with matching name
|
59 |
+
new_audio_path = None
|
60 |
if audio_path:
|
61 |
+
new_audio_path = gallery_dir / f"audio_{timestamp}_{safe_name}.mp3"
|
62 |
os.system(f"cp {audio_path} {str(new_audio_path)}")
|
63 |
|
64 |
+
return str(story_path), str(new_audio_path) if new_audio_path else None
|
65 |
except Exception as e:
|
66 |
print(f"Error saving to gallery: {str(e)}")
|
67 |
return None, None
|
68 |
|
69 |
def load_gallery():
|
70 |
+
"""Load all stories and audio from gallery with markdown support"""
|
71 |
try:
|
72 |
gallery_dir = Path("gallery")
|
73 |
if not gallery_dir.exists():
|
74 |
return []
|
75 |
|
76 |
files = []
|
77 |
+
for story_file in sorted(gallery_dir.glob("story_*.md"), reverse=True):
|
78 |
+
# Extract timestamp and name from filename
|
79 |
+
parts = story_file.stem.split('_', 2)
|
80 |
+
timestamp = f"{parts[1]}"
|
81 |
|
82 |
+
# Find matching audio file
|
83 |
+
audio_pattern = f"audio_{timestamp}_*.mp3"
|
84 |
+
audio_files = list(gallery_dir.glob(audio_pattern))
|
85 |
+
audio_file = audio_files[0] if audio_files else None
|
86 |
+
|
87 |
+
# Read story content and get preview
|
88 |
with open(story_file) as f:
|
89 |
+
content = f.read()
|
90 |
+
# Skip markdown header and get preview
|
91 |
+
preview = content.split('\n\n', 1)[1][:100] + "..."
|
92 |
|
|
|
93 |
files.append([
|
94 |
timestamp,
|
95 |
+
f"[{preview}]({str(story_file)})", # Markdown link to story
|
96 |
str(story_file),
|
97 |
+
str(audio_file) if audio_file else None
|
98 |
])
|
99 |
|
100 |
+
return files
|
101 |
except Exception as e:
|
102 |
print(f"Error loading gallery: {str(e)}")
|
103 |
return []
|
104 |
|
105 |
+
# Keep all other functions unchanged
|
106 |
def generate_story(prompt, model_choice):
|
107 |
"""Generate story using specified model"""
|
108 |
try:
|
|
|
162 |
print(f"Error playing gallery audio: {str(e)}")
|
163 |
return None
|
164 |
|
165 |
+
# Create the Gradio interface (keep unchanged)
|
166 |
with gr.Blocks(title="AI Story Generator") as demo:
|
167 |
gr.Markdown("""
|
168 |
# ๐ญ AI Story Generator & Narrator
|