Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,92 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from moviepy.editor import ImageClip, concatenate_videoclips, TextClip, CompositeVideoClip
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
IMAGE_DB = {
|
| 9 |
"haus": [
|
| 10 |
-
"https://
|
| 11 |
-
"https://
|
| 12 |
],
|
| 13 |
"geist": [
|
| 14 |
-
"https://
|
| 15 |
-
"https://
|
| 16 |
],
|
| 17 |
"spuk": [
|
| 18 |
-
"https://
|
| 19 |
-
"https://
|
| 20 |
],
|
| 21 |
"katze": [
|
| 22 |
-
"https://
|
| 23 |
],
|
| 24 |
"ritual": [
|
| 25 |
-
"https://
|
| 26 |
],
|
| 27 |
}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
for keyword
|
| 34 |
-
if keyword in
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Sicheres Bild-Download mit Prüfung
|
| 42 |
def download_image(url):
|
| 43 |
try:
|
| 44 |
-
response = requests.get(url
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return img
|
| 48 |
-
else:
|
| 49 |
-
print(f"WARNUNG: Ungültiges Bild: {url}")
|
| 50 |
-
return None
|
| 51 |
except Exception as e:
|
| 52 |
-
print(f"
|
| 53 |
return None
|
| 54 |
|
| 55 |
-
# Video erstellen aus Titel & Bildern
|
| 56 |
def create_video(title):
|
| 57 |
-
image_urls =
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
for i, url in enumerate(image_urls):
|
| 62 |
img = download_image(url)
|
| 63 |
-
if img
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
img_clip = ImageClip(img).set_duration(duration_per_image)
|
| 67 |
-
|
| 68 |
-
# Text-Overlay
|
| 69 |
-
txt = TextClip(
|
| 70 |
-
f"{title}\nSzene {i+1}",
|
| 71 |
-
fontsize=40,
|
| 72 |
-
color='white',
|
| 73 |
-
font='Amiri-Bold',
|
| 74 |
-
method='caption',
|
| 75 |
-
size=img.size
|
| 76 |
-
)
|
| 77 |
-
txt = txt.set_duration(duration_per_image).set_pos('bottom').margin(bottom=30, opacity=0)
|
| 78 |
-
|
| 79 |
-
video_clip = CompositeVideoClip([img_clip, txt])
|
| 80 |
-
clips.append(video_clip)
|
| 81 |
-
|
| 82 |
-
if not clips:
|
| 83 |
raise RuntimeError("Keine gültigen Bilder gefunden. Bitte gib einen anderen Titel ein.")
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
# Gradio-Funktion
|
| 91 |
def run(title):
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
iface = gr.Interface(
|
| 97 |
-
fn=run,
|
| 98 |
-
inputs=gr.Textbox(label="🎬 Gib den Titel deines Horror-Videos ein"),
|
| 99 |
-
outputs=gr.Video(label="👻 Generiertes Horror-Video"),
|
| 100 |
-
title="Creepy Horror Shorts Generator (kostenfrei)",
|
| 101 |
-
description="Gib einen gruseligen Titel ein – und erhalte automatisch ein Horror-Video aus freien Bildern.",
|
| 102 |
-
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
+
from moviepy.editor import ImageClip, concatenate_videoclips
|
| 6 |
+
import random
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Sichere, getestete Horror-Bilder (GitHub gehostet)
|
| 10 |
IMAGE_DB = {
|
| 11 |
"haus": [
|
| 12 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/haunted_house_1.jpg",
|
| 13 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/haunted_house_2.jpg"
|
| 14 |
],
|
| 15 |
"geist": [
|
| 16 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/ghost_1.jpg",
|
| 17 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/ghost_2.jpg"
|
| 18 |
],
|
| 19 |
"spuk": [
|
| 20 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/spooky_forest.jpg",
|
| 21 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/abandoned_building.jpg"
|
| 22 |
],
|
| 23 |
"katze": [
|
| 24 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/black_cat.jpg"
|
| 25 |
],
|
| 26 |
"ritual": [
|
| 27 |
+
"https://raw.githubusercontent.com/chrispawluk/horror-img/main/ritual_circle.jpg"
|
| 28 |
],
|
| 29 |
}
|
| 30 |
|
| 31 |
+
def find_images_for_title(title):
|
| 32 |
+
# Suche passende Bilder basierend auf Keywords im Titel
|
| 33 |
+
keywords = IMAGE_DB.keys()
|
| 34 |
+
selected_images = []
|
| 35 |
+
for keyword in keywords:
|
| 36 |
+
if keyword in title.lower():
|
| 37 |
+
selected_images.extend(IMAGE_DB[keyword])
|
| 38 |
+
# Falls keine Keywords gefunden, zufällige Bilder
|
| 39 |
+
if not selected_images:
|
| 40 |
+
# alle Bilder aus allen Kategorien sammeln
|
| 41 |
+
all_images = sum(IMAGE_DB.values(), [])
|
| 42 |
+
selected_images = random.sample(all_images, min(5, len(all_images)))
|
| 43 |
+
else:
|
| 44 |
+
selected_images = selected_images[:5] # max 5 Bilder
|
| 45 |
+
return selected_images
|
| 46 |
|
|
|
|
| 47 |
def download_image(url):
|
| 48 |
try:
|
| 49 |
+
response = requests.get(url)
|
| 50 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 51 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"WARNUNG: Ungültiges Bild: {url}")
|
| 54 |
return None
|
| 55 |
|
|
|
|
| 56 |
def create_video(title):
|
| 57 |
+
image_urls = find_images_for_title(title)
|
| 58 |
+
images = []
|
| 59 |
+
for url in image_urls:
|
|
|
|
|
|
|
| 60 |
img = download_image(url)
|
| 61 |
+
if img:
|
| 62 |
+
images.append(img)
|
| 63 |
+
if not images:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
raise RuntimeError("Keine gültigen Bilder gefunden. Bitte gib einen anderen Titel ein.")
|
| 65 |
|
| 66 |
+
clips = []
|
| 67 |
+
duration_per_image = 12 # 5 Bilder x 12s = 60s Video
|
| 68 |
+
for img in images:
|
| 69 |
+
clip = ImageClip(img).set_duration(duration_per_image)
|
| 70 |
+
clips.append(clip)
|
| 71 |
+
|
| 72 |
+
video = concatenate_videoclips(clips, method="compose")
|
| 73 |
+
output_path = "output.mp4"
|
| 74 |
+
video.write_videofile(output_path, fps=24, codec="libx264", audio=False)
|
| 75 |
+
return output_path
|
| 76 |
|
|
|
|
| 77 |
def run(title):
|
| 78 |
+
try:
|
| 79 |
+
video_path = create_video(title)
|
| 80 |
+
return video_path
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return str(e)
|
| 83 |
+
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# Creepy Horror Movie Shorts Generator")
|
| 86 |
+
title_input = gr.Textbox(label="Gib den Titel oder die Idee des Videos ein", placeholder="z.B. Spuk im alten Haus")
|
| 87 |
+
submit_btn = gr.Button("Submit")
|
| 88 |
+
video_output = gr.Video(label="Dein Horror-Video")
|
| 89 |
|
| 90 |
+
submit_btn.click(run, inputs=title_input, outputs=video_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
demo.launch(share=True)
|
|
|