Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,76 @@ clientOpenAI = OpenAI(
|
|
| 20 |
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Definizione dello schema Pydantic per un personaggio
|
| 24 |
class Character(BaseModel):
|
| 25 |
nome: str
|
|
@@ -34,13 +104,13 @@ class Character(BaseModel):
|
|
| 34 |
class CharactersResponse(BaseModel):
|
| 35 |
personaggi: list[Character]
|
| 36 |
|
| 37 |
-
def generate_story(character: Character):
|
| 38 |
response = clientOpenAI.chat.completions.create(
|
| 39 |
-
model=
|
| 40 |
n=1,
|
| 41 |
stream=True,
|
| 42 |
messages=[
|
| 43 |
-
{"role": "system", "content": "Tu sei un creatore di giochi di ruolo a CARTE. Crea un regolamento per un gioco di ruolo sul
|
| 44 |
{
|
| 45 |
"role": "user",
|
| 46 |
"content": f"Ecco i personaggi del gioco di RUOLO a carte. Crea un regolamento!!! {character.model_dump_json()}"
|
|
@@ -58,10 +128,10 @@ def generate_story(character: Character):
|
|
| 58 |
print(story)
|
| 59 |
return story
|
| 60 |
|
| 61 |
-
def generate_ai(num_personaggi):
|
| 62 |
# Costruzione del prompt in italiano per generare i personaggi
|
| 63 |
prompt = (
|
| 64 |
-
f"Genera {num_personaggi} personaggi per un gioco di ruolo a carte ambientato nel
|
| 65 |
"Ogni personaggio deve avere i seguenti campi specificati nel modello. "
|
| 66 |
"Restituisci il risultato in formato JSON seguendo lo schema fornito")
|
| 67 |
|
|
@@ -69,7 +139,7 @@ def generate_ai(num_personaggi):
|
|
| 69 |
completion = clientOpenAI.beta.chat.completions.parse(
|
| 70 |
model=MODEL,
|
| 71 |
messages=[
|
| 72 |
-
{"role": "system", "content": "Sei un assistente utile per la generazione di personaggi per un gioco di RUOLO sul
|
| 73 |
{"role": "user", "content": prompt},
|
| 74 |
],
|
| 75 |
response_format=CharactersResponse,
|
|
@@ -129,15 +199,12 @@ def generate_images(character: Character, stile_immagine, num_immagini):
|
|
| 129 |
def main():
|
| 130 |
st.title("Imperium AI 🏰")
|
| 131 |
st.sidebar.header("Impostazioni")
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
"and classical sculpture, with a balanced mix of realism and stylization. "
|
| 136 |
-
"Elegant, decorative card borders with intricate engravings and antique flourishes. "
|
| 137 |
-
"Designed for a tabletop card game, ensuring clarity, readability, and a visually immersive experience.")
|
| 138 |
stile_immagine = st.sidebar.text_area("Stile Immagine", stile_default, disabled=False)
|
| 139 |
auto = False
|
| 140 |
-
prompt_input = st.sidebar.text_input("Prompt Immagine (in inglese)", value="
|
| 141 |
auto = st.sidebar.toggle(label= 'Generazione automatica')
|
| 142 |
num_personaggi = st.sidebar.slider("Personaggi", min_value=1, max_value=30, value=5, disabled=not auto)
|
| 143 |
num_immagini = st.sidebar.slider("Variazioni Immagini", min_value=1, max_value=6, value=2)
|
|
@@ -151,12 +218,12 @@ def main():
|
|
| 151 |
return
|
| 152 |
if auto:
|
| 153 |
with st.spinner('Generazione Personaggi'):
|
| 154 |
-
characters = generate_ai(num_personaggi)
|
| 155 |
st.subheader('Personaggi 🎭')
|
| 156 |
df = pd.DataFrame([{k: v for k, v in character.model_dump().items() if k != "english_description"} for character in characters.personaggi])
|
| 157 |
st.dataframe(df, hide_index=True)
|
| 158 |
st.divider()
|
| 159 |
-
generate_story(characters)
|
| 160 |
st.divider()
|
| 161 |
else:
|
| 162 |
characters = CharactersResponse(personaggi=[Character(nome="", classe="Guerriero", forza=10, destrezza=8, intelligenza=6, descrizione="Un forte guerriero", english_description=prompt_input)])
|
|
|
|
| 20 |
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 21 |
)
|
| 22 |
|
| 23 |
+
ambientazioni = {
|
| 24 |
+
"Sacro Romano Impero": {
|
| 25 |
+
"nome": "Sacro Romano Impero",
|
| 26 |
+
"stile_immagine": (
|
| 27 |
+
"Highly detailed, painterly style with a historical yet stylized aesthetic. "
|
| 28 |
+
"Rich textures, ornate patterns, and a color palette dominated by imperial gold, "
|
| 29 |
+
"deep red, and aged marble tones. Inspired by medieval European art, "
|
| 30 |
+
"gothic illuminations, and the grandeur of cathedrals. "
|
| 31 |
+
"Designed for a tabletop card game, ensuring clarity, readability, "
|
| 32 |
+
"and a visually immersive experience."
|
| 33 |
+
)
|
| 34 |
+
},
|
| 35 |
+
"Antico Egitto": {
|
| 36 |
+
"nome": "Antico Egitto",
|
| 37 |
+
"stile_immagine": (
|
| 38 |
+
"A stylized Egyptian art style, reminiscent of ancient tomb paintings. "
|
| 39 |
+
"Use sandy, ochre and turquoise color palette, with hieroglyphic details. "
|
| 40 |
+
"Geometric shapes and patterns evoke the grandeur of pharaohs and pyramids. "
|
| 41 |
+
"Designed for a tabletop card game with clarity and distinct thematic flourishes."
|
| 42 |
+
)
|
| 43 |
+
},
|
| 44 |
+
"Dinastia Han Cinese": {
|
| 45 |
+
"nome": "Dinastia Han Cinese",
|
| 46 |
+
"stile_immagine": (
|
| 47 |
+
"Traditional Chinese art style, with ink wash influences, ornate calligraphy, "
|
| 48 |
+
"and vibrant silk robe patterns. Use subtle brush strokes and bold reds/golds. "
|
| 49 |
+
"Designed for clarity in a card game with a distinctly ancient Asian aesthetic."
|
| 50 |
+
)
|
| 51 |
+
},
|
| 52 |
+
"Impero Persiano": {
|
| 53 |
+
"nome": "Impero Persiano",
|
| 54 |
+
"stile_immagine": (
|
| 55 |
+
"Intricate Persian miniature style, lavish details, floral ornaments, "
|
| 56 |
+
"and strong jewel tones like turquoise, gold, and purple. Stylized silhouettes "
|
| 57 |
+
"in a rich, decorative composition suited for card art."
|
| 58 |
+
)
|
| 59 |
+
},
|
| 60 |
+
"Impero Ottomano": {
|
| 61 |
+
"nome": "Impero Ottomano",
|
| 62 |
+
"stile_immagine": (
|
| 63 |
+
"Ottoman illuminated manuscript style, featuring ornate arabesque borders, "
|
| 64 |
+
"rich gold leaf, and deep sapphire or emerald backgrounds. "
|
| 65 |
+
"Figures with flowing robes and turbans, capturing courtly splendor."
|
| 66 |
+
)
|
| 67 |
+
},
|
| 68 |
+
"Maya": {
|
| 69 |
+
"nome": "Maya",
|
| 70 |
+
"stile_immagine": (
|
| 71 |
+
"Mesoamerican style with stylized glyphs, stepped pyramids, and bright colors. "
|
| 72 |
+
"Incorporate traditional motifs of jaguars and feathers in a decorative, yet readable card layout."
|
| 73 |
+
)
|
| 74 |
+
},
|
| 75 |
+
"Vichinghi": {
|
| 76 |
+
"nome": "Vichinghi",
|
| 77 |
+
"stile_immagine": (
|
| 78 |
+
"Norse-inspired style, with knotwork borders, runic inscriptions, and rugged, "
|
| 79 |
+
"weathered textures. Use earthy colors and references to Viking longships, "
|
| 80 |
+
"mythology, and wooden carvings. Balanced for a legible card game design."
|
| 81 |
+
)
|
| 82 |
+
},
|
| 83 |
+
"Grecia Antica": {
|
| 84 |
+
"nome": "Grecia Antica",
|
| 85 |
+
"stile_immagine": (
|
| 86 |
+
"Classic Greek art style with white marble textures, red-figure pottery motifs, "
|
| 87 |
+
"and references to mythological scenes. Elegant lines and columns, evoking the "
|
| 88 |
+
"aesthetics of ancient vases and statues. Ideal for a tabletop card game with readable design."
|
| 89 |
+
)
|
| 90 |
+
},
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
# Definizione dello schema Pydantic per un personaggio
|
| 94 |
class Character(BaseModel):
|
| 95 |
nome: str
|
|
|
|
| 104 |
class CharactersResponse(BaseModel):
|
| 105 |
personaggi: list[Character]
|
| 106 |
|
| 107 |
+
def generate_story(character: Character, nome_ambientazione):
|
| 108 |
response = clientOpenAI.chat.completions.create(
|
| 109 |
+
model='gemini-2.0-flash-thinking-exp-01-21',
|
| 110 |
n=1,
|
| 111 |
stream=True,
|
| 112 |
messages=[
|
| 113 |
+
{"role": "system", "content": f"Tu sei un creatore di giochi di ruolo a CARTE. Crea un regolamento per un gioco di ruolo sul {nome_ambientazione} sulla base dei personaggi che ti fornirò"},
|
| 114 |
{
|
| 115 |
"role": "user",
|
| 116 |
"content": f"Ecco i personaggi del gioco di RUOLO a carte. Crea un regolamento!!! {character.model_dump_json()}"
|
|
|
|
| 128 |
print(story)
|
| 129 |
return story
|
| 130 |
|
| 131 |
+
def generate_ai(num_personaggi, nome_ambientazione):
|
| 132 |
# Costruzione del prompt in italiano per generare i personaggi
|
| 133 |
prompt = (
|
| 134 |
+
f"Genera {num_personaggi} personaggi per un gioco di ruolo a carte ambientato nel {nome_ambientazione}. "
|
| 135 |
"Ogni personaggio deve avere i seguenti campi specificati nel modello. "
|
| 136 |
"Restituisci il risultato in formato JSON seguendo lo schema fornito")
|
| 137 |
|
|
|
|
| 139 |
completion = clientOpenAI.beta.chat.completions.parse(
|
| 140 |
model=MODEL,
|
| 141 |
messages=[
|
| 142 |
+
{"role": "system", "content": f"Sei un assistente utile per la generazione di personaggi per un gioco di RUOLO sul {nome_ambientazione}."},
|
| 143 |
{"role": "user", "content": prompt},
|
| 144 |
],
|
| 145 |
response_format=CharactersResponse,
|
|
|
|
| 199 |
def main():
|
| 200 |
st.title("Imperium AI 🏰")
|
| 201 |
st.sidebar.header("Impostazioni")
|
| 202 |
+
selected_ambientazione = st.sidebar.selectbox("Ambientazione", list(ambientazioni.keys()), index=0)
|
| 203 |
+
stile_default = ambientazioni[selected_ambientazione]["stile_immagine"]
|
| 204 |
+
nome_ambientazione = ambientazioni[selected_ambientazione]["nome"]
|
|
|
|
|
|
|
|
|
|
| 205 |
stile_immagine = st.sidebar.text_area("Stile Immagine", stile_default, disabled=False)
|
| 206 |
auto = False
|
| 207 |
+
prompt_input = st.sidebar.text_input("Prompt Immagine (in inglese)", value="Soldier", disabled=auto)
|
| 208 |
auto = st.sidebar.toggle(label= 'Generazione automatica')
|
| 209 |
num_personaggi = st.sidebar.slider("Personaggi", min_value=1, max_value=30, value=5, disabled=not auto)
|
| 210 |
num_immagini = st.sidebar.slider("Variazioni Immagini", min_value=1, max_value=6, value=2)
|
|
|
|
| 218 |
return
|
| 219 |
if auto:
|
| 220 |
with st.spinner('Generazione Personaggi'):
|
| 221 |
+
characters = generate_ai(num_personaggi, nome_ambientazione)
|
| 222 |
st.subheader('Personaggi 🎭')
|
| 223 |
df = pd.DataFrame([{k: v for k, v in character.model_dump().items() if k != "english_description"} for character in characters.personaggi])
|
| 224 |
st.dataframe(df, hide_index=True)
|
| 225 |
st.divider()
|
| 226 |
+
generate_story(characters, nome_ambientazione)
|
| 227 |
st.divider()
|
| 228 |
else:
|
| 229 |
characters = CharactersResponse(personaggi=[Character(nome="", classe="Guerriero", forza=10, destrezza=8, intelligenza=6, descrizione="Un forte guerriero", english_description=prompt_input)])
|