Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
peft_model_id = "Bsbell21/MarketMailAI"
|
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 |
+
mixtral_tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
|
15 |
+
|
16 |
+
# Load the Lora model
|
17 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
18 |
+
|
19 |
+
def input_from_text(product, description):
|
20 |
+
return f"<s>[INST]Below 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:[/INST]"
|
21 |
+
|
22 |
+
def make_inference(product, description):
|
23 |
+
inputs = mixtral_tokenizer(input_from_text(product, description), return_tensors="pt")
|
24 |
+
|
25 |
+
outputs = merged_model.generate(
|
26 |
+
**inputs,
|
27 |
+
max_new_tokens=150,
|
28 |
+
generation_kwargs={"repetition_penalty" : 1.7}
|
29 |
+
)
|
30 |
+
# print(mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True))
|
31 |
+
result = mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[1]
|
32 |
+
return result
|
33 |
+
'''
|
34 |
+
def make_inference(product_name, product_description):
|
35 |
+
batch = tokenizer(
|
36 |
+
f"### Product and Description:\n{product_name}: {product_description}\n\n### Ad:",
|
37 |
+
return_tensors="pt",
|
38 |
+
)
|
39 |
+
|
40 |
+
batch = {key: value.to('cuda:0') for key, value in batch.items()}
|
41 |
+
with torch.cuda.amp.autocast():
|
42 |
+
output_tokens = model.generate(**batch, max_new_tokens=50)
|
43 |
+
return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
44 |
+
'''
|
45 |
+
if __name__ == "__main__":
|
46 |
+
# make a gradio interface
|
47 |
+
import gradio as gr
|
48 |
+
|
49 |
+
gr.Interface(
|
50 |
+
make_inference,
|
51 |
+
[
|
52 |
+
gr.Textbox(lines=2, label="Product Name"),
|
53 |
+
gr.Textbox(lines=5, label="Product Description"),
|
54 |
+
],
|
55 |
+
gr.Textbox(label="Ad"),
|
56 |
+
title="GenerAd-AI",
|
57 |
+
description="GenerAd-AI is a generative model that generates ads for products.",
|
58 |
+
).launch()
|