Spaces:
Runtime error
Runtime error
Commit
·
8b21c01
1
Parent(s):
6dd704e
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from peft import PeftModel, PeftConfig
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
peft_model_id = f"FourthBrainGenAI/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 |
+
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 |
+
|
| 19 |
+
def make_inference(product_name, product_description):
|
| 20 |
+
batch = tokenizer(
|
| 21 |
+
f"### Product and Description:\n{product_name}: {product_description}\n\n### Ad:",
|
| 22 |
+
return_tensors="pt",
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
with torch.cuda.amp.autocast():
|
| 26 |
+
output_tokens = model.generate(**batch, max_new_tokens=50)
|
| 27 |
+
|
| 28 |
+
return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
# make a gradio interface
|
| 33 |
+
import gradio as gr
|
| 34 |
+
|
| 35 |
+
gr.Interface(
|
| 36 |
+
make_inference,
|
| 37 |
+
[
|
| 38 |
+
gr.inputs.Textbox(lines=2, label="Product Name"),
|
| 39 |
+
gr.inputs.Textbox(lines=5, label="Product Description"),
|
| 40 |
+
],
|
| 41 |
+
gr.outputs.Textbox(label="Ad"),
|
| 42 |
+
title="GenerAd-AI",
|
| 43 |
+
description="GenerAd-AI is a generative model that generates ads for products.",
|
| 44 |
+
).launch()
|