File size: 2,133 Bytes
4eb8ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0613982
4eb8ef9
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
    Build Your Character
    @author : Sakshi Tantk
"""

# Imports
from io import BytesIO
import requests
from PIL import Image

import streamlit as st
from filepaths import LOGO_PATH

st.set_page_config(page_title = 'Build Your Character', layout = 'wide',
                   page_icon = LOGO_PATH)

def set_centered_heading(text):
    st.markdown(f"<h1 style='text-align: center; color: #FF5733;'>{text}</h1>", unsafe_allow_html=True)

set_centered_heading('Build Your Characters')

if 'avatars' not in st.session_state:
    st.session_state['avatars'] = {}

def get_parameters():
    i = 5
    while(i > 0):
        form = st.form(f'Character {i+1}')
        with form:
            character_gender = st.selectbox('Gender', options = ['Male', 'Female', 'Other'])
            character_class = st.selectbox('Class',
                                options = ['barbarian', 'bard', 'cleric',
                                           'druid', 'fighter', 'monk',
                                           'paladin', 'ranger', 'rogue',
                                           'sorcerer', 'warlock', 'wizard'
                                        ])
            submit_btn = st.form_submit_button('Done')
        if submit_btn:
            with st.spinner('AI is generating your avatar ;) '):
                prompt = f'Generate ONE Dungeons & Dragons character of gender {character_gender} and belonging to the {character_class} class'
                response = requests.get(url = st.session_state['stable_diffusion_endpoint'] + '/get-avatar',
                                    json = {'avatar_description' : prompt})
                if response.status_code == 200:
                    image_bytes = BytesIO(response.content)
                    avatar = Image.open(image_bytes)
                    st.session_state['avatars'][i+1] = avatar
                    print(st.session_state['avatars'])
                    st.image(avatar, caption = character_class)
        i-=1
        if st.button('Add character?', key = f'add-btn-{i+1}'):
            continue
        break

if __name__ == '__main__':
    get_parameters()