File size: 3,191 Bytes
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
    Title : Landing Page of the application
    @author : Sakshi Tantak
"""

# Imports
import base64
import requests

import streamlit as st

from filepaths import LOGO_PATH, POSTER_PATH

st.set_page_config(page_title = 'Dungeons & Dragons', 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)

if 'llm_endpoint' not in st.session_state:
    st.session_state['llm_endpoint'] = ''

if 'stable_diffusion_endpoint' not in st.session_state:
    st.session_state['stable_diffusion_endpoint'] = ''

def add_bg_from_local(image_file):
    with open(image_file, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
    st.markdown(
    f"""
    <style>
    .stApp {{
        image: url(data:image/{"jpeg"};base64,{encoded_string.decode()});
        size: auto
    }}
    </style>
    """,
    unsafe_allow_html=True
    )

def get_api_endpoints():
    form = st.form('api-endpoints')
    with form:
        llm_endpoint = st.text_input(label = 'LLM Endpoint',
                                    value = 'https://643b-34-132-196-118.ngrok-free.app')

        stable_diffusion_endpoint = form.text_input(label = 'Stable Diffusion Endpoint',
                                                    value = 'https://643b-34-132-196-118.ngrok-free.app')
        submitted = st.form_submit_button('Add URLs')
    if submitted:
        with st.spinner('Checking if the URLs are valid'):
            try:
                llm_response = requests.get(url = llm_endpoint)
                if llm_response.status_code == 200 and llm_response.text == 'Dungeon Master':
                    st.success('Hurrah! Your LLM endpoint works!')
                else:
                    st.error('Err! This LLM endpoint is invalid or is not working!')
            except:
                st.error('Err! This LLM endpoint is invalid or is not working!')

            try:
                sd_response = requests.get(url = stable_diffusion_endpoint)
                if sd_response.status_code == 200 and sd_response.text == 'Generate Avatar':
                    st.success('Hurrah! Your Stable Diffusion endpoint works!')
                else:
                    st.error('Err! This Stable Diffusion endpoint is invalid or is not working!')
            except:
                st.error('Err! This Stable Diffusion endpoint is invalid or is not working!')
            return llm_endpoint, stable_diffusion_endpoint
    return None, None

def main():
    set_centered_heading('Dungeons & Dragons')
    placeholder = st.empty()
    with placeholder:
        cols = st.columns(2)
        with cols[0]:
            st.image(POSTER_PATH)

        with cols[-1]:
            llm_endpoint, stable_diffusion_endpoint = get_api_endpoints()
            if llm_endpoint is not None and stable_diffusion_endpoint is not None:
                print(llm_endpoint, stable_diffusion_endpoint)
                st.session_state['llm_endpoint'] = llm_endpoint
                st.session_state['stable_diffusion_endpoint'] = stable_diffusion_endpoint

if __name__ == '__main__':
    main()