File size: 3,619 Bytes
7ec874f
5446d2d
 
7ec874f
5446d2d
 
 
43775ca
5446d2d
43775ca
5446d2d
 
43775ca
 
 
5446d2d
43775ca
43a042b
5446d2d
 
 
 
 
 
 
 
 
 
 
 
 
 
7ec874f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5446d2d
 
 
 
 
7ec874f
5446d2d
 
 
 
 
 
 
 
43a042b
7ec874f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5446d2d
 
7ec874f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch

# Set the random seed for reproducibility
torch.random.manual_seed(0)

# Load the model and tokenizer with pinned revision
model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Phi-3.5-mini-instruct", 
    device_map="auto", 
    torch_dtype="auto", 
    trust_remote_code=True,
    revision="main",  # Pin the revision for stability
    attn_implementation="eager"  # Use eager attention implementation if flash-attention is not installed
)
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-mini-instruct")

# Set up the pipeline
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
)

# Define the generation arguments
generation_args = {
    "max_new_tokens": 150,
    "return_full_text": False,
    "temperature": 0.7,
    "do_sample": False,
}

# Simple HTML template for the website
simple_website_template = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Personalized Website</title>
    <style>
        body {{
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            color: #333;
            padding: 20px;
        }}
        h1 {{
            color: {title_color};
        }}
        p {{
            font-size: {font_size}px;
        }}
    </style>
</head>
<body>
    <h1>{title}</h1>
    <p>{content}</p>
</body>
</html>
"""

# Function to generate personalized content using Phi-3.5-mini-instruct
def personalize_website_llm(persona_text):
    # Construct the conversation history
    messages = [
        {"role": "system", "content": "You are a helpful AI assistant that personalizes content for websites."},
        {"role": "user", "content": f"Persona: {persona_text}. Generate a personalized website content including a title and a paragraph."},
    ]
    
    # Generate content using the pipeline
    output = pipe(messages, **generation_args)
    generated_text = output[0]['generated_text'].strip()

    # Simple heuristic to split title and content
    lines = generated_text.split('\n')
    title = lines[0]
    content = "\n".join(lines[1:])

    # Set the title color and font size based on simple heuristics
    title_color = "#333"
    font_size = 16

    if "young" in persona_text.lower():
        title_color = "#ff5733"
        font_size = 18

    if "professional" in persona_text.lower():
        title_color = "#1c1c1c"
        font_size = 14

    # Create the personalized website HTML
    personalized_website = simple_website_template.format(
        title_color=title_color,
        font_size=font_size,
        title=title,
        content=content
    )
    
    return personalized_website

# Create the Gradio interface
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            gr.HTML('<h3>Original Simple Website</h3>')
            gr.HTML(simple_website_template.format(title_color="#333", font_size=16, title="Welcome to Our Website!", content="We are glad to have you here."))
        
        with gr.Column():
            persona_input = gr.Textbox(label="Define Persona", placeholder="Describe the persona here...")
            generate_button = gr.Button("Generate Personalized Website")
        
        with gr.Column():
            personalized_output = gr.HTML(label="Personalized Website Output")
    
    generate_button.click(personalize_website_llm, inputs=persona_input, outputs=personalized_output)

# Launch the app
demo.launch()