import gradio as gr
import pandas as pd
levels={
'KB':1,
'MB':2,
'GB':3,
'TB':4,
'PB':5,
}
B_Price = 400/(2**(10*4))
def discount_ratio_fn(disk_size:float,unit:str):
level = levels[unit]
SByte_base = 1000**level
LByte_base = 2**(10*level)
SBytes = disk_size * SByte_base
LBytes = disk_size * LByte_base
discount_ratio = SBytes/LBytes
return discount_ratio
x_values=[
(1,'KB'),
#(10,'KB'),
#(100,'KB'),
(1,'MB'),
#(10,'MB'),
#(100,'MB'),
(1,'GB'),
#(10,'GB'),
#(100,'GB'),
(1,'TB'),
#(10,'TB'),
#(100,'TB'),
(1,'PB'),
#(10,'PB'),
#(100,'PB'),
]
y_float_numbers = [(1-discount_ratio_fn(*x))*100 for x in x_values]
x_labels = [f"{dsize}{unit}" for dsize,unit in x_values]
data_frame = pd.DataFrame(
{
'size':x_labels,
'discount':y_float_numbers
})
def process(disk_size:float,unit:str):
if unit not in levels:
return '容量单位有误,请重新选择'
level = levels[unit]
SByte_base = 1000**level
LByte_base = 2**(10*level)
SBytes = disk_size * SByte_base
LBytes = disk_size * LByte_base
discount_ratio = SBytes/LBytes
steal_ratio = 1-discount_ratio
Bytes_stolen = LBytes - SBytes
Yuan_stolen = B_Price * Bytes_stolen
SB_to_LB = disk_size * discount_ratio
text = f'''
Claimed Size {disk_size:.2f} {unit}
Actual Size {SB_to_LB:.2f} {unit}.
{steal_ratio*100:.2f}% Space stolen by the SSD Brand.
{Yuan_stolen:.5f}¥ Stolen.
-----------------------------------------------------
商品标注容量: {disk_size:.2f} {unit}
实际容量: {SB_to_LB:.2f} {unit} 以计算机标准计。
{steal_ratio*100:.2f}% 的空间被厂商偷窃.
您的 {Yuan_stolen:.5f}¥ 被厂商明抢.
'''
return text
with gr.Blocks() as demo:
gr.Interface(
process,
inputs=[gr.Number(value=1,info="硬盘厂商声称容量"),gr.Dropdown(['KB','MB','GB','TB','PB'],value='TB',info="容量单位")],
outputs=[gr.Text()]
)
gr.Text('下图展示了,您购买的容量越大,厂商克扣您容量的比例越大:')
gr.LinePlot(
data_frame,
x="size",
y="discount",
x_title="购买容量",
y_title="厂商克扣百分比%"
)
demo.launch()
# Same syntax as requirements.txt
pandas