onnew commited on
Commit
8b094c3
·
verified ·
1 Parent(s): 0bc52e3

Update app.py

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