|
|
import gradio as gr
|
|
|
import pandas as pd
|
|
|
import matplotlib
|
|
|
matplotlib.use("Agg")
|
|
|
import matplotlib.pyplot as plt
|
|
|
import inspect
|
|
|
import io
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
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>')
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
|
with gr.Row():
|
|
|
gr.Markdown(f'<h1 class="subtitle">{title}</h1>')
|
|
|
download_btn = gr.DownloadButton(
|
|
|
label="Download CSV",
|
|
|
value=get_file,
|
|
|
elem_id=f"download-{key}" if key else None
|
|
|
)
|
|
|
|
|
|
|
|
|
gr.Dataframe(
|
|
|
value=df.head(20),
|
|
|
headers=list(df.columns),
|
|
|
elem_id=f"table-{key}" if key else None,
|
|
|
interactive=False,
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
def describe_value_counts(series):
|
|
|
description = series.describe().to_frame(name='value')
|
|
|
description = description.reset_index()
|
|
|
description.columns = ['Statistic', 'Value']
|
|
|
return description
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
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:
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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,
|
|
|
value=[] if Multiselect else None,
|
|
|
elem_classes="dropdown"
|
|
|
)
|
|
|
return multiple_choice |