Spaces:
Running
Running
Create test-interface.py
Browse files- test-interface.py +99 -0
test-interface.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tempfile
|
3 |
+
import time
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
|
6 |
+
def generate_animated_sprite(character_description, num_frames, character_action, viewing_direction):
|
7 |
+
"""
|
8 |
+
Funzione di simulazione per generare un'immagine GIF animata.
|
9 |
+
In un'implementazione reale, qui chiameresti il tuo modello di IA.
|
10 |
+
|
11 |
+
Per ora, questa funzione genera solo un'immagine di esempio per testare l'interfaccia.
|
12 |
+
"""
|
13 |
+
print(f"Generazione sprite con questi parametri:")
|
14 |
+
print(f"- Descrizione: {character_description}")
|
15 |
+
print(f"- Frames: {num_frames}")
|
16 |
+
print(f"- Azione: {character_action}")
|
17 |
+
print(f"- Direzione: {viewing_direction}")
|
18 |
+
|
19 |
+
# Crea delle immagini di esempio per simulare un'animazione
|
20 |
+
frames = []
|
21 |
+
for i in range(num_frames):
|
22 |
+
# Crea un'immagine di esempio
|
23 |
+
img = Image.new('RGB', (100, 100), color=(73, 109, 137))
|
24 |
+
d = ImageDraw.Draw(img)
|
25 |
+
|
26 |
+
# Disegna qualcosa di diverso in ogni frame per simulare l'animazione
|
27 |
+
x_offset = 10 + (i * 10) % 50
|
28 |
+
d.ellipse((x_offset, 20, x_offset + 60, 80), fill=(255, 255, 0))
|
29 |
+
d.text((10, 10), f"{character_action} - {i+1}", fill=(255, 255, 255))
|
30 |
+
|
31 |
+
frames.append(img)
|
32 |
+
|
33 |
+
# Salva i frame come GIF animata
|
34 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".gif")
|
35 |
+
frames[0].save(
|
36 |
+
temp_file.name,
|
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 |
+
temp_file.close()
|
44 |
+
|
45 |
+
return temp_file.name
|
46 |
+
|
47 |
+
# Interface Gradio locale
|
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 |
+
# Launch the Gradio interface
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.launch(share=True) # Imposta share=False se non vuoi un URL pubblico temporaneo
|