Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,16 +16,13 @@ from gradio_client import Client
|
|
| 16 |
warnings.filterwarnings('ignore')
|
| 17 |
|
| 18 |
# Initialize story starters
|
| 19 |
-
STORY_STARTERS =
|
| 20 |
-
|
| 21 |
-
'
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
'A magical portal appeared in the garden...'
|
| 27 |
-
]
|
| 28 |
-
})
|
| 29 |
|
| 30 |
# Initialize client outside of interface definition
|
| 31 |
arxiv_client = None
|
|
@@ -76,14 +73,15 @@ def load_gallery():
|
|
| 76 |
with open(story_file) as f:
|
| 77 |
story_text = f.read()
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
return sorted(files, key=lambda x: x[
|
| 87 |
except Exception as e:
|
| 88 |
print(f"Error loading gallery: {str(e)}")
|
| 89 |
return []
|
|
@@ -135,6 +133,18 @@ def process_story_and_audio(prompt, model_choice):
|
|
| 135 |
except Exception as e:
|
| 136 |
return f"Error: {str(e)}", None, None
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# Create the Gradio interface
|
| 139 |
with gr.Blocks(title="AI Story Generator") as demo:
|
| 140 |
gr.Markdown("""
|
|
@@ -180,20 +190,20 @@ with gr.Blocks(title="AI Story Generator") as demo:
|
|
| 180 |
gr.Markdown("### π Story Starters")
|
| 181 |
story_starters = gr.Dataframe(
|
| 182 |
value=STORY_STARTERS,
|
| 183 |
-
headers=["
|
| 184 |
interactive=False
|
| 185 |
)
|
| 186 |
|
| 187 |
gr.Markdown("### π¬ Gallery")
|
| 188 |
gallery = gr.Dataframe(
|
| 189 |
value=load_gallery(),
|
| 190 |
-
headers=["
|
| 191 |
interactive=False
|
| 192 |
)
|
| 193 |
|
| 194 |
# Event handlers
|
| 195 |
def update_prompt(evt: gr.SelectData):
|
| 196 |
-
return STORY_STARTERS
|
| 197 |
|
| 198 |
story_starters.select(update_prompt, None, prompt_input)
|
| 199 |
|
|
@@ -202,6 +212,12 @@ with gr.Blocks(title="AI Story Generator") as demo:
|
|
| 202 |
inputs=[prompt_input, model_choice],
|
| 203 |
outputs=[story_output, audio_output, gallery]
|
| 204 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch()
|
|
|
|
| 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
|
| 28 |
arxiv_client = None
|
|
|
|
| 73 |
with open(story_file) as f:
|
| 74 |
story_text = f.read()
|
| 75 |
|
| 76 |
+
# Format as list instead of dict for Gradio Dataframe
|
| 77 |
+
files.append([
|
| 78 |
+
timestamp,
|
| 79 |
+
story_text[:100] + "...",
|
| 80 |
+
str(story_file),
|
| 81 |
+
str(audio_file) if audio_file.exists() else None
|
| 82 |
+
])
|
| 83 |
|
| 84 |
+
return sorted(files, key=lambda x: x[0], reverse=True)
|
| 85 |
except Exception as e:
|
| 86 |
print(f"Error loading gallery: {str(e)}")
|
| 87 |
return []
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
return f"Error: {str(e)}", None, None
|
| 135 |
|
| 136 |
+
def play_gallery_audio(evt: gr.SelectData, gallery_data):
|
| 137 |
+
"""Play audio from gallery selection"""
|
| 138 |
+
try:
|
| 139 |
+
selected_row = gallery_data[evt.index[0]]
|
| 140 |
+
audio_path = selected_row[3] # Audio path is the fourth element
|
| 141 |
+
if audio_path and os.path.exists(audio_path):
|
| 142 |
+
return audio_path
|
| 143 |
+
return None
|
| 144 |
+
except Exception as e:
|
| 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("""
|
|
|
|
| 190 |
gr.Markdown("### π Story Starters")
|
| 191 |
story_starters = gr.Dataframe(
|
| 192 |
value=STORY_STARTERS,
|
| 193 |
+
headers=["Category", "Starter"],
|
| 194 |
interactive=False
|
| 195 |
)
|
| 196 |
|
| 197 |
gr.Markdown("### π¬ Gallery")
|
| 198 |
gallery = gr.Dataframe(
|
| 199 |
value=load_gallery(),
|
| 200 |
+
headers=["Timestamp", "Preview", "Story Path", "Audio Path"],
|
| 201 |
interactive=False
|
| 202 |
)
|
| 203 |
|
| 204 |
# Event handlers
|
| 205 |
def update_prompt(evt: gr.SelectData):
|
| 206 |
+
return STORY_STARTERS[evt.index[0]][1]
|
| 207 |
|
| 208 |
story_starters.select(update_prompt, None, prompt_input)
|
| 209 |
|
|
|
|
| 212 |
inputs=[prompt_input, model_choice],
|
| 213 |
outputs=[story_output, audio_output, gallery]
|
| 214 |
)
|
| 215 |
+
|
| 216 |
+
gallery.select(
|
| 217 |
+
fn=play_gallery_audio,
|
| 218 |
+
inputs=[gallery],
|
| 219 |
+
outputs=[audio_output]
|
| 220 |
+
)
|
| 221 |
|
| 222 |
if __name__ == "__main__":
|
| 223 |
demo.launch()
|