Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,32 +30,40 @@ def extract_react_preview(code: str) -> str:
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
def run_app(user_input, enhance_prompt, action, state, progress=gr.Progress()):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
with open("generated_app.zip", "wb") as f:
|
| 52 |
f.write(zip_bytes)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
progress(1.0, desc="Done!")
|
| 58 |
-
return code_bundle.get("full_code", ""), "generated_app.zip", preview_html, code_bundle.get("files", {}), state
|
| 59 |
|
| 60 |
with gr.Blocks(title="Auto App Builder") as iface:
|
| 61 |
gr.Markdown("# ⚡ Auto App Builder (Gemini Powered)\n_Powered by Google Gemini API_")
|
|
@@ -98,16 +106,6 @@ with gr.Blocks(title="Auto App Builder") as iface:
|
|
| 98 |
outputs=[file_selector, file_viewer]
|
| 99 |
)
|
| 100 |
|
| 101 |
-
btn_enh.click(
|
| 102 |
-
fn=run_app,
|
| 103 |
-
inputs=[inp, enh, gr.State("Enhance"), state],
|
| 104 |
-
outputs=[code_out, zip_file, preview, file_selector, state]
|
| 105 |
-
).then(
|
| 106 |
-
fn=lambda code, zip_name, preview_path, state_obj: update_file_tree(code, zip_name, preview_path, state_obj.get("bundle", {}).get("files", {}), state_obj),
|
| 107 |
-
inputs=[code_out, zip_file, preview, state],
|
| 108 |
-
outputs=[file_selector, file_viewer]
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
file_selector.change(
|
| 112 |
lambda name, state: state["bundle"]["files"].get(name, "") if state and "bundle" in state else "",
|
| 113 |
inputs=[file_selector, state],
|
|
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
def run_app(user_input, enhance_prompt, action, state, progress=gr.Progress()):
|
| 33 |
+
import google.generativeai as genai
|
| 34 |
+
from dotenv import load_dotenv
|
| 35 |
+
load_dotenv()
|
| 36 |
+
|
| 37 |
+
progress(0, desc="Authenticating with Gemini...")
|
| 38 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 39 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 40 |
+
|
| 41 |
+
progress(0.2, desc="Sending prompt to Gemini...")
|
| 42 |
+
prompt_text = f"""Create a full React app based on this idea:
|
| 43 |
+
{user_input}
|
| 44 |
+
Return only relevant files like App.jsx, index.html, styles, utils or components in plain code blocks with filenames."""
|
| 45 |
+
|
| 46 |
+
response = model.generate_content(prompt_text)
|
| 47 |
+
parts = response.text.split("```")
|
| 48 |
+
|
| 49 |
+
files = {}
|
| 50 |
+
current_file = None
|
| 51 |
+
for part in parts:
|
| 52 |
+
if part.strip().startswith("jsx") or part.strip().startswith("javascript") or part.strip().startswith("html"):
|
| 53 |
+
lines = part.strip().split("\n")
|
| 54 |
+
if len(lines) > 1 and lines[1].startswith("// filename:"):
|
| 55 |
+
current_file = lines[1].replace("// filename:", "").strip()
|
| 56 |
+
files[current_file] = "\n".join(lines[2:])
|
| 57 |
+
|
| 58 |
+
app_code = files.get("App.jsx", next(iter(files.values()), ""))
|
| 59 |
+
preview_html = extract_react_preview(app_code)
|
| 60 |
+
zip_bytes = save_as_zip({"files": files})
|
| 61 |
+
|
| 62 |
with open("generated_app.zip", "wb") as f:
|
| 63 |
f.write(zip_bytes)
|
| 64 |
|
| 65 |
+
state = {"bundle": {"full_code": app_code, "files": files}, "history": []}
|
| 66 |
+
return app_code, "generated_app.zip", preview_html, files, state
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with gr.Blocks(title="Auto App Builder") as iface:
|
| 69 |
gr.Markdown("# ⚡ Auto App Builder (Gemini Powered)\n_Powered by Google Gemini API_")
|
|
|
|
| 106 |
outputs=[file_selector, file_viewer]
|
| 107 |
)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
file_selector.change(
|
| 110 |
lambda name, state: state["bundle"]["files"].get(name, "") if state and "bundle" in state else "",
|
| 111 |
inputs=[file_selector, state],
|