Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,9 +14,20 @@ import numpy as np
|
|
| 14 |
|
| 15 |
warnings.filterwarnings('ignore')
|
| 16 |
|
| 17 |
-
# Initialize
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
STORY_GENRES = [
|
| 22 |
"Science Fiction",
|
|
@@ -46,7 +57,7 @@ async def generate_speech(text, voice="en-US-AriaNeural"):
|
|
| 46 |
return tmp_path
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error in text2speech: {str(e)}")
|
| 49 |
-
|
| 50 |
|
| 51 |
def generate_story_prompt(base_prompt, genre, structure):
|
| 52 |
"""Generate an expanded story prompt based on genre and structure"""
|
|
@@ -62,7 +73,10 @@ def generate_story_prompt(base_prompt, genre, structure):
|
|
| 62 |
def generate_story(prompt, model_choice):
|
| 63 |
"""Generate story using specified model"""
|
| 64 |
try:
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
prompt,
|
| 67 |
model_choice,
|
| 68 |
True,
|
|
@@ -75,28 +89,41 @@ def generate_story(prompt, model_choice):
|
|
| 75 |
def generate_image_from_text(text_prompt):
|
| 76 |
"""Generate an image from text description"""
|
| 77 |
try:
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
text_prompt,
|
| 80 |
-
|
| 81 |
-
guidance_scale=7.5,
|
| 82 |
-
width=768,
|
| 83 |
-
height=512,
|
| 84 |
-
api_name="/text2image"
|
| 85 |
)
|
| 86 |
return result
|
| 87 |
except Exception as e:
|
|
|
|
| 88 |
return None
|
| 89 |
|
| 90 |
def create_video_from_images(image_paths, durations):
|
| 91 |
"""Create video from a series of images"""
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
def process_story(story_text, num_scenes=5):
|
| 99 |
"""Break story into scenes for visualization"""
|
|
|
|
|
|
|
|
|
|
| 100 |
sentences = story_text.split('.')
|
| 101 |
scenes = []
|
| 102 |
scene_length = max(1, len(sentences) // num_scenes)
|
|
@@ -110,33 +137,45 @@ def process_story(story_text, num_scenes=5):
|
|
| 110 |
|
| 111 |
def story_generator_interface(prompt, genre, structure, model_choice, num_scenes, words_per_scene):
|
| 112 |
"""Main story generation and multimedia creation function"""
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
# Create Gradio interface
|
| 142 |
with gr.Blocks(title="AI Story Generator & Visualizer") as demo:
|
|
|
|
| 14 |
|
| 15 |
warnings.filterwarnings('ignore')
|
| 16 |
|
| 17 |
+
# Initialize Gradio clients with public demo spaces
|
| 18 |
+
def initialize_clients():
|
| 19 |
+
try:
|
| 20 |
+
# Use a public Stable Diffusion demo space instead of SDXL
|
| 21 |
+
image_client = Client("gradio/stable-diffusion-2")
|
| 22 |
+
arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 23 |
+
return image_client, arxiv_client
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Error initializing clients: {str(e)}")
|
| 26 |
+
return None, None
|
| 27 |
+
|
| 28 |
+
if gr.NO_RELOAD:
|
| 29 |
+
# Initialize clients in NO_RELOAD block to prevent multiple initializations
|
| 30 |
+
IMAGE_CLIENT, ARXIV_CLIENT = initialize_clients()
|
| 31 |
|
| 32 |
STORY_GENRES = [
|
| 33 |
"Science Fiction",
|
|
|
|
| 57 |
return tmp_path
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error in text2speech: {str(e)}")
|
| 60 |
+
return None
|
| 61 |
|
| 62 |
def generate_story_prompt(base_prompt, genre, structure):
|
| 63 |
"""Generate an expanded story prompt based on genre and structure"""
|
|
|
|
| 73 |
def generate_story(prompt, model_choice):
|
| 74 |
"""Generate story using specified model"""
|
| 75 |
try:
|
| 76 |
+
if ARXIV_CLIENT is None:
|
| 77 |
+
return "Error: Story generation service is not available."
|
| 78 |
+
|
| 79 |
+
result = ARXIV_CLIENT.predict(
|
| 80 |
prompt,
|
| 81 |
model_choice,
|
| 82 |
True,
|
|
|
|
| 89 |
def generate_image_from_text(text_prompt):
|
| 90 |
"""Generate an image from text description"""
|
| 91 |
try:
|
| 92 |
+
if IMAGE_CLIENT is None:
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
+
result = IMAGE_CLIENT.predict(
|
| 96 |
text_prompt,
|
| 97 |
+
api_name="/predict" # Updated API endpoint for the public demo
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
return result
|
| 100 |
except Exception as e:
|
| 101 |
+
print(f"Error generating image: {str(e)}")
|
| 102 |
return None
|
| 103 |
|
| 104 |
def create_video_from_images(image_paths, durations):
|
| 105 |
"""Create video from a series of images"""
|
| 106 |
+
try:
|
| 107 |
+
if not image_paths:
|
| 108 |
+
return None
|
| 109 |
+
|
| 110 |
+
clips = [ImageClip(img_path).set_duration(dur) for img_path, dur in zip(image_paths, durations) if os.path.exists(img_path)]
|
| 111 |
+
if not clips:
|
| 112 |
+
return None
|
| 113 |
+
|
| 114 |
+
final_clip = concatenate_videoclips(clips, method="compose")
|
| 115 |
+
output_path = tempfile.mktemp(suffix=".mp4")
|
| 116 |
+
final_clip.write_videofile(output_path, fps=24)
|
| 117 |
+
return output_path
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error creating video: {str(e)}")
|
| 120 |
+
return None
|
| 121 |
|
| 122 |
def process_story(story_text, num_scenes=5):
|
| 123 |
"""Break story into scenes for visualization"""
|
| 124 |
+
if not story_text:
|
| 125 |
+
return []
|
| 126 |
+
|
| 127 |
sentences = story_text.split('.')
|
| 128 |
scenes = []
|
| 129 |
scene_length = max(1, len(sentences) // num_scenes)
|
|
|
|
| 137 |
|
| 138 |
def story_generator_interface(prompt, genre, structure, model_choice, num_scenes, words_per_scene):
|
| 139 |
"""Main story generation and multimedia creation function"""
|
| 140 |
+
try:
|
| 141 |
+
# Generate expanded prompt
|
| 142 |
+
story_prompt = generate_story_prompt(prompt, genre, structure)
|
| 143 |
+
|
| 144 |
+
# Generate story
|
| 145 |
+
story = generate_story(story_prompt, model_choice)
|
| 146 |
+
if story.startswith("Error"):
|
| 147 |
+
return story, None, None, None
|
| 148 |
+
|
| 149 |
+
# Process story into scenes
|
| 150 |
+
scenes = process_story(story, num_scenes)
|
| 151 |
+
|
| 152 |
+
# Generate images for each scene
|
| 153 |
+
image_paths = []
|
| 154 |
+
for scene in scenes:
|
| 155 |
+
image = generate_image_from_text(scene)
|
| 156 |
+
if image is not None:
|
| 157 |
+
if isinstance(image, (str, bytes)):
|
| 158 |
+
image_paths.append(image)
|
| 159 |
+
else:
|
| 160 |
+
temp_path = tempfile.mktemp(suffix=".png")
|
| 161 |
+
Image.fromarray(image).save(temp_path)
|
| 162 |
+
image_paths.append(temp_path)
|
| 163 |
+
|
| 164 |
+
# Generate speech
|
| 165 |
+
audio_path = asyncio.run(generate_speech(story))
|
| 166 |
+
|
| 167 |
+
# Create video if we have images
|
| 168 |
+
if image_paths:
|
| 169 |
+
scene_durations = [5.0] * len(image_paths) # 5 seconds per scene
|
| 170 |
+
video_path = create_video_from_images(image_paths, scene_durations)
|
| 171 |
+
else:
|
| 172 |
+
video_path = None
|
| 173 |
+
|
| 174 |
+
return story, image_paths, audio_path, video_path
|
| 175 |
+
|
| 176 |
+
except Exception as e:
|
| 177 |
+
error_msg = f"An error occurred: {str(e)}"
|
| 178 |
+
return error_msg, None, None, None
|
| 179 |
|
| 180 |
# Create Gradio interface
|
| 181 |
with gr.Blocks(title="AI Story Generator & Visualizer") as demo:
|