Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
generator = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def generate_marketing_text(prompt, content_type,
|
| 8 |
"""
|
| 9 |
-
Generate marketing text based on
|
| 10 |
"""
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Generate text
|
| 19 |
result = generator(
|
| 20 |
-
enhanced_prompt,
|
| 21 |
-
max_length=max_length,
|
| 22 |
-
temperature=temperature,
|
| 23 |
do_sample=True,
|
| 24 |
pad_token_id=50256
|
| 25 |
)
|
| 26 |
-
|
| 27 |
-
# Return the generated text
|
| 28 |
return result[0]['generated_text']
|
| 29 |
|
| 30 |
-
#
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=generate_marketing_text,
|
| 33 |
inputs=[
|
| 34 |
-
gr.Textbox(lines=3, placeholder="Enter your topic here...", label="Topic"),
|
| 35 |
gr.Radio(
|
| 36 |
["social media post", "email newsletter", "product description", "ad copy"],
|
| 37 |
label="Content Type",
|
| 38 |
value="social media post"
|
| 39 |
),
|
| 40 |
-
gr.Slider(minimum=50, maximum=
|
| 41 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Creativity (Temperature)")
|
| 42 |
],
|
| 43 |
outputs=gr.Textbox(lines=10, label="Generated Marketing Content"),
|
| 44 |
title="AdGenAI - Marketing Content Generator",
|
| 45 |
-
description="Enter a topic and select content type to generate marketing content using
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the powerful open-source model
|
| 5 |
+
generator = pipeline(
|
| 6 |
+
"text-generation",
|
| 7 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
| 8 |
+
device_map="auto" # If running locally with GPU; otherwise remove
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Define max length recommendations
|
| 12 |
+
max_lengths = {
|
| 13 |
+
"social media post": 280,
|
| 14 |
+
"email newsletter": 800,
|
| 15 |
+
"product description": 600,
|
| 16 |
+
"ad copy": 400
|
| 17 |
+
}
|
| 18 |
|
| 19 |
+
def generate_marketing_text(prompt, content_type, _, temperature=0.7):
|
| 20 |
"""
|
| 21 |
+
Generate marketing text using Mistral 7B based on topic and content type.
|
| 22 |
"""
|
| 23 |
+
# Clean and enhance prompt
|
| 24 |
+
enhanced_prompt = f"Write a {content_type} for this product: {prompt}\nMake it engaging, professional, and persuasive."
|
| 25 |
+
|
| 26 |
+
# Determine proper max_length for the type
|
| 27 |
+
max_length = max_lengths.get(content_type, 400)
|
| 28 |
+
|
| 29 |
+
# Generate the text
|
|
|
|
| 30 |
result = generator(
|
| 31 |
+
enhanced_prompt,
|
| 32 |
+
max_length=max_length,
|
| 33 |
+
temperature=temperature,
|
| 34 |
do_sample=True,
|
| 35 |
pad_token_id=50256
|
| 36 |
)
|
| 37 |
+
|
|
|
|
| 38 |
return result[0]['generated_text']
|
| 39 |
|
| 40 |
+
# Gradio UI
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=generate_marketing_text,
|
| 43 |
inputs=[
|
| 44 |
+
gr.Textbox(lines=3, placeholder="Enter your product or topic here...", label="Topic"),
|
| 45 |
gr.Radio(
|
| 46 |
["social media post", "email newsletter", "product description", "ad copy"],
|
| 47 |
label="Content Type",
|
| 48 |
value="social media post"
|
| 49 |
),
|
| 50 |
+
gr.Slider(minimum=50, maximum=800, value=280, step=10, label="(Disabled) Max Length", interactive=False),
|
| 51 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Creativity (Temperature)")
|
| 52 |
],
|
| 53 |
outputs=gr.Textbox(lines=10, label="Generated Marketing Content"),
|
| 54 |
title="AdGenAI - Marketing Content Generator",
|
| 55 |
+
description="Enter a topic and select a content type to generate persuasive, high-quality marketing content using Mistral 7B."
|
| 56 |
)
|
| 57 |
|
| 58 |
+
demo.launch()
|
|
|