File size: 4,525 Bytes
18fb155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os

import pandas as pd
import streamlit as st
from streamlit_extras.grid import grid
from streamlit_card import card

from lida_ko import Manager, TextGenerationConfig
from lida_ko.datamodel import Goal
from lida_ko.utils import clean_code_snippet

openai_api_key = os.environ["OPENAI_API_KEY"]
selected_dataset = st.session_state.selected_dataset
selected_method = st.session_state.selected_method
selected_model = st.session_state.selected_model
use_cache = st.session_state.use_cache
temperature = st.session_state.temperature
lida_manager: Manager = st.session_state.lida_manager
summary = st.session_state.summary 
selected_goal_object: Goal = st.session_state.selected_goal_object
selected_dataframe: pd.DataFrame = st.session_state.selected_dataframe
num_visualizations = st.session_state.num_visualizations

def generate_visualizations(code = None, feedback=None):
    if code and feedback:
        visualizations = lida_manager.repair(
            code=code,
            goal=selected_goal_object,
            summary=summary,
            feedback=feedback,
            textgen_config=textgen_config,
            library=st.session_state.selected_library
        )
    else:
        visualizations = lida_manager.visualize(
                summary=summary,
                goal=selected_goal_object,
                textgen_config=textgen_config,
                library=st.session_state.selected_library)
    return visualizations

st.title("๐Ÿ“Š ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ๋งŒ๋“ค๊ธฐ")
st.write("")
st.empty()

if not selected_goal_object:
    st.error("**ERROR**: ๐Ÿšจ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ชฉํ‘œ๋ฅผ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.")
    st.stop()

if selected_goal_object:
    # Update the visualization generation call to use the selected library.

    textgen_config = TextGenerationConfig(
        n=num_visualizations, temperature=temperature,
        model=selected_model,
        use_cache=use_cache)

    # **** lida.visualize *****
    if not st.session_state.update_viz:
        visualizations = generate_visualizations()
        st.session_state.visualizations = visualizations
    else:
        st.session_state.update_viz = False

    col1, col2, col3 = st.columns([5, 0.5, 5])
    def render_visualization(idx, viz):
        st.write(f'### ๐ŸŒŸ ์‹œ๊ฐํ™” {idx + 1}')
        if viz:
            with st.spinner("์ธ๊ณต์ง€๋Šฅ์ด ์‹œ๊ฐํ™”๋ฅผ ์ƒ์„ฑ์ค‘์ž…๋‹ˆ๋‹ค..."):
                try:
                    if st.session_state.selected_library == "plotly":
                        data = st.session_state.selected_dataframe
                        # extract the code from the generated responses and execute it
                        temp_namespace = {
                            'data': data,
                        }
                        exec(clean_code_snippet(viz['code']), temp_namespace)
                        fig = st.plotly_chart(temp_namespace['chart'])
                    else:
                        from PIL import Image
                        import io
                        import base64

                        imgdata = base64.b64decode(viz.raster)
                        img = Image.open(io.BytesIO(imgdata))
                        st.image(img, caption=f"Visualization {idx + 1}", use_column_width=True)
                except Exception as e:
                    st.error(f"Error loading visualization: {e}")
        with st.popover("๐Ÿง‘โ€๐Ÿ’ป ์ฝ”๋“œ ํ™•์ธํ•˜๊ธฐ", use_container_width=True):
            if isinstance(viz, dict):
                code_string = viz['code']
            else:
                code_string = viz.code
            st.code(clean_code_snippet(code_string))
        with st.popover("๐Ÿ—จ๏ธ ๋ณ€๊ฒฝ ์š”์ฒญํ•˜๊ธฐ", use_container_width=True):
            chat_message = st.chat_input("(๊ตฌํ˜„ ์ค‘)๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ์„ ์ž์—ฐ์–ด ๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”",key=f"chat_message_{idx}",
                                         disabled=True)
            if chat_message:
                st.session_state.visualizations = generate_visualizations(viz['code'], chat_message)
                st.session_state.update_viz = True
                st.rerun()
                render_visualization(idx, st.session_state.visualizations[idx])

        return fig

    with col1:
        idx = 0
        selected_viz = st.session_state.visualizations[idx]
        render_visualization(idx, selected_viz)

    with col2:
        st.empty()
        
    with col3:
        idx = 1
        selected_viz = visualizations[idx]
        render_visualization(idx, selected_viz)