Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,27 @@ import os
|
|
| 6 |
import edge_tts
|
| 7 |
import asyncio
|
| 8 |
import warnings
|
| 9 |
-
from gradio_client import Client
|
| 10 |
import pytz
|
| 11 |
import re
|
| 12 |
import json
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
warnings.filterwarnings('ignore')
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Initialize client outside of interface definition
|
| 17 |
arxiv_client = None
|
| 18 |
|
|
@@ -22,6 +36,58 @@ def init_client():
|
|
| 22 |
arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 23 |
return arxiv_client
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def generate_story(prompt, model_choice):
|
| 26 |
"""Generate story using specified model"""
|
| 27 |
try:
|
|
@@ -52,63 +118,90 @@ async def generate_speech(text, voice="en-US-AriaNeural"):
|
|
| 52 |
return None
|
| 53 |
|
| 54 |
def process_story_and_audio(prompt, model_choice):
|
| 55 |
-
"""Process story and
|
| 56 |
try:
|
| 57 |
# Generate story
|
| 58 |
story = generate_story(prompt, model_choice)
|
| 59 |
if isinstance(story, str) and story.startswith("Error"):
|
| 60 |
-
return story, None
|
| 61 |
-
|
| 62 |
# Generate audio
|
| 63 |
audio_path = asyncio.run(generate_speech(story))
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
-
return f"Error: {str(e)}", None
|
| 68 |
|
| 69 |
# Create the Gradio interface
|
| 70 |
with gr.Blocks(title="AI Story Generator") as demo:
|
| 71 |
gr.Markdown("""
|
| 72 |
# ๐ญ AI Story Generator & Narrator
|
| 73 |
-
Generate creative stories
|
| 74 |
""")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
-
with gr.Column():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
value="mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 90 |
)
|
| 91 |
-
generate_btn = gr.Button("Generate Story")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
lines=10,
|
| 97 |
-
interactive=False
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
audio_output = gr.Audio(
|
| 102 |
-
label="Story Narration",
|
| 103 |
-
type="filepath"
|
| 104 |
-
)
|
| 105 |
|
| 106 |
generate_btn.click(
|
| 107 |
fn=process_story_and_audio,
|
| 108 |
inputs=[prompt_input, model_choice],
|
| 109 |
-
outputs=[story_output, audio_output]
|
| 110 |
)
|
| 111 |
|
| 112 |
-
# Launch the app using the current pattern
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
|
|
|
| 6 |
import edge_tts
|
| 7 |
import asyncio
|
| 8 |
import warnings
|
|
|
|
| 9 |
import pytz
|
| 10 |
import re
|
| 11 |
import json
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
from gradio_client import Client
|
| 15 |
|
| 16 |
warnings.filterwarnings('ignore')
|
| 17 |
|
| 18 |
+
# Initialize story starters
|
| 19 |
+
STORY_STARTERS = pd.DataFrame({
|
| 20 |
+
'category': ['Adventure', 'Mystery', 'Romance', 'Sci-Fi', 'Fantasy'],
|
| 21 |
+
'starter': [
|
| 22 |
+
'In a hidden temple deep in the Amazon...',
|
| 23 |
+
'The detective found an unusual note...',
|
| 24 |
+
'Two strangers meet on a rainy evening...',
|
| 25 |
+
'The space station received an unexpected signal...',
|
| 26 |
+
'A magical portal appeared in the garden...'
|
| 27 |
+
]
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
# Initialize client outside of interface definition
|
| 31 |
arxiv_client = None
|
| 32 |
|
|
|
|
| 36 |
arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 37 |
return arxiv_client
|
| 38 |
|
| 39 |
+
def save_story(story, audio_path):
|
| 40 |
+
"""Save story and audio to gallery"""
|
| 41 |
+
try:
|
| 42 |
+
# Create gallery directory if it doesn't exist
|
| 43 |
+
gallery_dir = Path("gallery")
|
| 44 |
+
gallery_dir.mkdir(exist_ok=True)
|
| 45 |
+
|
| 46 |
+
# Generate timestamp for unique filename
|
| 47 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 48 |
+
|
| 49 |
+
# Save story text
|
| 50 |
+
story_path = gallery_dir / f"story_{timestamp}.txt"
|
| 51 |
+
with open(story_path, "w") as f:
|
| 52 |
+
f.write(story)
|
| 53 |
+
|
| 54 |
+
# Copy audio file to gallery
|
| 55 |
+
if audio_path:
|
| 56 |
+
new_audio_path = gallery_dir / f"audio_{timestamp}.mp3"
|
| 57 |
+
os.system(f"cp {audio_path} {str(new_audio_path)}")
|
| 58 |
+
|
| 59 |
+
return str(story_path), str(new_audio_path)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Error saving to gallery: {str(e)}")
|
| 62 |
+
return None, None
|
| 63 |
+
|
| 64 |
+
def load_gallery():
|
| 65 |
+
"""Load all stories and audio from gallery"""
|
| 66 |
+
try:
|
| 67 |
+
gallery_dir = Path("gallery")
|
| 68 |
+
if not gallery_dir.exists():
|
| 69 |
+
return []
|
| 70 |
+
|
| 71 |
+
files = []
|
| 72 |
+
for story_file in gallery_dir.glob("story_*.txt"):
|
| 73 |
+
timestamp = story_file.stem.split('_')[1]
|
| 74 |
+
audio_file = gallery_dir / f"audio_{timestamp}.mp3"
|
| 75 |
+
|
| 76 |
+
with open(story_file) as f:
|
| 77 |
+
story_text = f.read()
|
| 78 |
+
|
| 79 |
+
files.append({
|
| 80 |
+
"timestamp": timestamp,
|
| 81 |
+
"story_path": str(story_file),
|
| 82 |
+
"audio_path": str(audio_file) if audio_file.exists() else None,
|
| 83 |
+
"story_preview": story_text[:100] + "..."
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
return sorted(files, key=lambda x: x["timestamp"], reverse=True)
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"Error loading gallery: {str(e)}")
|
| 89 |
+
return []
|
| 90 |
+
|
| 91 |
def generate_story(prompt, model_choice):
|
| 92 |
"""Generate story using specified model"""
|
| 93 |
try:
|
|
|
|
| 118 |
return None
|
| 119 |
|
| 120 |
def process_story_and_audio(prompt, model_choice):
|
| 121 |
+
"""Process story, generate audio, and save to gallery"""
|
| 122 |
try:
|
| 123 |
# Generate story
|
| 124 |
story = generate_story(prompt, model_choice)
|
| 125 |
if isinstance(story, str) and story.startswith("Error"):
|
| 126 |
+
return story, None, None
|
| 127 |
+
|
| 128 |
# Generate audio
|
| 129 |
audio_path = asyncio.run(generate_speech(story))
|
| 130 |
|
| 131 |
+
# Save to gallery
|
| 132 |
+
story_path, saved_audio_path = save_story(story, audio_path)
|
| 133 |
+
|
| 134 |
+
return story, audio_path, load_gallery()
|
| 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("""
|
| 141 |
# ๐ญ AI Story Generator & Narrator
|
| 142 |
+
Generate creative stories, listen to them, and build your gallery!
|
| 143 |
""")
|
| 144 |
|
| 145 |
with gr.Row():
|
| 146 |
+
with gr.Column(scale=3):
|
| 147 |
+
with gr.Row():
|
| 148 |
+
prompt_input = gr.Textbox(
|
| 149 |
+
label="Story Concept",
|
| 150 |
+
placeholder="Enter your story idea...",
|
| 151 |
+
lines=3
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
with gr.Row():
|
| 155 |
+
model_choice = gr.Dropdown(
|
| 156 |
+
label="Model",
|
| 157 |
+
choices=[
|
| 158 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 159 |
+
"mistralai/Mistral-7B-Instruct-v0.2"
|
| 160 |
+
],
|
| 161 |
+
value="mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 162 |
+
)
|
| 163 |
+
generate_btn = gr.Button("Generate Story")
|
| 164 |
+
|
| 165 |
+
with gr.Row():
|
| 166 |
+
story_output = gr.Textbox(
|
| 167 |
+
label="Generated Story",
|
| 168 |
+
lines=10,
|
| 169 |
+
interactive=False
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
with gr.Row():
|
| 173 |
+
audio_output = gr.Audio(
|
| 174 |
+
label="Story Narration",
|
| 175 |
+
type="filepath"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Sidebar with Story Starters and Gallery
|
| 179 |
+
with gr.Column(scale=1):
|
| 180 |
+
gr.Markdown("### ๐ Story Starters")
|
| 181 |
+
story_starters = gr.Dataframe(
|
| 182 |
+
value=STORY_STARTERS,
|
| 183 |
+
headers=["category", "starter"],
|
| 184 |
+
interactive=False
|
| 185 |
)
|
| 186 |
+
|
| 187 |
+
gr.Markdown("### ๐ฌ Gallery")
|
| 188 |
+
gallery = gr.Dataframe(
|
| 189 |
+
value=load_gallery(),
|
| 190 |
+
headers=["timestamp", "story_preview"],
|
| 191 |
+
interactive=False
|
|
|
|
| 192 |
)
|
|
|
|
| 193 |
|
| 194 |
+
# Event handlers
|
| 195 |
+
def update_prompt(evt: gr.SelectData):
|
| 196 |
+
return STORY_STARTERS.iloc[evt.index[0]]["starter"]
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
story_starters.select(update_prompt, None, prompt_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
generate_btn.click(
|
| 201 |
fn=process_story_and_audio,
|
| 202 |
inputs=[prompt_input, model_choice],
|
| 203 |
+
outputs=[story_output, audio_output, gallery]
|
| 204 |
)
|
| 205 |
|
|
|
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch()
|