Spaces:
Sleeping
Sleeping
Deploy Gradio app with multiple files
Browse files- app.py +78 -0
- requirements.txt +3 -0
- utils.py +77 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import generate_website_code, preview_website
|
| 3 |
+
|
| 4 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Website Builder") as demo:
|
| 5 |
+
gr.Markdown("""
|
| 6 |
+
# 🤖 AI Website Builder
|
| 7 |
+
Create stunning websites using powerful Hugging Face models. Preview your code in real-time!
|
| 8 |
+
|
| 9 |
+
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 10 |
+
""")
|
| 11 |
+
|
| 12 |
+
with gr.Row():
|
| 13 |
+
with gr.Column(scale=1):
|
| 14 |
+
prompt = gr.Textbox(
|
| 15 |
+
label="Describe your website",
|
| 16 |
+
placeholder="E.g., 'A modern portfolio website for a photographer with a dark theme'",
|
| 17 |
+
lines=3
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
model_choice = gr.Dropdown(
|
| 21 |
+
choices=[
|
| 22 |
+
"OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
| 23 |
+
"Salesforce/codegen-350M-mono",
|
| 24 |
+
"Salesforce/codegen-2B-mono",
|
| 25 |
+
"bigcode/starcoder",
|
| 26 |
+
"meta-llama/Llama-2-70b-chat-hf",
|
| 27 |
+
"meta-llama/Llama-2-13b-chat-hf",
|
| 28 |
+
"mistralai/Mistral-7B-Instruct-v0.2",
|
| 29 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 30 |
+
"google/gemma-7b-it",
|
| 31 |
+
"microsoft/Phi-3-mini-4k-instruct"
|
| 32 |
+
],
|
| 33 |
+
value="Salesforce/codegen-2B-mono",
|
| 34 |
+
label="Choose AI Model"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
generate_btn = gr.Button("Generate Website", variant="primary")
|
| 38 |
+
|
| 39 |
+
with gr.Column(scale=2):
|
| 40 |
+
with gr.Tab("Code"):
|
| 41 |
+
code_output = gr.Code(
|
| 42 |
+
label="Generated HTML/CSS/JS",
|
| 43 |
+
language="html",
|
| 44 |
+
interactive=True,
|
| 45 |
+
lines=20
|
| 46 |
+
)
|
| 47 |
+
with gr.Tab("Preview"):
|
| 48 |
+
preview_output = gr.HTML(label="Website Preview")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column():
|
| 52 |
+
gr.Examples(
|
| 53 |
+
examples=[
|
| 54 |
+
["A simple landing page for a startup with a blue theme"],
|
| 55 |
+
["A personal blog with a clean, minimalist design"],
|
| 56 |
+
["An e-commerce product showcase page with cards"],
|
| 57 |
+
["A restaurant menu with images and pricing"],
|
| 58 |
+
],
|
| 59 |
+
inputs=[prompt]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
generate_btn.click(
|
| 63 |
+
generate_website_code,
|
| 64 |
+
inputs=[prompt, model_choice],
|
| 65 |
+
outputs=code_output
|
| 66 |
+
).then(
|
| 67 |
+
preview_website,
|
| 68 |
+
inputs=code_output,
|
| 69 |
+
outputs=preview_output
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
code_output.change(
|
| 73 |
+
preview_website,
|
| 74 |
+
inputs=code_output,
|
| 75 |
+
outputs=preview_output
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.29.0
|
| 2 |
+
huggingface-hub==0.20.3
|
| 3 |
+
requests==2.31.0
|
utils.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from huggingface_hub import InferenceApi
|
| 4 |
+
|
| 5 |
+
def generate_website_code(prompt, model_choice):
|
| 6 |
+
"""Generate website code using Hugging Face inference API"""
|
| 7 |
+
try:
|
| 8 |
+
# Add context to the prompt for better results
|
| 9 |
+
full_prompt = f"""
|
| 10 |
+
<!DOCTYPE html>
|
| 11 |
+
<html>
|
| 12 |
+
<head>
|
| 13 |
+
<title>Generated Website</title>
|
| 14 |
+
<style>
|
| 15 |
+
/* CSS styles */
|
| 16 |
+
</style>
|
| 17 |
+
</head>
|
| 18 |
+
<body>
|
| 19 |
+
<!-- HTML content -->
|
| 20 |
+
<script>
|
| 21 |
+
// JavaScript code
|
| 22 |
+
</script>
|
| 23 |
+
</body>
|
| 24 |
+
</html>
|
| 25 |
+
|
| 26 |
+
Create a complete, functional website based on this description: {prompt}
|
| 27 |
+
Include proper HTML5 structure, responsive CSS styling, and interactive JavaScript.
|
| 28 |
+
Respond only with the complete code.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
inference = InferenceApi(
|
| 32 |
+
repo_id=model_choice,
|
| 33 |
+
token=os.getenv("HF_API_TOKEN")
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
response = inference(
|
| 37 |
+
inputs=full_prompt,
|
| 38 |
+
parameters={
|
| 39 |
+
"max_new_tokens": 800,
|
| 40 |
+
"temperature": 0.7,
|
| 41 |
+
"top_p": 0.9,
|
| 42 |
+
"repetition_penalty": 1.2,
|
| 43 |
+
"do_sample": True
|
| 44 |
+
}
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if isinstance(response, list) and len(response) > 0:
|
| 48 |
+
generated_text = response[0].get("generated_text", "")
|
| 49 |
+
# Extract HTML code from response
|
| 50 |
+
start_html = generated_text.find("<!DOCTYPE html>")
|
| 51 |
+
if start_html != -1:
|
| 52 |
+
return generated_text[start_html:]
|
| 53 |
+
else:
|
| 54 |
+
return generated_text
|
| 55 |
+
else:
|
| 56 |
+
return f"Error: Unexpected response format\n{str(response)}"
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error generating code: {str(e)}"
|
| 60 |
+
|
| 61 |
+
def preview_website(code):
|
| 62 |
+
"""Preview website from HTML code"""
|
| 63 |
+
if not code or code.strip() == "":
|
| 64 |
+
return "<div style='padding: 20px; text-align: center; font-family: sans-serif;'>Enter code to preview website</div>"
|
| 65 |
+
|
| 66 |
+
# Sanitize and wrap code in iframe for preview
|
| 67 |
+
sanitized_code = code.replace('"', '"').replace("'", "'")
|
| 68 |
+
iframe_html = f"""
|
| 69 |
+
<div style="border: 1px solid #ddd; border-radius: 4px; overflow: hidden; height: 600px;">
|
| 70 |
+
<iframe
|
| 71 |
+
srcdoc="{sanitized_code}"
|
| 72 |
+
style="width: 100%; height: 100%; border: none;"
|
| 73 |
+
sandbox="allow-scripts allow-same-origin">
|
| 74 |
+
</iframe>
|
| 75 |
+
</div>
|
| 76 |
+
"""
|
| 77 |
+
return iframe_html
|