Spaces:
Runtime error
Runtime error
Commit ·
3f7b91f
0
Parent(s):
Update to load fine-tuned model from Hugging Face Hub
Browse files- app.py +50 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
|
| 4 |
+
# Load the fine-tuned model and tokenizer
|
| 5 |
+
model_name = "./fine-tuned-distilgpt2" # Path to the saved fine-tuned model
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def generate_copy(book_title, book_author, book_genre, book_themes, book_description):
|
| 11 |
+
prompt = f"""You are a marketing copywriter for an online bookstore.
|
| 12 |
+
Given the following book details, write three compelling landing page headlines and a short product description:
|
| 13 |
+
|
| 14 |
+
TITLE: {book_title}
|
| 15 |
+
AUTHOR: {book_author}
|
| 16 |
+
GENRE: {book_genre}
|
| 17 |
+
KEY THEMES: {book_themes}
|
| 18 |
+
DESCRIPTION: {book_description}
|
| 19 |
+
|
| 20 |
+
Landing Page Copy:
|
| 21 |
+
"""
|
| 22 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
| 23 |
+
output = model.generate(
|
| 24 |
+
inputs,
|
| 25 |
+
max_length=200,
|
| 26 |
+
temperature=0.7,
|
| 27 |
+
top_p=0.9,
|
| 28 |
+
do_sample=True
|
| 29 |
+
)
|
| 30 |
+
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 31 |
+
final_output = result.split("Landing Page Copy:")[-1].strip()
|
| 32 |
+
return final_output
|
| 33 |
+
|
| 34 |
+
# Gradio UI
|
| 35 |
+
title_input = gr.Textbox(label="Book Title")
|
| 36 |
+
description_input = gr.Textbox(label="Book Description")
|
| 37 |
+
author_input = gr.Textbox(label="Author")
|
| 38 |
+
genre_input = gr.Textbox(label="Genre")
|
| 39 |
+
themes_input = gr.Textbox(label="Key Themes")
|
| 40 |
+
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=generate_copy,
|
| 43 |
+
inputs=[title_input, author_input, genre_input, themes_input, description_input],
|
| 44 |
+
outputs="text",
|
| 45 |
+
title="Dynamic Landing Page Copy Generator",
|
| 46 |
+
description="Enter book details and get compelling marketing copy."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
datasets
|