asusevski commited on
Commit
ab95205
·
1 Parent(s): 4e9520d

+app.py v1, +reqs

Browse files
Files changed (2) hide show
  1. app.py +74 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+
6
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
+
8
+
9
+ base_model_id = "mistralai/Mistral-7B-v0.1"
10
+ ft_model_id = "asusevski/mistraloo-sft"
11
+
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained(
14
+ base_model_id,
15
+ add_bos_token=True,
16
+ )
17
+
18
+ model = AutoModelForCausalLM.from_pretrained(ft_model_id).to(device)
19
+ model.eval()
20
+
21
+
22
+ def uwaterloo_output(post_title, post_text):
23
+ prompt = f"""
24
+ Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
25
+
26
+ ### Instruction:
27
+ Respond to the reddit post in the style of a University of Waterloo student.
28
+
29
+ ### Input:
30
+ {post_title}
31
+ {post_text}
32
+
33
+ ### Response:
34
+ """
35
+ model_input = tokenizer(prompt, return_tensors="pt").to(device)
36
+ with torch.no_grad():
37
+ model_output = model.generate(**model_input, max_new_tokens=256, repetition_penalty=1.15)[0]
38
+ output = tokenizer.decode(model_output, skip_special_tokens=True)
39
+ return output.split('### Response:\n')[-1]
40
+
41
+
42
+ iface = gr.Interface(
43
+ fn=uwaterloo_output,
44
+ inputs=[
45
+ gr.Textbox("", label="Post Title"),
46
+ gr.Textbox("", label="Post Text"),
47
+ ],
48
+ outputs=gr.Textbox("", label="Mistraloo-SFT")
49
+ )
50
+
51
+ iface.launch()
52
+
53
+
54
+
55
+
56
+ # base_model_id = "mistralai/Mistral-7B-v0.1"
57
+ # bnb_config = BitsAndBytesConfig(
58
+ # load_in_4bit=True,
59
+ # bnb_4bit_use_double_quant=True,
60
+ # bnb_4bit_quant_type="nf4",
61
+ # bnb_4bit_compute_dtype=torch.bfloat16
62
+ # )
63
+
64
+
65
+ # base_model = AutoModelForCausalLM.from_pretrained(
66
+ # base_model_id, # Mistral, same as before
67
+ # quantization_config=bnb_config, # Same quantization config as before
68
+ # device_map="auto",
69
+ # trust_remote_code=True,
70
+ # use_auth_token=True
71
+ # )
72
+
73
+
74
+ # ft_model = PeftModel.from_pretrained(base_model, "mistral-mistraloo/checkpoint-500")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ gradio
3
+ torch
4
+ s