Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,23 +3,23 @@ import gradio as gr
|
|
| 3 |
from generator import generate_app, enhance_app
|
| 4 |
from helpers import save_as_zip
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
def extract_react_preview(code: str) -> str:
|
| 8 |
-
# Write preview.html with basic React support
|
| 9 |
react_template = f"""
|
| 10 |
<!DOCTYPE html>
|
| 11 |
-
<html lang
|
| 12 |
<head>
|
| 13 |
-
<meta charset
|
| 14 |
-
<meta name
|
| 15 |
<title>React Preview</title>
|
| 16 |
-
<script crossorigin src
|
| 17 |
-
<script crossorigin src
|
| 18 |
-
<script src
|
| 19 |
</head>
|
| 20 |
<body>
|
| 21 |
-
<div id
|
| 22 |
-
<script type
|
| 23 |
{code}
|
| 24 |
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
|
| 25 |
</script>
|
|
@@ -31,25 +31,33 @@ def extract_react_preview(code: str) -> str:
|
|
| 31 |
f.write(react_template)
|
| 32 |
return filename
|
| 33 |
|
| 34 |
-
def run_app(user_input, enhance_prompt, action, state):
|
|
|
|
|
|
|
| 35 |
if not state:
|
|
|
|
| 36 |
code_bundle = generate_app(user_input)
|
| 37 |
state = {"bundle": code_bundle, "history": []}
|
| 38 |
else:
|
| 39 |
code_bundle = state["bundle"]
|
| 40 |
|
| 41 |
if action == "Enhance" and enhance_prompt:
|
|
|
|
| 42 |
enhanced = enhance_app(code_bundle, enhance_prompt)
|
| 43 |
state["bundle"] = enhanced
|
| 44 |
state["history"].append(enhance_prompt)
|
| 45 |
code_bundle = enhanced
|
| 46 |
|
|
|
|
| 47 |
zip_bytes = save_as_zip(code_bundle)
|
| 48 |
with open("generated_app.zip", "wb") as f:
|
| 49 |
f.write(zip_bytes)
|
| 50 |
|
|
|
|
| 51 |
preview_path = extract_react_preview(code_bundle["full_code"])
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
with gr.Blocks(title="Auto App Builder") as iface:
|
| 55 |
gr.Markdown("# ⚡ Auto App Builder (Gemini Powered)\n_Powered by Google Gemini API_")
|
|
@@ -64,13 +72,37 @@ with gr.Blocks(title="Auto App Builder") as iface:
|
|
| 64 |
enh = gr.Textbox(label="Enhancement Prompt", placeholder="e.g. Add login, PDF export, dark mode...", lines=1)
|
| 65 |
btn_enh = gr.Button("✨ Enhance")
|
| 66 |
zip_file = gr.File(label="⬇️ Download App ZIP", interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Column():
|
| 68 |
gr.Markdown("### 🔍 Live React Preview")
|
| 69 |
preview = gr.HTML(label="React App Preview")
|
| 70 |
|
| 71 |
state = gr.State()
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
iface.launch()
|
|
|
|
| 3 |
from generator import generate_app, enhance_app
|
| 4 |
from helpers import save_as_zip
|
| 5 |
import uuid
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
def extract_react_preview(code: str) -> str:
|
|
|
|
| 9 |
react_template = f"""
|
| 10 |
<!DOCTYPE html>
|
| 11 |
+
<html lang=\"en\">
|
| 12 |
<head>
|
| 13 |
+
<meta charset=\"UTF-8\" />
|
| 14 |
+
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
|
| 15 |
<title>React Preview</title>
|
| 16 |
+
<script crossorigin src=\"https://unpkg.com/react@18/umd/react.development.js\"></script>
|
| 17 |
+
<script crossorigin src=\"https://unpkg.com/react-dom@18/umd/react-dom.development.js\"></script>
|
| 18 |
+
<script src=\"https://unpkg.com/@babel/standalone/babel.min.js\"></script>
|
| 19 |
</head>
|
| 20 |
<body>
|
| 21 |
+
<div id=\"root\"></div>
|
| 22 |
+
<script type=\"text/babel\">
|
| 23 |
{code}
|
| 24 |
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
|
| 25 |
</script>
|
|
|
|
| 31 |
f.write(react_template)
|
| 32 |
return filename
|
| 33 |
|
| 34 |
+
def run_app(user_input, enhance_prompt, action, state, progress=gr.Progress()):
|
| 35 |
+
progress(0, desc="Starting Gemini generation...")
|
| 36 |
+
|
| 37 |
if not state:
|
| 38 |
+
progress(0.1, desc="Calling Gemini API...")
|
| 39 |
code_bundle = generate_app(user_input)
|
| 40 |
state = {"bundle": code_bundle, "history": []}
|
| 41 |
else:
|
| 42 |
code_bundle = state["bundle"]
|
| 43 |
|
| 44 |
if action == "Enhance" and enhance_prompt:
|
| 45 |
+
progress(0.5, desc="Enhancing with Gemini...")
|
| 46 |
enhanced = enhance_app(code_bundle, enhance_prompt)
|
| 47 |
state["bundle"] = enhanced
|
| 48 |
state["history"].append(enhance_prompt)
|
| 49 |
code_bundle = enhanced
|
| 50 |
|
| 51 |
+
progress(0.7, desc="Creating zip archive...")
|
| 52 |
zip_bytes = save_as_zip(code_bundle)
|
| 53 |
with open("generated_app.zip", "wb") as f:
|
| 54 |
f.write(zip_bytes)
|
| 55 |
|
| 56 |
+
progress(0.9, desc="Generating preview...")
|
| 57 |
preview_path = extract_react_preview(code_bundle["full_code"])
|
| 58 |
+
|
| 59 |
+
progress(1.0, desc="Done!")
|
| 60 |
+
return code_bundle["full_code"], "generated_app.zip", preview_path, code_bundle.get("files", {}), state
|
| 61 |
|
| 62 |
with gr.Blocks(title="Auto App Builder") as iface:
|
| 63 |
gr.Markdown("# ⚡ Auto App Builder (Gemini Powered)\n_Powered by Google Gemini API_")
|
|
|
|
| 72 |
enh = gr.Textbox(label="Enhancement Prompt", placeholder="e.g. Add login, PDF export, dark mode...", lines=1)
|
| 73 |
btn_enh = gr.Button("✨ Enhance")
|
| 74 |
zip_file = gr.File(label="⬇️ Download App ZIP", interactive=True)
|
| 75 |
+
|
| 76 |
+
gr.Markdown("### 📁 File Explorer")
|
| 77 |
+
file_selector = gr.Dropdown(choices=[], label="Select File")
|
| 78 |
+
file_viewer = gr.Code(label="📂 File Content")
|
| 79 |
with gr.Column():
|
| 80 |
gr.Markdown("### 🔍 Live React Preview")
|
| 81 |
preview = gr.HTML(label="React App Preview")
|
| 82 |
|
| 83 |
state = gr.State()
|
| 84 |
|
| 85 |
+
def update_file_tree(code, zip_name, preview_path, files, state):
|
| 86 |
+
file_list = list(files.keys()) if files else []
|
| 87 |
+
if file_list:
|
| 88 |
+
return gr.update(choices=file_list, value=file_list[0]), files[file_list[0]]
|
| 89 |
+
return gr.update(choices=[]), ""
|
| 90 |
+
|
| 91 |
+
btn.click(
|
| 92 |
+
fn=run_app,
|
| 93 |
+
inputs=[inp, enh, gr.State(""), state],
|
| 94 |
+
outputs=[code_out, zip_file, preview, file_selector, state]
|
| 95 |
+
)
|
| 96 |
+
btn_enh.click(
|
| 97 |
+
fn=run_app,
|
| 98 |
+
inputs=[inp, enh, gr.State("Enhance"), state],
|
| 99 |
+
outputs=[code_out, zip_file, preview, file_selector, state]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
file_selector.change(
|
| 103 |
+
lambda name, state: state["bundle"]["files"].get(name, ""),
|
| 104 |
+
inputs=[file_selector, state],
|
| 105 |
+
outputs=file_viewer
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
iface.launch()
|