Spaces:
Sleeping
Sleeping
Update generator.py
Browse files- generator.py +24 -129
generator.py
CHANGED
|
@@ -1,129 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
{code}
|
| 26 |
-
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
|
| 27 |
-
</script>
|
| 28 |
-
</body>
|
| 29 |
-
</html>
|
| 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 |
-
try:
|
| 47 |
-
response = model.generate_content(prompt_text)
|
| 48 |
-
print("π FULL GEMINI RESPONSE RAW >>>\n", response)
|
| 49 |
-
print("π Gemini TEXT PREVIEW >>>\n", getattr(response, "text", "(no .text found)"))
|
| 50 |
-
if not hasattr(response, "text") or not response.text.strip():
|
| 51 |
-
raise ValueError("Gemini response is empty.")
|
| 52 |
-
parts = response.text.split("```")
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print("β Gemini error:", e)
|
| 55 |
-
return (
|
| 56 |
-
"β Error: Could not generate app from Gemini.",
|
| 57 |
-
None,
|
| 58 |
-
"<p style='color:red;'>Gemini API failed or returned empty. Try again.</p>",
|
| 59 |
-
{},
|
| 60 |
-
{}
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
files = {}
|
| 64 |
-
current_file = None
|
| 65 |
-
for part in parts:
|
| 66 |
-
if part.strip().startswith("jsx") or part.strip().startswith("javascript") or part.strip().startswith("html"):
|
| 67 |
-
lines = part.strip().split("\n")
|
| 68 |
-
if len(lines) > 1 and lines[1].startswith("// filename:"):
|
| 69 |
-
current_file = lines[1].replace("// filename:", "").strip()
|
| 70 |
-
files[current_file] = "\n".join(lines[2:])
|
| 71 |
-
|
| 72 |
-
app_code = files.get("App.jsx", next(iter(files.values()), ""))
|
| 73 |
-
preview_html = extract_react_preview(app_code)
|
| 74 |
-
zip_bytes = save_as_zip({"files": files})
|
| 75 |
-
|
| 76 |
-
with open("generated_app.zip", "wb") as f:
|
| 77 |
-
f.write(zip_bytes)
|
| 78 |
-
|
| 79 |
-
state = {"bundle": {"full_code": app_code, "files": files}, "history": []}
|
| 80 |
-
return app_code, "generated_app.zip", preview_html, files, state
|
| 81 |
-
|
| 82 |
-
with gr.Blocks(title="Auto App Builder") as iface:
|
| 83 |
-
gr.Markdown("# β‘ Auto App Builder (Gemini Powered)\n_Powered by Google Gemini API_")
|
| 84 |
-
|
| 85 |
-
with gr.Row():
|
| 86 |
-
inp = gr.Textbox(label="App Idea", placeholder="e.g. Inventory system with login", lines=2)
|
| 87 |
-
btn = gr.Button("π Generate App")
|
| 88 |
-
|
| 89 |
-
with gr.Row():
|
| 90 |
-
with gr.Column():
|
| 91 |
-
code_out = gr.Code(label="π Generated Code")
|
| 92 |
-
enh = gr.Textbox(label="Enhancement Prompt", placeholder="e.g. Add login, PDF export, dark mode...", lines=1)
|
| 93 |
-
btn_enh = gr.Button("β¨ Enhance")
|
| 94 |
-
zip_file = gr.File(label="β¬οΈ Download App ZIP", interactive=True)
|
| 95 |
-
|
| 96 |
-
gr.Markdown("### π File Explorer")
|
| 97 |
-
file_selector = gr.Dropdown(choices=[], label="Select File")
|
| 98 |
-
file_viewer = gr.Code(label="π File Content")
|
| 99 |
-
with gr.Column():
|
| 100 |
-
gr.Markdown("### π Live React Preview")
|
| 101 |
-
preview = gr.HTML(label="React App Preview")
|
| 102 |
-
|
| 103 |
-
state = gr.State()
|
| 104 |
-
|
| 105 |
-
def update_file_tree(code, zip_name, preview_path, files, state):
|
| 106 |
-
if isinstance(files, dict) and len(files) > 0:
|
| 107 |
-
keys = list(files.keys())
|
| 108 |
-
default_key = keys[0] if keys else None
|
| 109 |
-
return gr.update(choices=keys, value=default_key), files.get(default_key, "")
|
| 110 |
-
else:
|
| 111 |
-
return gr.update(choices=[], value=None), ""
|
| 112 |
-
|
| 113 |
-
btn.click(
|
| 114 |
-
fn=run_app,
|
| 115 |
-
inputs=[inp, enh, gr.State(""), state],
|
| 116 |
-
outputs=[code_out, zip_file, preview, file_selector, state]
|
| 117 |
-
).then(
|
| 118 |
-
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),
|
| 119 |
-
inputs=[code_out, zip_file, preview, state],
|
| 120 |
-
outputs=[file_selector, file_viewer]
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
file_selector.change(
|
| 124 |
-
lambda name, state: state["bundle"]["files"].get(name, "") if state and "bundle" in state else "",
|
| 125 |
-
inputs=[file_selector, state],
|
| 126 |
-
outputs=file_viewer
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
iface.launch()
|
|
|
|
| 1 |
+
def generate_app(prompt: str):
|
| 2 |
+
print("π§ Simulating Gemini API call...")
|
| 3 |
+
return {
|
| 4 |
+
"full_code": f'''
|
| 5 |
+
function App() {{
|
| 6 |
+
return <div>β‘ Hello, this is your generated app for: {prompt}</div>;
|
| 7 |
+
}}
|
| 8 |
+
export default App;
|
| 9 |
+
''',
|
| 10 |
+
"files": {
|
| 11 |
+
"App.jsx": f'''
|
| 12 |
+
function App() {{
|
| 13 |
+
return <div>β‘ Hello, this is your generated app for: {prompt}</div>;
|
| 14 |
+
}}
|
| 15 |
+
export default App;
|
| 16 |
+
'''
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def enhance_app(existing_bundle: dict, enhancement_prompt: str):
|
| 21 |
+
updated_code = existing_bundle["full_code"] + f"\n// Enhancement applied: {enhancement_prompt}"
|
| 22 |
+
existing_bundle["full_code"] = updated_code
|
| 23 |
+
existing_bundle["files"]["App.jsx"] = updated_code
|
| 24 |
+
return existing_bundle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|