drietsch's picture
Update app.py
43a042b verified
raw
history blame
3.07 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import onnxruntime as ort
# Load the Phi-3.5-mini-instruct model and tokenizer
model_name = "microsoft/Phi-3.5-mini-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Load the ONNX model
session = ort.InferenceSession(f"{model_name}/model.onnx")
# 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):
# Create a prompt for the model
prompt = f"Generate personalized website content for the following persona: {persona_text}. Provide a title and main content."
# Tokenize the prompt
inputs = tokenizer(prompt, return_tensors="np")
# Run the ONNX model
ort_inputs = {session.get_inputs()[0].name: inputs["input_ids"]}
ort_outs = session.run(None, ort_inputs)
# Decode the output
generated_text = tokenizer.decode(ort_outs[0][0], skip_special_tokens=True)
# Split the response into a title and content
title, content = generated_text.split('\n', 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.strip(),
content=content.strip()
)
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()