File size: 8,002 Bytes
213e089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd2bea0
 
213e089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7df087
 
 
 
213e089
f7df087
 
 
 
 
213e089
f7df087
213e089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd2bea0
 
 
 
 
 
 
213e089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import gradio as gr
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import inspect
import io


# style formating for Header
def header(input:str):
    """

    Usage:

        header('your text')

    Output: 

        <h1 class="header"> {input} <h1>

        output will be bold. use for container header only

    Args:

        input (str): _header_Title_

    """
    gr.Markdown(f"# {input}", elem_classes='header')

# style formating for Header2
def h2(input:str):
    """

        Usage:

            h2('your text')

        Output: 

            <h2 class="subheader"> {input} <h2>

        output will be bold. use for optional

    Args:

        input (str): _subheader_Title_

    """
    gr.Markdown(f'<h2 class="subheader" style="black">{input}</h2>')

# style formating for Text
def p(input:str):
    """

    Usage:

        p('''

            text <br>

            text

        ''')

        

        or

        

        p('text')

    Outputs:

        Multiple <p class="desc">...</p> blocks, one per paragraph.

    """
    paragraphs = input.strip().split("<br>")
    text = ''.join(f'<p class="desc">{para.strip()}</p>' for para in paragraphs if para.strip())
    return gr.Markdown(text)

# this for displaying dataframe and also provied downlaod csv
def Dataset(df,title, source, key=None):
    def get_file():
        return source

    with gr.Column(elem_classes='dataframe-layout', elem_id=f"dataset-{key}" if key else None):
        # Title and download button in a row
        with gr.Row():
            gr.Markdown(f'<h1 class="subtitle">{title}</h1>') # title formating
            download_btn = gr.DownloadButton(
                label="Download CSV",
                value=get_file,
                elem_id=f"download-{key}" if key else None
            )
        
        # Dataframe display
    gr.Dataframe(
        value=df.head(20),
        headers=list(df.columns),
        elem_id=f"table-{key}" if key else None,
        interactive=False,    # read only
        # disable the warp for reduce height of data
        # wrap=True 
    )

def describe_value_counts(series):
    description = series.describe().to_frame(name='value')
    description = description.reset_index()                       # Move index (stat name) into column
    description.columns = ['Statistic', 'Value']     
    return description

# this is for EDA, preprocess
def plot_distribution(df, column):
    """

    Generates a matplotlib plot (bar chart or histogram) showing the distribution 

    of values in a selected column from the dataframe.



    Parameters:

    -----------

    df : pd.DataFrame

        The dataframe to plot from.

    column : str

        The column name to visualize.



    Returns:

    --------

    matplotlib.figure.Figure

        A figure object representing the distribution plot.

    """
    fig, ax = plt.subplots(figsize=(10, 5))
    
    if df[column].dtype == 'object' or df[column].nunique() < 20:
        # Bar plot for categorical/small unique values
        value_counts = df[column].value_counts().head(20)
        ax.bar(value_counts.index, value_counts.values)
        ax.set_xticklabels(value_counts.index, rotation=45, ha='right')
        ax.set_ylabel('Count')
        ax.set_title(f'Distribution of {column}')
    else:
        # Histogram for numerical
        ax.hist(df[column].dropna(), bins=100, edgecolor='black')
        ax.set_title(f'Distribution of {column}')
        ax.set_xlabel(column)
        ax.set_ylabel('Frequency')

    fig.tight_layout()
    return fig

## this is for eda, preprocess, and training
def code_cell(code):
    """

        simply syntax for gr.code

    Usage :

        Code_cell('df = pd.read_csv(path)')

    or 

        using triple string for multiple line

        code_cell("""""")

    """
    gr.Code(inspect.cleandoc(code), language='python')

## This for EDA, Preprocess, and training
def plot_training_results(x,y1,y2,y1label,y2label,ylabel,xlabel,title=""):
    fig,ax=plt.subplots(figsize=(8,5))
    ax.plot(x, y1, marker='o', label=y1label, color='blue')
    ax.plot(x, y2, marker='s', label=y2label, color='orange')

    ax.set_title(title)
    ax.set_xlabel(xlabel=xlabel)
    ax.set_ylabel(ylabel=ylabel)
    ax.legend()
    ax.grid(True)

    return fig

# for Recommendation section
def input_name_textbox(Label:str, Placeholder:str):
    """

    usage:

        app_name = input_name_textbox('Input Your App', 'Enter game title...')

    Args:

        Label (str): Title textbox

        Placeholder (str): placeholder text



    Returns:

        variable : str

    """

    inputbox = gr.Textbox(
        label=Label,
        placeholder=Placeholder,
        elem_classes="text-input"
    )
    return inputbox

def input_number(Label:str,Precision = 0,**kwargs):
    """

    usage:

        app_name = input_number('Input Number', 'Enter game number...')

    Args:

        Label (str): Title textbox

        Placeholder (str): placeholder text



    Returns:

        variable : str

    """

    inputbox = gr.Number(
        label=Label,
        elem_classes="text-input",
        precision=Precision,
        **kwargs
    )
    return inputbox
def show_missing_values(df:pd.DataFrame):
    try:
        missing_df = pd.DataFrame(df.isnull().sum(), columns=['Missing Values'])
        missing_df = missing_df.reset_index().rename(columns={'index': 'Column'})
        return missing_df
    except Exception as e:
        return pd.DataFrame({'Error': [str(e)]})

def input_paragaph_textbox(Label:str, Placeholder:str):
    """

    usage:

        paragraph = input_paragaph_textbox('Your Story', 'Type your text...')

    Args:

        Label (str): Title textbox

        Placeholder (str): placeholder text



    Returns:

        variable : str

    """
    paragraph = gr.Textbox(
            label=Label,
            placeholder=Placeholder,
            lines=5,
            max_lines=8,
            max_length=1200,
            elem_classes="text-input"
        )
    return paragraph

def input_choice(Label:str, Choices:list, Multiselect:bool):
    """Allow user to select choices\n

        Multiselect True for multiple choices\n

        Multiselect False for single choices\n

    Usage:\n

        genre = gr.Dropdown(\n

            label="Select Your Genre (Multiple Choice)",\n

            choices=[\n

                'Action', 'Adventure', 'RPG', 'Strategy', 'Simulation',\n

                'Casual', 'Indie', 'Sports', 'Racing', 'Fighting',\n

                'Puzzle', 'Shooter', 'Platformer', 'MMO', 'Horror',\n

                'Survival', 'Open World', 'Visual Novel', 'Point & Click',\n

                'Sandbox', 'Metroidvania', 'Tactical', 'Rhythm',\n

                'Stealth', 'Rogue-like', 'Rogue-lite'\n

            ],\n

            multiselect=True,\n

            value=[],\n

            elem_classes="dropdown"\n

        )\n

    

    or only single choice \n

    

        price_range_input = gr.Dropdown(\n

            label="Select Your Price Range (Only Single Choice)",\n

            choices=[\n

                'Free',\n

                '5$ - 10%',\n

                '10$ - 50%',\n

                '50$ - 100%',\n

                '100$ - 500%',\n

                'above 500%',\n

            ],

            multiselect=False,\n

            value=[],\n

            elem_classes="dropdown"\n

        )\n

    Args:\n

        Label (str): _description_\n

        Choices (list): _description_\n

    """
    multiple_choice = gr.Dropdown(
        label=Label,
        choices=Choices,
        multiselect=Multiselect, # True Allowing multi select
        value=[] if Multiselect else None, # the choosen value will be passed here
        elem_classes="dropdown"
    )
    return multiple_choice