Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# Keep all existing imports and add any new ones needed
|
| 2 |
import gradio as gr
|
| 3 |
import random
|
| 4 |
from datetime import datetime
|
|
@@ -16,13 +15,62 @@ from gradio_client import Client
|
|
| 16 |
|
| 17 |
warnings.filterwarnings('ignore')
|
| 18 |
|
| 19 |
-
#
|
| 20 |
PAGE_SIZE = 10
|
| 21 |
-
FILE_DIR_PATH = "gallery"
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def list_all_outputs(generation_history):
|
| 28 |
"""Load all story generations for community view"""
|
|
@@ -57,6 +105,53 @@ def increase_list_size(list_size):
|
|
| 57 |
"""Increase the number of visible community generations"""
|
| 58 |
return list_size + PAGE_SIZE
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Add CSS for community generations
|
| 61 |
css = '''
|
| 62 |
#live_gen:before {
|
|
@@ -77,7 +172,7 @@ css = '''
|
|
| 77 |
}
|
| 78 |
'''
|
| 79 |
|
| 80 |
-
#
|
| 81 |
with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
| 82 |
gr.Markdown("""
|
| 83 |
# ๐ญ AI Story Generator & Narrator
|
|
@@ -89,7 +184,6 @@ with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
|
| 89 |
list_size = gr.Number(value=PAGE_SIZE, visible=False)
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
-
# Keep existing left column
|
| 93 |
with gr.Column(scale=3):
|
| 94 |
with gr.Row():
|
| 95 |
prompt_input = gr.Textbox(
|
|
@@ -122,7 +216,7 @@ with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
|
| 122 |
type="filepath"
|
| 123 |
)
|
| 124 |
|
| 125 |
-
#
|
| 126 |
with gr.Column(scale=1):
|
| 127 |
gr.Markdown("### ๐ Story Starters")
|
| 128 |
story_starters = gr.Dataframe(
|
|
@@ -131,7 +225,7 @@ with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
|
| 131 |
interactive=False
|
| 132 |
)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
with gr.Column(elem_id="live_gen") as community_list:
|
| 136 |
gr.Markdown("### ๐ฌ Community Stories")
|
| 137 |
with gr.Column(elem_id="live_gen_items"):
|
|
@@ -141,7 +235,7 @@ with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
|
| 141 |
history_list_latest = history_list[:list_size]
|
| 142 |
|
| 143 |
for audio_path in history_list_latest:
|
| 144 |
-
if not audio_path:
|
| 145 |
continue
|
| 146 |
|
| 147 |
try:
|
|
@@ -166,7 +260,7 @@ with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
|
| 166 |
load_more = gr.Button("Load More Stories")
|
| 167 |
load_more.click(fn=increase_list_size, inputs=list_size, outputs=list_size)
|
| 168 |
|
| 169 |
-
#
|
| 170 |
def update_prompt(evt: gr.SelectData):
|
| 171 |
return STORY_STARTERS[evt.index[0]][1]
|
| 172 |
|
|
@@ -175,10 +269,10 @@ with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
|
| 175 |
generate_btn.click(
|
| 176 |
fn=process_story_and_audio,
|
| 177 |
inputs=[prompt_input, model_choice],
|
| 178 |
-
outputs=[story_output, audio_output
|
| 179 |
)
|
| 180 |
|
| 181 |
-
#
|
| 182 |
demo.load(fn=list_all_outputs, inputs=generation_history, outputs=[generation_history, community_list], every=2)
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
from datetime import datetime
|
|
|
|
| 15 |
|
| 16 |
warnings.filterwarnings('ignore')
|
| 17 |
|
| 18 |
+
# Initialize constants
|
| 19 |
PAGE_SIZE = 10
|
| 20 |
+
FILE_DIR_PATH = "gallery"
|
| 21 |
|
| 22 |
+
# Initialize story starters
|
| 23 |
+
STORY_STARTERS = [
|
| 24 |
+
['Adventure', 'In a hidden temple deep in the Amazon...'],
|
| 25 |
+
['Mystery', 'The detective found an unusual note...'],
|
| 26 |
+
['Romance', 'Two strangers meet on a rainy evening...'],
|
| 27 |
+
['Sci-Fi', 'The space station received an unexpected signal...'],
|
| 28 |
+
['Fantasy', 'A magical portal appeared in the garden...'],
|
| 29 |
+
['Comedy-Sitcom', 'The new roommate arrived with seven emotional support animals...'],
|
| 30 |
+
['Comedy-Workplace', 'The office printer started sending mysterious messages...'],
|
| 31 |
+
['Comedy-Family', 'Grandma decided to become a social media influencer...'],
|
| 32 |
+
['Comedy-Supernatural', 'The ghost haunting the house was absolutely terrible at scaring people...'],
|
| 33 |
+
['Comedy-Travel', 'The GPS insisted on giving directions in interpretive dance descriptions...']
|
| 34 |
+
]
|
| 35 |
|
| 36 |
+
# Initialize client outside of interface definition
|
| 37 |
+
arxiv_client = None
|
| 38 |
+
|
| 39 |
+
def init_client():
|
| 40 |
+
global arxiv_client
|
| 41 |
+
if arxiv_client is None:
|
| 42 |
+
arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 43 |
+
return arxiv_client
|
| 44 |
+
|
| 45 |
+
def save_story(story, audio_path):
|
| 46 |
+
"""Save story and audio to gallery"""
|
| 47 |
+
try:
|
| 48 |
+
# Create gallery directory if it doesn't exist
|
| 49 |
+
gallery_dir = Path(FILE_DIR_PATH)
|
| 50 |
+
gallery_dir.mkdir(exist_ok=True)
|
| 51 |
+
|
| 52 |
+
# Generate timestamp for unique filename
|
| 53 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 54 |
+
|
| 55 |
+
# Get first line for title
|
| 56 |
+
first_line = story.split('\n')[0].strip()
|
| 57 |
+
safe_name = re.sub(r'[^\w\s-]', '', first_line)[:50]
|
| 58 |
+
|
| 59 |
+
# Save story text as markdown
|
| 60 |
+
story_path = gallery_dir / f"story_{timestamp}_{safe_name}.md"
|
| 61 |
+
with open(story_path, "w") as f:
|
| 62 |
+
f.write(f"# {first_line}\n\n{story}")
|
| 63 |
+
|
| 64 |
+
# Copy audio file to gallery
|
| 65 |
+
new_audio_path = None
|
| 66 |
+
if audio_path:
|
| 67 |
+
new_audio_path = gallery_dir / f"audio_{timestamp}_{safe_name}.mp3"
|
| 68 |
+
os.system(f"cp {audio_path} {str(new_audio_path)}")
|
| 69 |
+
|
| 70 |
+
return str(story_path), str(new_audio_path) if new_audio_path else None
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error saving to gallery: {str(e)}")
|
| 73 |
+
return None, None
|
| 74 |
|
| 75 |
def list_all_outputs(generation_history):
|
| 76 |
"""Load all story generations for community view"""
|
|
|
|
| 105 |
"""Increase the number of visible community generations"""
|
| 106 |
return list_size + PAGE_SIZE
|
| 107 |
|
| 108 |
+
def generate_story(prompt, model_choice):
|
| 109 |
+
"""Generate story using specified model"""
|
| 110 |
+
try:
|
| 111 |
+
client = init_client()
|
| 112 |
+
if client is None:
|
| 113 |
+
return "Error: Story generation service is not available."
|
| 114 |
+
|
| 115 |
+
result = client.predict(
|
| 116 |
+
prompt=prompt,
|
| 117 |
+
llm_model_picked=model_choice,
|
| 118 |
+
stream_outputs=True,
|
| 119 |
+
api_name="/ask_llm"
|
| 120 |
+
)
|
| 121 |
+
return result
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return f"Error generating story: {str(e)}"
|
| 124 |
+
|
| 125 |
+
async def generate_speech(text, voice="en-US-AriaNeural"):
|
| 126 |
+
"""Generate speech from text"""
|
| 127 |
+
try:
|
| 128 |
+
communicate = edge_tts.Communicate(text, voice)
|
| 129 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 130 |
+
tmp_path = tmp_file.name
|
| 131 |
+
await communicate.save(tmp_path)
|
| 132 |
+
return tmp_path
|
| 133 |
+
except Exception as e:
|
| 134 |
+
print(f"Error in text2speech: {str(e)}")
|
| 135 |
+
return None
|
| 136 |
+
|
| 137 |
+
def process_story_and_audio(prompt, model_choice):
|
| 138 |
+
"""Process story, generate audio, and save to gallery"""
|
| 139 |
+
try:
|
| 140 |
+
# Generate story
|
| 141 |
+
story = generate_story(prompt, model_choice)
|
| 142 |
+
if isinstance(story, str) and story.startswith("Error"):
|
| 143 |
+
return story, None
|
| 144 |
+
|
| 145 |
+
# Generate audio
|
| 146 |
+
audio_path = asyncio.run(generate_speech(story))
|
| 147 |
+
|
| 148 |
+
# Save to gallery
|
| 149 |
+
story_path, saved_audio_path = save_story(story, audio_path)
|
| 150 |
+
|
| 151 |
+
return story, audio_path
|
| 152 |
+
except Exception as e:
|
| 153 |
+
return f"Error: {str(e)}", None
|
| 154 |
+
|
| 155 |
# Add CSS for community generations
|
| 156 |
css = '''
|
| 157 |
#live_gen:before {
|
|
|
|
| 172 |
}
|
| 173 |
'''
|
| 174 |
|
| 175 |
+
# Create the Gradio interface
|
| 176 |
with gr.Blocks(title="AI Story Generator", css=css) as demo:
|
| 177 |
gr.Markdown("""
|
| 178 |
# ๐ญ AI Story Generator & Narrator
|
|
|
|
| 184 |
list_size = gr.Number(value=PAGE_SIZE, visible=False)
|
| 185 |
|
| 186 |
with gr.Row():
|
|
|
|
| 187 |
with gr.Column(scale=3):
|
| 188 |
with gr.Row():
|
| 189 |
prompt_input = gr.Textbox(
|
|
|
|
| 216 |
type="filepath"
|
| 217 |
)
|
| 218 |
|
| 219 |
+
# Sidebar with Story Starters and Community Generations
|
| 220 |
with gr.Column(scale=1):
|
| 221 |
gr.Markdown("### ๐ Story Starters")
|
| 222 |
story_starters = gr.Dataframe(
|
|
|
|
| 225 |
interactive=False
|
| 226 |
)
|
| 227 |
|
| 228 |
+
# Community Generations section
|
| 229 |
with gr.Column(elem_id="live_gen") as community_list:
|
| 230 |
gr.Markdown("### ๐ฌ Community Stories")
|
| 231 |
with gr.Column(elem_id="live_gen_items"):
|
|
|
|
| 235 |
history_list_latest = history_list[:list_size]
|
| 236 |
|
| 237 |
for audio_path in history_list_latest:
|
| 238 |
+
if not audio_path or not os.path.exists(audio_path):
|
| 239 |
continue
|
| 240 |
|
| 241 |
try:
|
|
|
|
| 260 |
load_more = gr.Button("Load More Stories")
|
| 261 |
load_more.click(fn=increase_list_size, inputs=list_size, outputs=list_size)
|
| 262 |
|
| 263 |
+
# Event handlers
|
| 264 |
def update_prompt(evt: gr.SelectData):
|
| 265 |
return STORY_STARTERS[evt.index[0]][1]
|
| 266 |
|
|
|
|
| 269 |
generate_btn.click(
|
| 270 |
fn=process_story_and_audio,
|
| 271 |
inputs=[prompt_input, model_choice],
|
| 272 |
+
outputs=[story_output, audio_output]
|
| 273 |
)
|
| 274 |
|
| 275 |
+
# Auto-refresh for community generations
|
| 276 |
demo.load(fn=list_all_outputs, inputs=generation_history, outputs=[generation_history, community_list], every=2)
|
| 277 |
|
| 278 |
if __name__ == "__main__":
|