Spaces:
Running
Running
Lorenzo Adacher
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
|
| 6 |
+
def generate_animated_sprite(character_description, num_frames, character_action, viewing_direction):
|
| 7 |
+
"""
|
| 8 |
+
Funzione per generare un'immagine GIF animata.
|
| 9 |
+
"""
|
| 10 |
+
print(f"Generazione sprite con questi parametri:")
|
| 11 |
+
print(f"- Descrizione: {character_description}")
|
| 12 |
+
print(f"- Frames: {num_frames}")
|
| 13 |
+
print(f"- Azione: {character_action}")
|
| 14 |
+
print(f"- Direzione: {viewing_direction}")
|
| 15 |
+
|
| 16 |
+
# Crea delle immagini di esempio per simulare un'animazione
|
| 17 |
+
frames = []
|
| 18 |
+
for i in range(int(num_frames)): # Converti in int per sicurezza
|
| 19 |
+
# Crea un'immagine di esempio
|
| 20 |
+
img = Image.new('RGB', (100, 100), color=(73, 109, 137))
|
| 21 |
+
d = ImageDraw.Draw(img)
|
| 22 |
+
|
| 23 |
+
# Disegna qualcosa di diverso in ogni frame per simulare l'animazione
|
| 24 |
+
x_offset = 10 + (i * 10) % 50
|
| 25 |
+
d.ellipse((x_offset, 20, x_offset + 60, 80), fill=(255, 255, 0))
|
| 26 |
+
d.text((10, 10), f"{character_action} - {i+1}", fill=(255, 255, 255))
|
| 27 |
+
|
| 28 |
+
frames.append(img)
|
| 29 |
+
|
| 30 |
+
# Crea una directory temporanea se non esiste
|
| 31 |
+
os.makedirs("tmp", exist_ok=True)
|
| 32 |
+
output_path = os.path.join("tmp", f"sprite_{hash(character_description)}.gif")
|
| 33 |
+
|
| 34 |
+
# Salva i frame come GIF animata
|
| 35 |
+
frames[0].save(
|
| 36 |
+
output_path,
|
| 37 |
+
format='GIF',
|
| 38 |
+
append_images=frames[1:],
|
| 39 |
+
save_all=True,
|
| 40 |
+
duration=200, # 200 ms tra i frame
|
| 41 |
+
loop=0 # 0 = loop infinito
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return output_path
|
| 45 |
+
|
| 46 |
+
# Definisci l'interfaccia Gradio
|
| 47 |
+
def create_interface():
|
| 48 |
+
with gr.Blocks(title="Animated Sprite Generator") as demo:
|
| 49 |
+
gr.Markdown("# 🎮 AI Animated Sprite Generator")
|
| 50 |
+
gr.Markdown("""
|
| 51 |
+
This tool uses an AI model to generate animated sprites based on text descriptions.
|
| 52 |
+
Enter the character description, number of frames, character action, and viewing direction to generate your animated sprite.
|
| 53 |
+
""")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
# Input components
|
| 58 |
+
char_desc = gr.Textbox(label="Character Description",
|
| 59 |
+
placeholder="Ex: a knight with golden armor and a fire sword",
|
| 60 |
+
lines=3)
|
| 61 |
+
num_frames = gr.Slider(minimum=1, maximum=8, step=1, value=4,
|
| 62 |
+
label="Number of Animation Frames")
|
| 63 |
+
char_action = gr.Dropdown(
|
| 64 |
+
choices=["idle", "walk", "run", "attack", "jump", "die", "cast spell", "dance"],
|
| 65 |
+
label="Character Action",
|
| 66 |
+
value="idle"
|
| 67 |
+
)
|
| 68 |
+
view_direction = gr.Dropdown(
|
| 69 |
+
choices=["front", "back", "left", "right", "front-left", "front-right", "back-left", "back-right"],
|
| 70 |
+
label="Viewing Direction",
|
| 71 |
+
value="front"
|
| 72 |
+
)
|
| 73 |
+
generate_btn = gr.Button("Generate Animated Sprite")
|
| 74 |
+
|
| 75 |
+
with gr.Column():
|
| 76 |
+
# Output component
|
| 77 |
+
animated_output = gr.Image(label="Animated Sprite (GIF)")
|
| 78 |
+
|
| 79 |
+
# Connect the button to the function
|
| 80 |
+
generate_btn.click(
|
| 81 |
+
fn=generate_animated_sprite,
|
| 82 |
+
inputs=[char_desc, num_frames, char_action, view_direction],
|
| 83 |
+
outputs=animated_output
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Predefined examples
|
| 87 |
+
gr.Examples(
|
| 88 |
+
[
|
| 89 |
+
["A wizard with blue cloak and pointed hat", 4, "cast spell", "front"],
|
| 90 |
+
["A warrior with heavy armor and axe", 6, "attack", "right"],
|
| 91 |
+
["A ninja with black clothes and throwing stars", 8, "run", "front-left"],
|
| 92 |
+
["A princess with golden crown and pink dress", 4, "dance", "front"]
|
| 93 |
+
],
|
| 94 |
+
inputs=[char_desc, num_frames, char_action, view_direction]
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
return demo
|
| 98 |
+
|
| 99 |
+
# Crea ed avvia l'interfaccia
|
| 100 |
+
demo = create_interface()
|
| 101 |
+
|
| 102 |
+
# Launch the Gradio interface
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo.launch()
|
| 105 |
+
else:
|
| 106 |
+
# Questa è la parte importante per Hugging Face Spaces
|
| 107 |
+
# Non chiamare .launch() qui, lo Space lo farà automaticamente
|
| 108 |
+
app = demo
|