Chris Alexiuk commited on
Commit
b9c4c97
·
1 Parent(s): fa75564

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ peft_model_id = f"c-s-ale/AI-Superstar-Model"
6
+ config = PeftConfig.from_pretrained(peft_model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(
8
+ config.base_model_name_or_path,
9
+ return_dict=True,
10
+ load_in_8bit=True,
11
+ device_map="auto",
12
+ )
13
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
14
+
15
+ # Load the Lora model
16
+ model = PeftModel.from_pretrained(model, peft_model_id)
17
+
18
+ def make_inference(product, description):
19
+ batch = tokenizer(
20
+ f"Please answer the following question to the best of your ability.\n\n### Question:\n{question}\n### Answer:\n",
21
+ return_tensors="pt",
22
+ )
23
+
24
+ with torch.cuda.amp.autocast():
25
+ output_tokens = model.generate(**batch, max_new_tokens=70)
26
+
27
+ return tokenizer.decode(output_tokens[0], skip_special_tokens=True).split("\n\n")[-1]
28
+
29
+
30
+ if __name__ == "__main__":
31
+ # make a gradio interface
32
+ import gradio as gr
33
+
34
+ gr.Interface(
35
+ make_inference,
36
+ [
37
+ gr.inputs.Textbox(lines=2, label="Question"),
38
+ ],
39
+ gr.outputs.Textbox(label="Answer"),
40
+ title="AI Super Star",
41
+ description="AI Super Star is a tool you can use to guide you on your ML journey.",
42
+ ).launch()