Bsbell21 commited on
Commit
f470724
·
verified ·
1 Parent(s): 37ab18c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ peft_model_id = f"Bsbell21/MarketMail-Bloomz"
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
+ device_map="auto",
11
+ load_in_8bit=False
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(f"### INSTRUCTION\nBelow is a product and description, please write a marketing email for this product.\n\n### Product:\n{product}\n### Description:\n{description}\n\n### Marketing Email:\n", return_tensors='pt')
20
+
21
+ with torch.cuda.amp.autocast():
22
+ output_tokens = model.generate(**batch, max_new_tokens=200)
23
+ return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
24
+ '''
25
+ def make_inference(product_name, product_description):
26
+ batch = tokenizer(
27
+ f"### Product and Description:\n{product_name}: {product_description}\n\n### Ad:",
28
+ return_tensors="pt",
29
+ )
30
+
31
+ batch = {key: value.to('cuda:0') for key, value in batch.items()}
32
+
33
+ with torch.cuda.amp.autocast():
34
+ output_tokens = model.generate(**batch, max_new_tokens=50)
35
+
36
+ return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
37
+
38
+ '''
39
+
40
+ # Load the Lora model
41
+ model = PeftModel.from_pretrained(model, peft_model_id)
42
+ if __name__ == "__main__":
43
+ # make a gradio interface
44
+ import gradio as gr
45
+
46
+ gr.Interface(
47
+ make_inference,
48
+ [
49
+ gr.Textbox(lines=2, label="Product Name"),
50
+ gr.Textbox(lines=5, label="Product Description"),
51
+ ],
52
+ gr.Textbox(label="Email Ad"),
53
+ title="MarketMail-AI",
54
+ description="MarketMail-AI is a generative model that generates email ads for products.",
55
+ ).launch()