drietsch commited on
Commit
7ec874f
·
verified ·
1 Parent(s): 626b0aa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load the Phi-3.5-mini-instruct model and tokenizer
6
+ model_name = "phi-3.5-mini-instruct"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ # Simple HTML template for the website
11
+ simple_website_template = """
12
+ <!DOCTYPE html>
13
+ <html lang="en">
14
+ <head>
15
+ <meta charset="UTF-8">
16
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
17
+ <title>Personalized Website</title>
18
+ <style>
19
+ body {{
20
+ font-family: Arial, sans-serif;
21
+ background-color: #f4f4f4;
22
+ color: #333;
23
+ padding: 20px;
24
+ }}
25
+ h1 {{
26
+ color: {title_color};
27
+ }}
28
+ p {{
29
+ font-size: {font_size}px;
30
+ }}
31
+ </style>
32
+ </head>
33
+ <body>
34
+ <h1>{title}</h1>
35
+ <p>{content}</p>
36
+ </body>
37
+ </html>
38
+ """
39
+
40
+ # Function to generate personalized content using Phi-3.5-mini-instruct
41
+ def personalize_website_llm(persona_text):
42
+ # Create a prompt for the model
43
+ prompt = f"Generate personalized website content for the following persona: {persona_text}. Provide a title and main content."
44
+
45
+ # Tokenize and generate output
46
+ inputs = tokenizer(prompt, return_tensors="pt")
47
+ outputs = model.generate(inputs.input_ids, max_length=150)
48
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
49
+
50
+ # Split the response into a title and content
51
+ title, content = generated_text.split('\n', 1)
52
+
53
+ # Set the title color and font size based on simple heuristics
54
+ title_color = "#333"
55
+ font_size = 16
56
+
57
+ if "young" in persona_text.lower():
58
+ title_color = "#ff5733"
59
+ font_size = 18
60
+
61
+ if "professional" in persona_text.lower():
62
+ title_color = "#1c1c1c"
63
+ font_size = 14
64
+
65
+ # Create the personalized website HTML
66
+ personalized_website = simple_website_template.format(
67
+ title_color=title_color,
68
+ font_size=font_size,
69
+ title=title.strip(),
70
+ content=content.strip()
71
+ )
72
+
73
+ return personalized_website
74
+
75
+ # Create the Gradio interface
76
+ with gr.Blocks() as demo:
77
+ with gr.Row():
78
+ with gr.Column():
79
+ gr.HTML('<h3>Original Simple Website</h3>')
80
+ 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."))
81
+
82
+ with gr.Column():
83
+ persona_input = gr.Textbox(label="Define Persona", placeholder="Describe the persona here...")
84
+ generate_button = gr.Button("Generate Personalized Website")
85
+
86
+ with gr.Column():
87
+ personalized_output = gr.HTML(label="Personalized Website Output")
88
+
89
+ generate_button.click(personalize_website_llm, inputs=persona_input, outputs=personalized_output)
90
+
91
+ # Launch the app
92
+ demo.launch()