Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,17 +17,16 @@ warnings.filterwarnings('ignore')
|
|
| 17 |
# Initialize Gradio clients with public demo spaces
|
| 18 |
def initialize_clients():
|
| 19 |
try:
|
| 20 |
-
# Use a public
|
| 21 |
-
image_client = Client("
|
| 22 |
-
|
| 23 |
-
return image_client, arxiv_client
|
| 24 |
except Exception as e:
|
| 25 |
print(f"Error initializing clients: {str(e)}")
|
| 26 |
-
return None
|
| 27 |
|
| 28 |
if gr.NO_RELOAD:
|
| 29 |
-
# Initialize
|
| 30 |
-
|
| 31 |
|
| 32 |
STORY_GENRES = [
|
| 33 |
"Science Fiction",
|
|
@@ -73,10 +72,10 @@ def generate_story_prompt(base_prompt, genre, structure):
|
|
| 73 |
def generate_story(prompt, model_choice):
|
| 74 |
"""Generate story using specified model"""
|
| 75 |
try:
|
| 76 |
-
if
|
| 77 |
return "Error: Story generation service is not available."
|
| 78 |
|
| 79 |
-
result =
|
| 80 |
prompt,
|
| 81 |
model_choice,
|
| 82 |
True,
|
|
@@ -86,39 +85,6 @@ def generate_story(prompt, model_choice):
|
|
| 86 |
except Exception as e:
|
| 87 |
return f"Error generating story: {str(e)}"
|
| 88 |
|
| 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:
|
|
@@ -146,40 +112,18 @@ def story_generator_interface(prompt, genre, structure, model_choice, num_scenes
|
|
| 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 |
-
|
| 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
|
| 182 |
-
gr.Markdown("# ๐ญ AI Story Generator
|
| 183 |
|
| 184 |
with gr.Row():
|
| 185 |
with gr.Column():
|
|
@@ -217,26 +161,23 @@ with gr.Blocks(title="AI Story Generator & Visualizer") as demo:
|
|
| 217 |
value=50,
|
| 218 |
step=10
|
| 219 |
)
|
| 220 |
-
generate_btn = gr.Button("Generate Story
|
| 221 |
|
| 222 |
with gr.Row():
|
| 223 |
with gr.Column():
|
| 224 |
story_output = gr.Textbox(
|
| 225 |
label="Generated Story",
|
| 226 |
lines=10,
|
| 227 |
-
readonly=
|
| 228 |
)
|
| 229 |
-
with gr.Column():
|
| 230 |
-
gallery = gr.Gallery(label="Scene Visualizations")
|
| 231 |
|
| 232 |
with gr.Row():
|
| 233 |
audio_output = gr.Audio(label="Story Narration")
|
| 234 |
-
video_output = gr.Video(label="Story Video")
|
| 235 |
|
| 236 |
generate_btn.click(
|
| 237 |
fn=story_generator_interface,
|
| 238 |
inputs=[prompt_input, genre_input, structure_input, model_choice, num_scenes, words_per_scene],
|
| 239 |
-
outputs=[story_output,
|
| 240 |
)
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
|
|
|
| 17 |
# Initialize Gradio clients with public demo spaces
|
| 18 |
def initialize_clients():
|
| 19 |
try:
|
| 20 |
+
# Use a simpler public demo space
|
| 21 |
+
image_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 22 |
+
return image_client
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
print(f"Error initializing clients: {str(e)}")
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
if gr.NO_RELOAD:
|
| 28 |
+
# Initialize client in NO_RELOAD block to prevent multiple initializations
|
| 29 |
+
CLIENT = initialize_clients()
|
| 30 |
|
| 31 |
STORY_GENRES = [
|
| 32 |
"Science Fiction",
|
|
|
|
| 72 |
def generate_story(prompt, model_choice):
|
| 73 |
"""Generate story using specified model"""
|
| 74 |
try:
|
| 75 |
+
if CLIENT is None:
|
| 76 |
return "Error: Story generation service is not available."
|
| 77 |
|
| 78 |
+
result = CLIENT.predict(
|
| 79 |
prompt,
|
| 80 |
model_choice,
|
| 81 |
True,
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"Error generating story: {str(e)}"
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def process_story(story_text, num_scenes=5):
|
| 89 |
"""Break story into scenes for visualization"""
|
| 90 |
if not story_text:
|
|
|
|
| 112 |
if story.startswith("Error"):
|
| 113 |
return story, None, None, None
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Generate speech
|
| 116 |
audio_path = asyncio.run(generate_speech(story))
|
| 117 |
|
| 118 |
+
return story, None, audio_path, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
error_msg = f"An error occurred: {str(e)}"
|
| 122 |
return error_msg, None, None, None
|
| 123 |
|
| 124 |
# Create Gradio interface
|
| 125 |
+
with gr.Blocks(title="AI Story Generator") as demo:
|
| 126 |
+
gr.Markdown("# ๐ญ AI Story Generator")
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column():
|
|
|
|
| 161 |
value=50,
|
| 162 |
step=10
|
| 163 |
)
|
| 164 |
+
generate_btn = gr.Button("Generate Story")
|
| 165 |
|
| 166 |
with gr.Row():
|
| 167 |
with gr.Column():
|
| 168 |
story_output = gr.Textbox(
|
| 169 |
label="Generated Story",
|
| 170 |
lines=10,
|
| 171 |
+
interactive=False # Changed from readonly to interactive=False
|
| 172 |
)
|
|
|
|
|
|
|
| 173 |
|
| 174 |
with gr.Row():
|
| 175 |
audio_output = gr.Audio(label="Story Narration")
|
|
|
|
| 176 |
|
| 177 |
generate_btn.click(
|
| 178 |
fn=story_generator_interface,
|
| 179 |
inputs=[prompt_input, genre_input, structure_input, model_choice, num_scenes, words_per_scene],
|
| 180 |
+
outputs=[story_output, None, audio_output, None] # Set image and video outputs to None for now
|
| 181 |
)
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|