onnew commited on
Commit
3083125
·
verified ·
1 Parent(s): 4083603

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -20
app.py CHANGED
@@ -1,34 +1,83 @@
1
- from transformers import pipeline
2
  import gradio as gr
3
- from PIL import Image
 
4
 
5
- # Carrega o pipeline de geração de imagens
6
- generator = pipeline("image-generation", model="stabilityai/stable-diffusion-2")
 
 
 
7
 
8
- def generate_frame_with_model(prompt, width=256, height=256):
9
- result = generator(prompt, height=height, width=width, num_inference_steps=50)
10
- image = Image.open(result[0]["generated_image"]).convert("RGB")
11
- return image
 
 
 
 
 
12
 
13
- def create_animation_with_model(prompt, frame_count, duration):
14
- frames = [generate_frame_with_model(f"{prompt}, frame {i}") for i in range(frame_count)]
 
 
 
 
 
 
 
 
15
  gif_path = "output.gif"
16
- frames[0].save(gif_path, save_all=True, append_images=frames[1:], duration=duration, loop=0)
 
 
 
 
 
 
17
  return gif_path
18
 
19
- def animate_with_model(prompt, frame_count, duration):
20
- gif_path = create_animation_with_model(prompt, frame_count, duration)
 
21
  return gif_path
22
 
23
- # Interface Gradio
24
  with gr.Blocks() as app:
25
  gr.Markdown("# Gerador de Animações com Prompt de Texto")
26
- prompt = gr.Textbox(label="Descrição do Sprite", placeholder="Descreva o sprite ou a animação que deseja criar")
27
- frame_count = gr.Slider(label="Número de Frames", minimum=1, maximum=10, value=5, step=1)
28
- duration = gr.Slider(label="Duração do Frame (ms)", minimum=50, maximum=500, value=100, step=10)
29
- animation_output = gr.Image(label="Animação Gerada")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  generate_button = gr.Button("Gerar Animação")
31
 
32
- generate_button.click(animate_with_model, inputs=[prompt, frame_count, duration], outputs=animation_output)
 
 
 
 
 
33
 
34
- app.launch()
 
 
 
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()