rdiazconcha commited on
Commit
e81e342
1 Parent(s): 449f15c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ peft_model_id = f"rdiazconcha/GenerAd-AI"
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
+ )
12
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
13
+
14
+ # Load the Lora model
15
+ model = PeftModel.from_pretrained(model, peft_model_id)
16
+
17
+
18
+ def make_inference(product_name, product_description):
19
+ batch = tokenizer(
20
+ f"### Product and Description:\n{product_name}: {product_description}\n\n### Ad:",
21
+ return_tensors="pt",
22
+ )
23
+
24
+ with torch.cuda.amp.autocast():
25
+ output_tokens = model.generate(**batch, max_new_tokens=50)
26
+
27
+ return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
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="Product Item"),
38
+ gr.inputs.Textbox(lines=5, label="Product Description"),
39
+ ],
40
+ gr.outputs.Textbox(label="Ad"),
41
+ title="GenerAd-AI",
42
+ description="GenerAd-AI is a generative model that generates ads for products.",
43
+ ).launch()