Download app.py from aresspark/learn: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/spaces/aresspark/learn/resolve/main/app.py
- Command line
-
hf download hf://spaces/aresspark/learn/app.py
-
curl -L -o app.py https://huggingface.co/spaces/aresspark/learn/resolve/main/app.py
12.3 kB
| # !/usr/bin/env python3 | |
| """ | |
| ==== No Bugs in code, just some Random Unexpected FEATURES ==== | |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| โโโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโโ | |
| โโEscโ!1 โ@2 โ#3 โ$4 โ%5 โ^6 โ&7 โ*8 โ(9 โ)0 โ_- โ+= โ|\ โ`~ โโ | |
| โโโโโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโโโคโ | |
| โโ Tab โ Q โ W โ E โ R โ T โ Y โ U โ I โ O โ P โ{[ โ}] โ BS โโ | |
| โโโโโโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโฌโโโดโโโโโโคโ | |
| โโ Ctrl โ A โ S โ D โ F โ G โ H โ J โ K โ L โ: ;โ" 'โ Enter โโ | |
| โโโโโโโโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโฌโโดโโโโโฌโโโโคโ | |
| โโ Shift โ Z โ X โ C โ V โ B โ N โ M โ< ,โ> .โ? /โShift โFn โโ | |
| โโโโโโโโฌโโโดโฌโโโดโโโฌโดโโโโดโโโโดโโโโดโโโโดโโโโดโโโฌโดโโโโดโฌโโโดโฌโโโโโโดโโโโโ | |
| โ โFn โ Alt โ Space โ Alt โWinโ HHKB โ | |
| โ โโโโโดโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโดโโโโ โ | |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| web็ซฏๆต่ฏLLMๆๆใ | |
| Author: pankeyu | |
| Date: 2023/03/17 | |
| """ | |
| import os | |
| import re | |
| import time | |
| import json | |
| import datetime | |
| import torch | |
| import streamlit as st | |
| import pandas as pd | |
| from transformers import AutoTokenizer, AutoModel | |
| torch.set_default_tensor_type(torch.cuda.HalfTensor) | |
| st.set_page_config( | |
| page_title="LLM Playground", | |
| layout="wide" | |
| ) | |
| device = 'cuda:0' | |
| max_new_tokens = 300 | |
| model_path = "checkpoints/model_1000" | |
| LOG_PATH = 'log' | |
| DATASET_PATH = 'data' | |
| LOG_FILE = 'web_log.log' | |
| FEEDBACK_FILE = 'human_feedback.log' | |
| DATASET_FILE = 'dataset.jsonl' | |
| if not os.path.exists(DATASET_PATH): | |
| os.makedirs(DATASET_PATH) | |
| if not os.path.exists(LOG_PATH): | |
| os.makedirs(LOG_PATH) | |
| if not os.path.exists(os.path.join(DATASET_PATH, DATASET_FILE)): | |
| with open(os.path.join(DATASET_PATH, DATASET_FILE), 'w', encoding='utf8') as f: | |
| print('ๆ ๆณจๆฐๆฎ้ๅทฒๅๅปบใ') | |
| if 'model_out' not in st.session_state: | |
| st.session_state['model_out'] = '' | |
| st.session_state['used_time'] = 0.0 | |
| if 'model' not in st.session_state: | |
| with st.spinner('Loading Model...'): | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| model_path, | |
| trust_remote_code=True | |
| ) | |
| model = AutoModel.from_pretrained( | |
| model_path, | |
| trust_remote_code=True | |
| ).half().to(device) | |
| st.session_state['model'] = model | |
| st.session_state['tokenizer'] = tokenizer | |
| def start_evaluate_page(): | |
| """ | |
| ๆจกๅๆต่ฏ้กต้ขใ | |
| """ | |
| c1, c2 = st.columns([5, 5]) | |
| with c1: | |
| with st.expander('โ๏ธ Instruct ่ฎพๅฎ๏ผInstruct Setting๏ผ', expanded=True): | |
| instruct = st.text_area( | |
| f'Instruct', | |
| value='ไฝ ็ฐๅจๆฏไธไธชๅพๅๅฎณ็้ ่ฏป็่งฃๅจ๏ผไธฅๆ ผๆ็ งไบบ็ฑปๆไปค่ฟ่กๅ็ญใ', | |
| height=250 | |
| ) | |
| with c2: | |
| with st.expander('๐ฌ ๅฏน่ฏ่พๅ ฅๆก', expanded=True): | |
| current_input = st.text_area( | |
| 'ๅฝๅ็จๆท่พๅ ฅ', | |
| value='ๅธฎๆๆๅๅบไธ้ขๅฅๅญไธญๆๆ็SPO๏ผๅนถ่พๅบไธบjson๏ผไธ่ฆๅๅคไฝ็ๅๅค๏ผ\n\nใ็ ็ๆฆใๆฏ็ฑๅฑฑไธๅฝฑ่งไผ ๅช้ๅขใๅฑฑไธๅฝฑ่งๅถไฝๆ้ๅ ฌๅธใๅไบฌๅๆๆฌฃๆฌฃๅฝฑไธๆ่ตๆ้ๅ ฌๅธใๅไบฌๅ้ขๅคฉๅฐๅฝฑ่งๆๅๆ้ๅ ฌๅธใๅไบฌๅฃๅบๅฝฑไธๆ้ๅ ฌๅธใไธ้ณๆญฃๅ้ณๅ ๅฝฑ่งๆ้ๅ ฌๅธ่ๅๅบๅ๏ผ็ฑๅญ็ฌใๆ้ชๆงๅฏผ๏ผ่กๆญใๅๆถใ็ๅฏใ้ป็ปดๅพทใ้้พใๅด็ฃใ้ซ้ซ็ญไธปๆผ็ๅค่ฃ ๅงใ', | |
| height=200 | |
| ) | |
| bt = st.button('Generate') | |
| if bt: | |
| start = time.time() | |
| with st.spinner('็ๆไธญ...'): | |
| with torch.no_grad(): | |
| input_text = f"Instruction: {instruct}\n" | |
| input_text += f"Input: {current_input}\n" | |
| input_text += f"Answer:" | |
| batch = st.session_state['tokenizer'](input_text, return_tensors="pt") | |
| out = st.session_state['model'].generate( | |
| input_ids=batch["input_ids"].to(device), | |
| max_new_tokens=max_new_tokens, | |
| temperature=0 | |
| ) | |
| out_text = st.session_state['tokenizer'].decode(out[0]) | |
| answer = out_text.split('Answer: ')[-1] | |
| used_time = round(time.time() - start, 2) | |
| st.session_state['model_out'] = answer | |
| st.session_state['used_time'] = used_time | |
| with open(os.path.join(LOG_PATH, LOG_FILE), 'a', encoding='utf8') as f: | |
| log_dict = { | |
| 'time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), | |
| 'used_seconds': used_time, | |
| 'instruct': instruct, | |
| 'input': current_input, | |
| 'model_output': st.session_state['model_out'] | |
| } | |
| f.write(f'{json.dumps(log_dict, ensure_ascii=False)}\n') | |
| if st.session_state['model_out']: | |
| c1, c2 = st.columns([5, 5]) | |
| with c2: | |
| with st.expander(f"๐ค ๅฝๅๆจกๅ่พๅบ๏ผ{st.session_state['used_time']}s๏ผ", expanded=True): | |
| answer = st.session_state['model_out'] | |
| if len(answer) < 200: | |
| height = 100 | |
| elif len(answer) < 500: | |
| height = 200 | |
| else: | |
| height = 300 | |
| st.text_area( | |
| '', | |
| value=f'{answer}', | |
| height=height | |
| ) | |
| human_feedback = st.radio( | |
| "ๆจกๅ็ๆ็ปๆๆฏๅฆๆญฃ็กฎ ๐", | |
| ["๐ค ไธๅ้ฆ", "๐ ๆญฃ็กฎ", "๐ก ้่ฏฏ"], | |
| key="visibility", | |
| horizontal=True | |
| ) | |
| if human_feedback in ["๐ก ้่ฏฏ", "๐ ๆญฃ็กฎ"]: | |
| if human_feedback == "๐ก ้่ฏฏ": | |
| error_feedback = st.text_area( | |
| '[error feedback]', | |
| placeholder='ๆ่ฐขๆจ็ๅ้ฆ๏ผ่ฏทๅกซๅ้ฎ้ข็ๆญฃ็กฎ็ญๆกไปฅๅธฎๅฉๆจกๅๆน่ฟ ๐ฅบ' | |
| ) | |
| current_feedback = { | |
| 'instruct': instruct, | |
| 'input': current_input, | |
| 'model_output': st.session_state['model_out'], | |
| 'human_feedback': error_feedback, | |
| 'is_error': True | |
| } | |
| else: | |
| advice = st.text_area( | |
| '[advice feedback]', | |
| placeholder='่ฏท่พๅ ฅๆจ็ๅ้ฆ...' | |
| ) | |
| current_feedback = { | |
| 'instruct': instruct, | |
| 'input': current_input, | |
| 'model_output': st.session_state['model_out'], | |
| 'human_feedback': advice, | |
| 'is_error': False | |
| } | |
| submit_button = st.button('ๆไบค') | |
| if submit_button: | |
| with open(os.path.join(LOG_PATH, FEEDBACK_FILE), 'a', encoding='utf8') as f: | |
| f.write(f'{json.dumps(current_feedback, ensure_ascii=False)}\n') | |
| st.success('ๆ่ฐขๆจ็ๅ้ฆ~', icon="โ ") | |
| with c1: | |
| with st.expander(f'๐ป json ่งฃๆ็ปๆ', expanded=True): | |
| st.markdown(answer) | |
| json_res = re.findall(r'```json(.*)```', answer.replace('\n', '')) | |
| if len(json_res): | |
| json_res = json_res[0] | |
| try: | |
| json_res = json.loads(json_res) | |
| st.write(json_res) | |
| except: | |
| pass | |
| def read_dataset_file(): | |
| """ | |
| ่ฏปๅๆฌๅฐๆ ๆณจ็ๆฐๆฎ้ใ | |
| """ | |
| temp_dict = {} | |
| with open(os.path.join(DATASET_PATH, DATASET_FILE), 'r', encoding='utf8') as f: | |
| for line in f.readlines(): | |
| line = json.loads(line) | |
| for key, value in line.items(): | |
| if key not in temp_dict: | |
| temp_dict[key] = [] | |
| temp_dict[key].append(value) | |
| df = pd.DataFrame.from_dict(temp_dict) | |
| return df | |
| def start_label_page(): | |
| """ | |
| ๆฐๆฎ้ๆ ๆณจ้กต้ขใ | |
| """ | |
| c1, c2 = st.columns([4, 6]) | |
| with c1: | |
| with st.expander(f'๐ป ๆ ๆณจ็้ข', expanded=True): | |
| instruct = st.text_area( | |
| 'Human Instruct', | |
| value='ไฝ ็ฐๅจๆฏไธไธชๅพๅๅฎณ็้ ่ฏป็่งฃๅจ๏ผไธฅๆ ผๆ็ งไบบ็ฑปๆไปค่ฟ่กๅ็ญใ' | |
| ) | |
| inputs = st.text_area( | |
| 'Human Input', | |
| placeholder='่พๅ ฅไบบๅทฅๆ้ ้ฎ้ข๏ผไพๅฆ: ๅธฎๆๆฝๅไธ้ขๅฅๅญ็SPO๏ผ็จjsonๆ ผๅผ่ฟๅ...' | |
| ) | |
| answer = st.text_area( | |
| 'Human Output', | |
| placeholder='่พๅ ฅไบบๅทฅๆ้ ็ญๆก๏ผไพๅฆ:\n ๅฅฝ็๏ผไปฅไธๆฏๆฝๅSPO็jsonไฟกๆฏ๏ผ\n```json\n{\n"subject": "ๅญ็บข้ท", \n"predicate": "ๅนด้พ", \n"object": "52ๅฒ"\n}\n```', | |
| height=500 | |
| ) | |
| save_button = st.button('Save') | |
| if save_button: | |
| with open(os.path.join(DATASET_PATH, DATASET_FILE), 'a', encoding='utf8') as f: | |
| context = f'Instruction: {instruct}\nInput: {inputs}\nAnswer: ' | |
| current_sample = { | |
| 'context': context, | |
| 'target': answer | |
| } | |
| f.write(json.dumps(current_sample, ensure_ascii=False) + '\n') | |
| st.success('ๆฐๆฎๅทฒไฟๅญ๏ผ', icon="โ ") | |
| st.session_state['dataset_df'] = read_dataset_file() | |
| with c2: | |
| if 'dataset_df' not in st.session_state: | |
| st.session_state['dataset_df'] = read_dataset_file() | |
| with st.expander(f"๐ ๆฌๅฐๆฐๆฎ้๏ผๅ ฑ {len(st.session_state['dataset_df'])} ๆก๏ผ", expanded=True): | |
| st.dataframe(st.session_state['dataset_df'], height=820) | |
| refresh_button = st.button('ๅทๆฐๆฐๆฎ้') | |
| if refresh_button: | |
| st.session_state['dataset_df'] = read_dataset_file() | |
| def main(): | |
| """ | |
| ไธปๅฝๆฐๆต็จใ | |
| """ | |
| logo = '[%E3%83%8E)](https://github.com/HarderThenHarder/transformers_tasks)' | |
| st.markdown(logo) | |
| evaluate_page, label_page = st.tabs(['evaluate_page', 'label_page']) | |
| with evaluate_page: | |
| start_evaluate_page() | |
| with label_page: | |
| start_label_page() | |
| if __name__ == '__main__': | |
| main() | |