Spaces:
Sleeping
Sleeping
Update app.py
Browse files19 unmodified lines2021222324252324252627282930312728293031343532333437383940414235363738454639404148495051424344455455464748575859495051526263535455656667565758597071606162737475636465667879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314467681471481496970151152153154717215715815916016116216316416516616716816973171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222747576224777822622722822979808182832338485868723788892392409091929394954 unmodified lines1001011022512521031041051061076 unmodified lines11411511626526626726826927027127227327427527627727827928028128228328428528628728828929011711811929229312012112212312412512612714 unmodified lines14214314431414514631631731814714814915032032115115215315415532332432532632732832933033133233333433533633733815615715815916034016116216334334416434634734834935035135235335435535635716516616716816917017136036136236336436517217336836937037137237317417517617717817910 unmodified lines19019119239039119319419 unmodified lines base_url="https://generativelanguage.googleapis.com/v1beta/openai/")
ambientazioni = { "Roma Repubblicana": { "nome": "Roma Repubblicana",LOGO_STYLES = { "Minimalist": { "nome": "Minimalist", "stile_immagine": ( "A painterly, highly detailed visual narrative rich with graphic elements Roman Repubblic " "Incorporate ornate Roman columns, laurel wreaths, engraved inscriptions, and intricate mosaic patterns. " "Textures of weathered marble, aged frescoes, and tattered papyrus blend with a dramatic chiaroscuro effect. " "A color palette of imperial red, bronze, and muted gold accentuates secretive senatorial silhouettes and subtle sigils. " "Designed for a tabletop card game, this style captures clandestine intrigue and political tension with dynamic visual flair." "A minimalist logo design prompt focusing on simplicity and essential elements. " "Clean lines, a limited color palette, and strategic use of negative space. " "Ideal for conveying modernity and clarity." ) }, "Sacro Romano Impero": { "nome": "Sacro Romano Impero", "Modern": { "nome": "Modern", "stile_immagine": ( "In the italian Holy Roman Empire, Highly detailed, painterly style with a historical yet stylized aesthetic. " "Rich textures, ornate patterns, and a color palette dominated by imperial gold, " "deep red, and aged marble tones. Inspired by medieval European art, " "gothic illuminations, and the grandeur of cathedrals. " "Designed for a tabletop card game, ensuring clarity, readability, " "and a visually immersive experience." "A modern logo design prompt featuring abstract forms, innovative color combinations, " "and dynamic geometric shapes. Sleek typography and a contemporary aesthetic make it perfect for tech companies and startups." ) }, "Antico Egitto": { "nome": "Antico Egitto", "Retro": { "nome": "Retro", "stile_immagine": ( "A stylized Egyptian art style, reminiscent of ancient tomb paintings. " "Use sandy, ochre and turquoise color palette, with hieroglyphic details. " "Geometric shapes and patterns evoke the grandeur of pharaohs and pyramids. " "Designed for a tabletop card game with clarity and distinct thematic flourishes." "A retro logo design prompt evoking nostalgia with vintage color schemes, classic typography, " "and design elements reminiscent of the past. Perfect for brands that want to express heritage and authenticity." ) }, "Dinastia Han Cinese": { "nome": "Dinastia Han Cinese", "Vintage": { "nome": "Vintage", "stile_immagine": ( "Traditional Chinese art style, with ink wash influences, ornate calligraphy, " "and vibrant silk robe patterns. Use subtle brush strokes and bold reds/golds. " "Designed for clarity in a card game with a distinctly ancient Asian aesthetic." "A vintage logo design prompt inspired by bygone eras. Emphasizes handcrafted details, worn textures, " "and a nostalgic atmosphere, ideal for artisanal products or brands with a long-standing tradition." ) }, "Impero Persiano": { "nome": "Impero Persiano", "Geometric": { "nome": "Geometric", "stile_immagine": ( "Intricate Persian miniature style, lavish details, floral ornaments, " "and strong jewel tones like turquoise, gold, and purple. Stylized silhouettes " "in a rich, decorative composition suited for card art." "A geometric logo design prompt that leverages simple, precise shapes, clean lines, and symmetry. " "Communicates order, professionalism, and a rational approach to design." ) }, "Impero Ottomano": { "nome": "Impero Ottomano", "Typographic": { "nome": "Typographic", "stile_immagine": ( "Ottoman illuminated manuscript style, featuring ornate arabesque borders, " "rich gold leaf, and deep sapphire or emerald backgrounds. " "Figures with flowing robes and turbans, capturing courtly splendor." "A typographic logo design prompt focused on the creative use of lettering. " "Bold typography paired with minimal color usage highlights the strength of word-based identities." ) }, "Maya": { "nome": "Maya", "stile_immagine": ( "Mesoamerican style with stylized glyphs, stepped pyramids, and bright colors. " "Incorporate traditional motifs of jaguars and feathers in a decorative, yet readable card layout." ) }, "Vichinghi": { "nome": "Vichinghi", "stile_immagine": ( "Norse-inspired style, with knotwork borders, runic inscriptions, and rugged, " "weathered textures. Use earthy colors and references to Viking longships, " "mythology, and wooden carvings. Balanced for a legible card game design." ) }, "Pirati": { "nome": "Pirati", "stile_immagine": ( "A dynamic and adventurous style inspired by leggende dei mari e pirati romantici. " "Immagini di navi in tempesta, mappe del tesoro, e personaggi con bandane e cappelli tricorni. " "Toni marinari, textures consumate dal sale e dall'acqua, e un'atmosfera ribelle ma affascinante, " "perfetta per un gioco di carte che evoca il brivido dell'avventura sul mare." ) }, "Cowboy del Far West": { "nome": "Cowboy del Far West", "stile_immagine": ( "Highly detailed, painterly style with a rugged yet cinematic aesthetic. " "Rich textures, earthy tones, and a palette dominated by dusty browns, deep reds, and sunset golds. " "Inspired by classic Western art, frontier landscapes, and vintage posters. " "Iconic elements like cowboy hats, revolvers, and stagecoaches evoke the romance of the Wild West. " "Designed for a tabletop card game, ensuring clarity, readability, and an immersive sense of adventure." ) }, "Steampunk": { "nome": "Steampunk", "stile_immagine": ( "Highly detailed, painterly style with a fusion of Victorian elegance and mechanical ingenuity. " "Brass gears, intricate machinery, steam-powered contraptions, and industrial backdrops " "create a retro-futuristic atmosphere. " "A color palette dominated by warm metallics, deep browns, and aged parchment tones. " "Inspired by classic steampunk literature and art, ensuring a visually immersive experience " "for a tabletop card game with a sense of mystery and innovation." ) }, "Alieni": { "nome": "Alieni", "stile_immagine": ( "Highly detailed, painterly style with a futuristic and surreal aesthetic. " "Glowing neon hues, organic and biomechanical forms, and impossible cosmic landscapes " "blend to create an otherworldly atmosphere. " "A color palette dominated by electric blues, deep purples, and iridescent greens. " "Inspired by sci-fi concept art and space opera visuals, ensuring clarity, readability, " "and a visually stunning experience for a tabletop card game set in a mysterious universe." ) }, "Homo Sapiens": { "nome": "Homo Sapiens", "stile_immagine": ( "Highly detailed, painterly style with a blend of realism and symbolic representation. " "Natural tones, textures inspired by organic matter, and visual elements reflecting " "human evolution, history, and cultural development. " "A fusion of prehistoric cave paintings, Renaissance anatomical studies, and modern artistic interpretations. " "Designed for a tabletop card game, ensuring clarity, readability, and a thought-provoking visual narrative " "that bridges the ancient past with the present." ) } }
# Definizione dello schema Pydantic per un personaggioclass Character(BaseModel):class Logo(BaseModel): nome: str classe: str forza: int destrezza: int intelligenza: int descrizione: str english_description:str storia: str
# Definizione dello schema per la risposta contenente una lista di personaggiclass CharactersResponse(BaseModel): personaggi: list[Character]
class ActionCard(BaseModel): nome_azione: str breve_descrizione: str breve_descrizione_inglese: str
class ActionCardsResponse(BaseModel): carte_azione: list[ActionCard]
def generate_story(character: Character, carte_azione: ActionCardsResponse, nome_ambientazione, creativita): descrizione_carte_azione = "" descrizione_personaggi = "" if carte_azione: descrizione_carte_azione = f"Ecco anche le CARTE AZIONE del gioco: {carte_azione.model_dump_json()}" if character: descrizione_personaggi = f"Ecco i personaggi del gioco di RUOLO a carte. {character.model_dump_json()}" response = clientOpenAI.chat.completions.create( model='gemini-2.0-flash-thinking-exp-01-21', n=1, stream=True, temperature=creativita, messages=[ {"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ò"}, { "role": "user", "content": f"Crea un regolamento!!! {descrizione_personaggi} \n\n {descrizione_carte_azione}" } ] ) story = "" st.subheader('Regole 📜') placeholder = st.empty() for chunk in response: if chunk.choices[0].delta.content is not None: text_chunk = chunk.choices[0].delta.content story += text_chunk placeholder.markdown(story) print(story) return story
def generate_ai(num_personaggi, nome_ambientazione, creativita): # Costruzione del prompt in italiano per generare i personaggi prompt = ( f"Genera {num_personaggi} personaggi AMBIENTATI NELL'EPOCA {nome_ambientazione}, per un gioco di ruolo a carte." "Ogni personaggio deve avere i seguenti campi specificati nel modello ed devono essere COERENTI con l'epoca e l'ambientazione fornita (per esempio, se siamo nel Sacro Romano Impero devono essere nomi italiani) " "nel campo STORIA, inventa la una BREVE storia di background del personaggio specificando aneddoti, e altre cose che rendono il personaggio unico. " "Restituisci il risultato in formato JSON seguendo lo schema fornito")
# Esecuzione della chiamata all'API utilizzando il formato response_format completion = clientOpenAI.beta.chat.completions.parse( model=MODEL, messages=[ {"role": "system", "content": f"Sei un assistente utile per la generazione di personaggi per un gioco di RUOLO sul {nome_ambientazione}."}, {"role": "user", "content": prompt}, ], temperature=creativita, response_format=CharactersResponse, ) characters_response = completion.choices[0].message.parsed print(characters_response) return characters_responseclass Loghi(BaseModel): loghi: list[Logo]
def generate_card_ai(num_carte_azione, nome_ambientazione, creativita): def generate_ai(num_loghi, tema, creativita): prompt = ( f"Genera {num_carte_azione} CARTE AZIONE, per un gioco di ruolo ambientato in {nome_ambientazione}. " "Attieniti allo schema formito e sii molto SINTETICO")
# Esecuzione della chiamata all'API utilizzando il formato response_format f"Genera {num_loghi} prompt per la generazione immagini che trasmetta questo: {tema}" "Sii molto SINTETICO e usa SIMBOLI stilizzati e non troppi oggetti. Restituisci il risultato in formato JSON seguendo lo schema fornito") completion = clientOpenAI.beta.chat.completions.parse( model=MODEL, messages=[ {"role": "system", "content": f"Sei un assistente utile per la generazione di CARTE AZIONE per un gioco di RUOLO sul {nome_ambientazione}."}, {"role": "system", "content": f"Sei un assistente utile per la generazione di IDEE per la generazioni immagini su questo tema: {tema}."}, {"role": "user", "content": prompt}, ], temperature=creativita, response_format=ActionCardsResponse, response_format=Loghi, ) action_card_response = completion.choices[0].message.parsed return action_card_response loghi = completion.choices[0].message.parsed print(loghi) return loghi
# Funzione per generare le immagini, con gestione errori e retry dopo 10 secondidef generate_image(prompt, max_retries=5):4 unmodified lines response = client.images.generate( prompt=prompt, model="black-forest-labs/FLUX.1-schnell-Free", width=960, height=1440, width=1024, height=1024, steps=4, n=1, response_format="b64_json"6 unmodified lines st.error("Numero massimo di tentativi raggiunto. Impossibile generare le immagini.") return None
def generate_images(character: Character, carta_azione: ActionCard, stile_immagine, num_immagini, tipo_carta: str = None, nome_carta: str = None, colore_bordo: str = None): # Lista per salvare le immagini generate come tuple (nome_file, bytes) if character: images_bytes_list = [] if character.nome == "": if tipo_carta == "Personaggio": prompt = f"{character.english_description} WITH THIS BOLD INSCRIPTION '{nome_carta}'. BORDER COLOR = {colore_bordo}). Use this Style: {stile_immagine}" if tipo_carta == "Fronte": prompt = f"FRONT OF CARD GAME (WITH THIS BOLD INSCRIPTION IN THE CENTER OF THE CARD = '{nome_carta}'. BORDER COLOR = {colore_bordo}). Use this Syle: {stile_immagine}" if tipo_carta == "Retro": prompt = ("Design an ornate playing card back with symmetrical filigree" "At the center, prominently display the name '" + nome_carta + "' in an elegant, calligraphic style. ") prompt += f"BACKGROUND COLOR = {colore_bordo}. Use this Syle: {stile_immagine}" else: prompt = f"{character.english_description} {stile_immagine}. (INSERT TEXT NAME = {character.nome})" st.subheader(f"{character.nome} 🦸♂️") st.write( f"- **Classe:** {character.classe}\n" f"- **Forza:** {character.forza}\n" f"- **Destrezza:** {character.destrezza}\n" f"- **Intelligenza:** {character.intelligenza}\n" f"- **Descrizione:** {character.descrizione}\n", f"- **Storia:** {character.storia}" ) if carta_azione: prompt = f"PLAYING CARD FOR A CARD GAME WITHOUT CHARACTER WITH THIS BOLD INSCRIPTION IN THE CENTER OF THE CARD: '{carta_azione.nome_azione}'. USE THIS STYLE: {stile_immagine}.)"def generate_images(logo: Logo, nome_stile, stile_immagine, num_immagini, colori: list[str] = None): if logo: images_bytes_list = [] st.subheader(f"{carta_azione.nome_azione} 🃏") st.write(f"- **Descrizione:** {carta_azione.breve_descrizione}") colors_str = " ".join(colori) if colori else ""
prompt = f"Create a Simple Logo in {nome_stile} STYLE background white, and COLORS '{colors_str}' of '{logo.english_description}'. use this style {stile_immagine}" st.subheader(f"{logo.nome} 🖌️") st.write(logo.descrizione) print(prompt) for numero in range(num_immagini): images_data = generate_image(prompt)14 unmodified lines return images_bytes_list
def main(): st.title("Imperium AI 🏰") st.title("Logo Generator AI 🎨") st.sidebar.header("Impostazioni") selected_ambientazione = st.sidebar.selectbox("Ambientazione", list(ambientazioni.keys()), index=0) stile_default = ambientazioni[selected_ambientazione]["stile_immagine"] nome_ambientazione = ambientazioni[selected_ambientazione]["nome"] selected_stile = st.sidebar.selectbox("Ambientazione", list(LOGO_STYLES.keys()), index=0) stile_default = LOGO_STYLES[selected_stile]["stile_immagine"] nome_stile = LOGO_STYLES[selected_stile]["nome"] stile_immagine = st.sidebar.text_area("Stile Immagine", stile_default, disabled=False) auto = False auto = st.sidebar.toggle(label= 'Generazione automatica', value = True) auto = True tema = st.sidebar.text_input("Tema Logo", value="Formazione Aziendale") #auto = st.sidebar.toggle(label= 'Generazione automatica', value = True) prompt_input = "" if auto == False: tipo_carta = st.sidebar.selectbox("Tipo Carta",("Personaggio", "Fronte", "Retro")) if tipo_carta == "Personaggio": prompt_input = st.sidebar.text_input("Prompt Immagine (in inglese)", value="Soldier") nome_carta = st.sidebar.text_input("Nome Carta", value="Carmine") colore_bordo = st.sidebar.selectbox("Colore Bordo",("Blue", "Red", "Yellow", "Green", "Pink", "Black", "White")) else: num_personaggi = st.sidebar.slider("Personaggi", min_value=0, max_value=30, value=5, disabled=not auto) num_carte_azione = st.sidebar.slider("Carte Azione", min_value=0, max_value=10, value=0, disabled=not auto) genera_regolamento = st.sidebar.toggle(label= 'Regolamento', value = True, disabled=not auto) tipo_carta = None nome_carta = None colore_bordo = None
num_immagini = st.sidebar.slider("Variazioni Immagini", min_value=1, max_value=6, value=2) creativita = st.sidebar.slider("Creativita", min_value=0.1, max_value=1.0, value=0.8, step=0.1) colori = st.sidebar.multiselect("Colori", ["Blue", "Orange", "Green", "Yellow", "Red", "Purple"], ["Blue", "Orange"]) num_loghi = st.sidebar.slider("Loghi", min_value=0, max_value=30, value=10, disabled=not auto) num_immagini = st.sidebar.slider("Variazioni", min_value=1, max_value=6, value=2) creativita = st.sidebar.slider("Creativita", min_value=0.1, max_value=1.0, value=0.95, step=0.1) submit_button = st.sidebar.button(label="Genera Immagine", type="primary", use_container_width=True) st.write("Forgia il tuo **destino nell'Impero**: crea, combatti e domina nel più grande gioco di ruolo a carte generato dall'AI") st.write("Genera il tuo **Logo Aziendale** tramite l'AI") if submit_button: carte_azione = None characters = None if auto: if num_personaggi > 0: with st.spinner('Generazione Personaggi'): characters = generate_ai(num_personaggi, nome_ambientazione, creativita) st.subheader('Personaggi 🎭') df = pd.DataFrame([{k: v for k, v in character.model_dump().items() if k != "english_description"} for character in characters.personaggi]) st.dataframe(df, hide_index=True, use_container_width=True) st.divider() if num_carte_azione > 0: with st.spinner('Generazione Carte Azione'): carte_azione = generate_card_ai(num_carte_azione, nome_ambientazione, creativita) st.subheader('Carte Azione 📒') df = pd.DataFrame([{k: v for k, v in carta_azione.model_dump().items() if k != "breve_descrizione_inglese"} for carta_azione in carte_azione.carte_azione]) if num_loghi > 0: with st.spinner('Generazione Loghi'): loghi = generate_ai(num_loghi, tema, creativita) st.subheader('Loghi 💡') df = pd.DataFrame([{k: v for k, v in logo.model_dump().items() if k != ""} for logo in loghi.loghi]) st.dataframe(df, hide_index=True, use_container_width=True) st.divider() if genera_regolamento: with st.spinner('Generazione Regolamento'): generate_story(characters, carte_azione, nome_ambientazione, creativita) st.divider() else: characters = CharactersResponse(personaggi=[Character(nome="", classe="Guerriero", forza=10, destrezza=8, intelligenza=6, storia="", descrizione="Un forte guerriero", english_description=prompt_input)]) with st.spinner('Generazione Immagini'): images = [] if characters: for character in characters.personaggi: images.extend(generate_images(character, None, stile_immagine, num_immagini, tipo_carta, nome_carta, colore_bordo)) if carte_azione: for carta_azione in carte_azione.carte_azione: images.extend(generate_images(None, carta_azione, stile_immagine, num_immagini)) if loghi: for logo in loghi.loghi: images.extend(generate_images(logo, nome_stile, stile_immagine, num_immagini, colori)) if images: zip_buffer = io.BytesIO() with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:10 unmodified lines st.success("Immagini generate con successo!")
if __name__ == "__main__": st.set_page_config(page_title="Imperium AI", page_icon="🏰", layout="wide") main() st.set_page_config(page_title="Logo Generator AI", page_icon="🎨", layout="wide") main()app.py+69-266