text stringlengths 1 93.6k |
|---|
maxk = max(topk)
|
batch_size = target.size(0)
|
_, pred = output.topk(maxk, 1, True, True)
|
pred = pred.t()
|
correct = pred.eq(target.view(1, -1).expand_as(pred))
|
res = []
|
for k in topk:
|
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
|
res.append(correct_k.mul_(100.0 / batch_size))
|
return res
|
if __name__ == '__main__':
|
main()
|
# <FILESEP>
|
# -*- coding: utf-8 -*-
|
import re
|
from fasthtml.common import *
|
from fasthtml.fastapp import *
|
from beancount import Beancount
|
title = 'Bean-Add'
|
app, rt = fast_app(
|
static_path='./icons',
|
htmlkw={'data_theme': 'light'}
|
)
|
bean = Beancount()
|
bean.bean_cache()
|
def new_input():
|
return Input(
|
id='raw_input', placeholder='add new', required=True,
|
autocapitalize='off', autocomplete='off', autocorrect='off',
|
hx_post='/complete', target_id='current',
|
hx_swap='innerHTML transition:true',
|
hx_trigger='keyup changed delay:300ms'
|
)
|
@app.get('/')
|
def index():
|
favicon = Link(rel='icon', type='image/png', href='/Assets.png')
|
input_box = new_input()
|
input_button = Button(
|
'Enter', id='add_button',
|
hx_post='/enter', target_id='output', hx_swap='beforeend transition:true',
|
hx_trigger='click', hx_include='#raw_input'
|
)
|
box_enter = Div(
|
hx_post='/enter', target_id='output', hx_swap='beforeend transition:true',
|
hx_trigger="keyup[key=='Enter'] from:#raw_input changed", hx_include='#raw_input'
|
)
|
box_clear = Div(
|
hx_post='/clear', target_id='raw_input', hx_swap='outerHTML transition:true',
|
hx_trigger="keyup[key=='Enter'] from:#raw_input"
|
)
|
button_clear = Div(
|
hx_post='/clear', target_id='raw_input', hx_swap='outerHTML transition:true',
|
hx_trigger='click from:#add_button'
|
)
|
input_group = Group(input_box, input_button, box_enter, box_clear)
|
current = Div(P('...'), id='current')
|
output = Div(H3('History'), id='output')
|
card = Card(current, header=input_group, footer=output)
|
return Title(title), favicon, Main(H1(title), card, cls='container')
|
@app.post('/complete')
|
def complete(raw_input:str):
|
if not raw_input.strip():
|
return P('...')
|
output = bean.bean_add(raw_input.strip().split())[0]
|
if len(raw_input.split()) < 4:
|
return output['subtitle']
|
return output['arg'].replace('\n', '<br>')
|
@app.post('/enter')
|
def enter(raw_input:str):
|
if len(raw_input.split()) < 4:
|
return
|
output = bean.bean_add(raw_input.strip().split())[0]
|
with open(bean.settings['default_ledger'], 'r') as ledger_file:
|
content_lines = ledger_file.readlines()
|
content_lines.insert(len(content_lines)-3, '\n'+output['arg']+'\n')
|
with open(bean.settings['default_ledger'], 'w') as ledger_file:
|
ledger_file.write("".join(content_lines))
|
return Pre(re.sub(' +', ' ', output['arg']))
|
@app.post('/clear')
|
def clear():
|
return new_input()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.