Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,83 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
-
from PIL import Image
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
gif_path = "output.gif"
|
16 |
-
frames[0].save(
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
return gif_path
|
18 |
|
19 |
-
|
20 |
-
|
|
|
21 |
return gif_path
|
22 |
|
23 |
-
#
|
24 |
with gr.Blocks() as app:
|
25 |
gr.Markdown("# Gerador de Animações com Prompt de Texto")
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
generate_button = gr.Button("Gerar Animação")
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
+
import os
|
4 |
|
5 |
+
# Função para criar frames a partir de prompts (exemplo usando texto sobre imagens)
|
6 |
+
def generate_frame(prompt, frame_number, width=256, height=256):
|
7 |
+
# Cria uma imagem em branco
|
8 |
+
img = Image.new("RGB", (width, height), color="white")
|
9 |
+
draw = ImageDraw.Draw(img)
|
10 |
|
11 |
+
# Adiciona o texto do prompt no centro
|
12 |
+
text = f"{prompt} ({frame_number})"
|
13 |
+
text_width, text_height = draw.textsize(text)
|
14 |
+
draw.text(
|
15 |
+
((width - text_width) / 2, (height - text_height) / 2),
|
16 |
+
text,
|
17 |
+
fill="black",
|
18 |
+
)
|
19 |
+
return img
|
20 |
|
21 |
+
# Função para criar uma animação (GIF)
|
22 |
+
def create_animation_from_prompts(prompt, frame_count=10, duration=100):
|
23 |
+
frames = []
|
24 |
+
|
25 |
+
# Gera os frames com base no prompt
|
26 |
+
for i in range(frame_count):
|
27 |
+
frame = generate_frame(prompt, i + 1)
|
28 |
+
frames.append(frame)
|
29 |
+
|
30 |
+
# Salva a animação como GIF
|
31 |
gif_path = "output.gif"
|
32 |
+
frames[0].save(
|
33 |
+
gif_path,
|
34 |
+
save_all=True,
|
35 |
+
append_images=frames[1:],
|
36 |
+
duration=duration,
|
37 |
+
loop=0,
|
38 |
+
)
|
39 |
return gif_path
|
40 |
|
41 |
+
# Interface Gradio
|
42 |
+
def animate_sprite(prompt, frame_count, duration):
|
43 |
+
gif_path = create_animation_from_prompts(prompt, frame_count, duration)
|
44 |
return gif_path
|
45 |
|
46 |
+
# Criação da interface
|
47 |
with gr.Blocks() as app:
|
48 |
gr.Markdown("# Gerador de Animações com Prompt de Texto")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
prompt = gr.Textbox(
|
52 |
+
label="Descrição do Sprite",
|
53 |
+
placeholder="Descreva o sprite ou a animação que deseja criar",
|
54 |
+
)
|
55 |
+
frame_count = gr.Slider(
|
56 |
+
label="Número de Frames",
|
57 |
+
minimum=1,
|
58 |
+
maximum=30,
|
59 |
+
value=10,
|
60 |
+
step=1,
|
61 |
+
)
|
62 |
+
duration = gr.Slider(
|
63 |
+
label="Duração do Frame (ms)",
|
64 |
+
minimum=50,
|
65 |
+
maximum=500,
|
66 |
+
value=100,
|
67 |
+
step=10,
|
68 |
+
)
|
69 |
+
|
70 |
+
# Saída de imagem
|
71 |
+
animation_output = gr.Image(label="Animação Gerada", type="filepath")
|
72 |
+
|
73 |
generate_button = gr.Button("Gerar Animação")
|
74 |
|
75 |
+
# Configuração do botão para gerar a animação
|
76 |
+
generate_button.click(
|
77 |
+
animate_sprite,
|
78 |
+
inputs=[prompt, frame_count, duration],
|
79 |
+
outputs=animation_output,
|
80 |
+
)
|
81 |
|
82 |
+
# Lançamento da aplicação
|
83 |
+
app.launch()
|