DMPark commited on
Commit
83808ad
·
1 Parent(s): bf0cbc4
albatross_classifier_showcase.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import cuda
3
+
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ from peft import PeftModel, PeftConfig
6
+
7
+ import streamlit as st
8
+ from streamlit_chat import message
9
+ from huggingface_hub import login
10
+
11
+ login(token='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')
12
+ device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
13
+
14
+ peft_model_id = "DMPark/albatross_classifier"
15
+ config = PeftConfig.from_pretrained(peft_model_id)
16
+ # model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=True, device_map='auto')
17
+ model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
18
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
19
+
20
+ model = PeftModel.from_pretrained(model, peft_model_id)
21
+
22
+ def evaluate_model(model, tokenizer, device, instruction, input=None, temperature=0.1, top_p=0.75, top_k=40, num_beams=4, max_new_tokens=128):
23
+ prompt_instruction = "### Instruction:\n{}\n" + (f"### Input:\n{input}\n" if input else "") + "### Response:\n"
24
+ prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n" + prompt_instruction.format(instruction)
25
+
26
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
27
+ with torch.no_grad():
28
+ generation_output = model.generate(
29
+ input_ids=inputs["input_ids"],
30
+ temperature=temperature,
31
+ top_p=top_p,
32
+ top_k=top_k,
33
+ num_beams=num_beams,
34
+ max_length=max_new_tokens + inputs["input_ids"].shape[1]
35
+ )
36
+
37
+ output = tokenizer.decode(generation_output[0])
38
+ return output.split("### Response:")[1].strip()
39
+
40
+ st.header("🦜Albatross")
41
+
42
+ if 'generated' not in st.session_state:
43
+ st.session_state['generated'] = []
44
+
45
+ if 'past' not in st.session_state:
46
+ st.session_state['past'] = []
47
+
48
+
49
+ with st.form('form', clear_on_submit=True):
50
+ user_input = st.text_area('You: ', '')
51
+ submitted = st.form_submit_button('Send')
52
+
53
+ if submitted and user_input:
54
+ output = evaluate_model(model, tokenizer, device, instruction="Predict FOMC' stance toward Interest Rate Dicision (Hawkish/Neutral/Dovish)", input=fomc)
55
+ st.session_state.past.append(user_input)
56
+ st.session_state.generated.append(output)
57
+
58
+ if 'generated' in st.session_state:
59
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
60
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
61
+ message(st.session_state["generated"][i], key=str(i))
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ peft
4
+ huggingface_hub
5
+ streamlit-chat
6
+ streamlit