files
Browse files
app.py
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
from sympy import true
|
| 7 |
+
from extract_metadata import get_metadata
|
| 8 |
+
from find_steps import find_all_steps
|
| 9 |
+
from video_editing import crop_video, get_final_video
|
| 10 |
+
import json
|
| 11 |
+
from video_editing_ffmpeg import concatenate_videos
|
| 12 |
+
|
| 13 |
+
from grok_analyze import get_story_with_grok
|
| 14 |
+
# Placeholder function for video generation
|
| 15 |
+
def generate_video(metadata, filenames,current_context):
|
| 16 |
+
"""
|
| 17 |
+
Generate a video using context and metadata.
|
| 18 |
+
In a real implementation, this would process metadata to create a video.
|
| 19 |
+
"""
|
| 20 |
+
if not metadata or not filenames:
|
| 21 |
+
return None, "No metadata or files provided."
|
| 22 |
+
|
| 23 |
+
print("Metadata:", metadata)
|
| 24 |
+
print("Filenames:", filenames)
|
| 25 |
+
print("context:", current_context)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
steps=get_story_with_grok(metadata, current_context)
|
| 29 |
+
print("here are the steps", steps)
|
| 30 |
+
|
| 31 |
+
# steps=find_all_steps(metadata,"nothing")
|
| 32 |
+
final_story=""
|
| 33 |
+
|
| 34 |
+
all_videos=[]
|
| 35 |
+
for step in steps:
|
| 36 |
+
# v=crop_video(step)
|
| 37 |
+
print("cropped!")
|
| 38 |
+
# all_videos.append(v)
|
| 39 |
+
final_story=final_story+ '\n' +step['description']
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# final_video= get_final_video(all_videos)
|
| 43 |
+
|
| 44 |
+
final_video=concatenate_videos(steps)
|
| 45 |
+
# Example: In a real scenario, use metadata and context to generate a video
|
| 46 |
+
return final_video, f"Video generated with context:",final_story
|
| 47 |
+
|
| 48 |
+
# Function to handle file uploads
|
| 49 |
+
def upload_files(uploaded_files):
|
| 50 |
+
if not uploaded_files:
|
| 51 |
+
return None, None, "No files uploaded."
|
| 52 |
+
|
| 53 |
+
# Extract metadata and filenames
|
| 54 |
+
metadata_file=""
|
| 55 |
+
|
| 56 |
+
for file in uploaded_files:
|
| 57 |
+
if file.name.lower().endswith('.json'):
|
| 58 |
+
metadata_file=file.name
|
| 59 |
+
|
| 60 |
+
if metadata_file=="":
|
| 61 |
+
metadata, filename = get_metadata(uploaded_files)
|
| 62 |
+
else:
|
| 63 |
+
|
| 64 |
+
with open(metadata_file, 'r') as file:
|
| 65 |
+
metadata = json.load(file)
|
| 66 |
+
return metadata, metadata_file, "Files uploaded successfully."
|
| 67 |
+
|
| 68 |
+
print("Uploaded files:", filename)
|
| 69 |
+
print("Metadata:", metadata)
|
| 70 |
+
|
| 71 |
+
return metadata, filename, "Files uploaded successfully."
|
| 72 |
+
|
| 73 |
+
# Gradio interface
|
| 74 |
+
with gr.Blocks() as demo:
|
| 75 |
+
gr.Markdown("# Story Generation App")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
file_input = gr.File(label="Upload Images, Videos or metadata", file_count="multiple", file_types=["image", "video",'.json'])
|
| 80 |
+
upload_button = gr.Button("Upload Files (video, image, metadata)")
|
| 81 |
+
generate_button = gr.Button("Generate Story!")
|
| 82 |
+
context = gr.Textbox(label="Context")
|
| 83 |
+
generated_story = gr.Textbox(label="Generated Story")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
with gr.Column():
|
| 87 |
+
video_output = gr.Video(label="Generated Video")
|
| 88 |
+
upload_output = gr.Textbox(label="Upload Status")
|
| 89 |
+
generation_status = gr.Textbox(label="Generation Status")
|
| 90 |
+
|
| 91 |
+
# State to store metadata and filenames
|
| 92 |
+
metadata_state = gr.State()
|
| 93 |
+
filenames_state = gr.State()
|
| 94 |
+
|
| 95 |
+
# Connect buttons to functions
|
| 96 |
+
upload_button.click(
|
| 97 |
+
fn=upload_files,
|
| 98 |
+
inputs=file_input,
|
| 99 |
+
outputs=[metadata_state, filenames_state, upload_output]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
generate_button.click(
|
| 103 |
+
fn=generate_video,
|
| 104 |
+
inputs=[metadata_state, filenames_state, context],
|
| 105 |
+
outputs=[video_output, generation_status,generated_story]
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
# Launch the app (Pyodide-compatible launch)
|
| 112 |
+
demo.launch(share=true)
|