Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
peft_model_id = f"Bsbell21/GenerAd-AI-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 |
+
)
|
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 |
+
batch = {key: value.to('cuda:0') for key, value in batch.items()}
|
25 |
+
|
26 |
+
with torch.cuda.amp.autocast():
|
27 |
+
output_tokens = model.generate(**batch, max_new_tokens=50)
|
28 |
+
|
29 |
+
return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
30 |
+
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
# make a gradio interface
|
34 |
+
import gradio as gr
|
35 |
+
|
36 |
+
gr.Interface(
|
37 |
+
make_inference,
|
38 |
+
[
|
39 |
+
gr.Textbox(lines=2, label="Product Name"),
|
40 |
+
gr.Textbox(lines=5, label="Product Description"),
|
41 |
+
],
|
42 |
+
gr.Textbox(label="Ad"),
|
43 |
+
title="GenerAd-AI",
|
44 |
+
description="GenerAd-AI is a generative model that generates ads for products.",
|
45 |
+
).launch()
|