offerpk3 commited on
Commit
dc4db04
·
verified ·
1 Parent(s): 58b257f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -33
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
- progress(0, desc="Starting Gemini generation...")
34
-
35
- if not state:
36
- progress(0.1, desc="Calling Gemini API...")
37
- code_bundle = generate_app(user_input)
38
- state = {"bundle": code_bundle, "history": []}
39
- else:
40
- code_bundle = state["bundle"]
41
-
42
- if action == "Enhance" and enhance_prompt:
43
- progress(0.5, desc="Enhancing with Gemini...")
44
- enhanced = enhance_app(code_bundle, enhance_prompt)
45
- state["bundle"] = enhanced
46
- state["history"].append(enhance_prompt)
47
- code_bundle = enhanced
48
-
49
- progress(0.7, desc="Creating zip archive...")
50
- zip_bytes = save_as_zip(code_bundle)
 
 
 
 
 
 
 
 
 
 
 
51
  with open("generated_app.zip", "wb") as f:
52
  f.write(zip_bytes)
53
 
54
- progress(0.9, desc="Generating preview...")
55
- preview_html = extract_react_preview(code_bundle.get("files", {}).get("App.jsx", ""))
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],