Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,124 @@
|
|
1 |
-
# app.py
|
2 |
import os
|
3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from transformers import pipeline
|
5 |
from diffusers import StableDiffusionPipeline
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
15 |
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
|
16 |
-
entities = ner_pipeline(
|
17 |
-
|
18 |
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
def
|
|
|
23 |
model_id = "CompVis/stable-diffusion-v1-4"
|
24 |
sd_pipeline = StableDiffusionPipeline.from_pretrained(model_id)
|
25 |
-
|
26 |
-
|
|
|
27 |
image = sd_pipeline(prompt).images[0]
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
# Gradio App Interface
|
32 |
-
def generate_video_from_text(input_text):
|
33 |
-
# Analyze text
|
34 |
-
summary, key_entities = analyze_text(input_text)
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
prompts = [f"{entity}, cinematic, ultra-realistic" for entity in key_entities]
|
38 |
-
|
|
|
39 |
|
40 |
-
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
-
|
|
|
|
|
|
1 |
import os
|
2 |
+
import subprocess
|
3 |
+
import glob
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Function to install missing dependencies
|
8 |
+
def install_dependencies():
|
9 |
+
packages = [
|
10 |
+
"groq",
|
11 |
+
"transformers",
|
12 |
+
"diffusers",
|
13 |
+
"gradio"
|
14 |
+
]
|
15 |
+
for package in packages:
|
16 |
+
try:
|
17 |
+
__import__(package)
|
18 |
+
except ImportError:
|
19 |
+
subprocess.check_call(["pip", "install", package])
|
20 |
+
|
21 |
+
# Install dependencies
|
22 |
+
install_dependencies()
|
23 |
+
|
24 |
+
# Import dependencies
|
25 |
+
from groq import Groq
|
26 |
from transformers import pipeline
|
27 |
from diffusers import StableDiffusionPipeline
|
28 |
+
import gradio as gr
|
29 |
|
30 |
+
# Validate GROQ_API_KEY environment variable
|
31 |
+
def validate_groq_api_key():
|
32 |
+
if not os.environ.get("GROQ_API_KEY"):
|
33 |
+
# Set default API key if not present
|
34 |
+
os.environ["GROQ_API_KEY"] = "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"
|
35 |
|
36 |
+
# Initialize Groq Client
|
37 |
+
validate_groq_api_key()
|
38 |
+
client = Groq(
|
39 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
40 |
+
)
|
41 |
+
|
42 |
+
# Example Groq Usage
|
43 |
+
def fetch_groq_completion(prompt):
|
44 |
+
chat_completion = client.chat.completions.create(
|
45 |
+
messages=[
|
46 |
+
{
|
47 |
+
"role": "user",
|
48 |
+
"content": prompt,
|
49 |
+
}
|
50 |
+
],
|
51 |
+
model="llama3-8b-8192",
|
52 |
+
stream=False,
|
53 |
+
)
|
54 |
+
return chat_completion.choices[0].message.content
|
55 |
|
56 |
+
# Text Understanding with Hugging Face Transformers
|
57 |
+
def extract_key_entities(text):
|
58 |
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
|
59 |
+
entities = ner_pipeline(text)
|
60 |
+
return [entity["word"] for entity in entities]
|
61 |
|
62 |
+
def summarize_text(text):
|
63 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
64 |
+
summary = summarizer(text, max_length=50, min_length=10, do_sample=False)
|
65 |
+
return summary[0]['summary_text']
|
66 |
|
67 |
+
# Frame Generation using Stable Diffusion
|
68 |
+
def generate_frames(prompts, output_dir="frames"):
|
69 |
+
os.makedirs(output_dir, exist_ok=True)
|
70 |
model_id = "CompVis/stable-diffusion-v1-4"
|
71 |
sd_pipeline = StableDiffusionPipeline.from_pretrained(model_id)
|
72 |
+
|
73 |
+
frames = []
|
74 |
+
for i, prompt in enumerate(prompts):
|
75 |
image = sd_pipeline(prompt).images[0]
|
76 |
+
frame_path = os.path.join(output_dir, f"frame_{i:04d}.png")
|
77 |
+
image.save(frame_path)
|
78 |
+
frames.append(frame_path)
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
return frames
|
81 |
+
|
82 |
+
# Video Stitching with FFmpeg
|
83 |
+
def create_video_from_frames(frames_dir, output_video="output.mp4", fps=24):
|
84 |
+
frame_pattern = os.path.join(frames_dir, "frame_%04d.png")
|
85 |
+
command = [
|
86 |
+
"ffmpeg", "-y", "-framerate", str(fps), "-i", frame_pattern,
|
87 |
+
"-c:v", "libx264", "-pix_fmt", "yuv420p", output_video
|
88 |
+
]
|
89 |
+
subprocess.run(command, check=True)
|
90 |
+
return output_video
|
91 |
+
|
92 |
+
# Gradio Interface for Final Output
|
93 |
+
def generate_video_interface(prompt):
|
94 |
+
# Step 1: Fetch understanding from Groq
|
95 |
+
groq_response = fetch_groq_completion(prompt)
|
96 |
+
|
97 |
+
# Step 2: Extract entities and summarize
|
98 |
+
key_entities = extract_key_entities(groq_response)
|
99 |
+
summary = summarize_text(groq_response)
|
100 |
+
|
101 |
+
# Step 3: Generate frames
|
102 |
prompts = [f"{entity}, cinematic, ultra-realistic" for entity in key_entities]
|
103 |
+
frame_dir = "frames"
|
104 |
+
generate_frames(prompts, output_dir=frame_dir)
|
105 |
|
106 |
+
# Step 4: Create video
|
107 |
+
video_path = create_video_from_frames(frame_dir)
|
108 |
+
|
109 |
+
return video_path
|
110 |
|
111 |
+
# Launch Gradio App
|
112 |
+
def gradio_ui():
|
113 |
+
interface = gr.Interface(
|
114 |
+
fn=generate_video_interface,
|
115 |
+
inputs="text",
|
116 |
+
outputs="video",
|
117 |
+
title="Text-to-Video Generator",
|
118 |
+
description="Generate videos from text descriptions using open-source AI tools."
|
119 |
+
)
|
120 |
+
interface.launch()
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
+
gradio_ui()
|
124 |
+
|