YSMlearnsCode
commited on
Commit
·
85f593f
1
Parent(s):
41d6e2b
moved app.py to root
Browse files- app/app.py → app.py +6 -6
- app/process.py +0 -15
app/app.py → app.py
RENAMED
@@ -1,13 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from pathlib import Path
|
3 |
-
from process import generate_script_and_run
|
4 |
|
5 |
# === File Paths ===
|
6 |
-
generated_dir = Path(
|
7 |
fcstd_file = generated_dir / "model.FCStd"
|
8 |
obj_file = generated_dir / "model.obj"
|
9 |
|
10 |
-
# Remove stale files
|
11 |
for file in ["generated_model.FCStd", "generated_model.obj"]:
|
12 |
fpath = generated_dir / file
|
13 |
if fpath.exists():
|
@@ -48,7 +48,7 @@ with gr.Blocks(css="""
|
|
48 |
}
|
49 |
""") as demo:
|
50 |
|
51 |
-
gr.Markdown("<h1 style='text-align: center;'>
|
52 |
gr.Markdown("Generate 3D models by describing them in plain English. Powered by FreeCAD and LLMs.")
|
53 |
|
54 |
input_text = gr.Textbox(
|
@@ -73,8 +73,8 @@ with gr.Blocks(css="""
|
|
73 |
|
74 |
gr.HTML("""
|
75 |
<div class='footer-text'>
|
76 |
-
<strong>Note:</strong> CADomatic is still under development and
|
77 |
-
Please refresh and run if there is no preview
|
78 |
View the source on <a href="https://github.com/yas1nsyed/CADomatic" target="_blank">GitHub</a>.
|
79 |
</div>
|
80 |
""")
|
|
|
1 |
import gradio as gr
|
2 |
from pathlib import Path
|
3 |
+
from app.process import generate_script_and_run # Correct import from nested app directory
|
4 |
|
5 |
# === File Paths ===
|
6 |
+
generated_dir = Path("app/generated")
|
7 |
fcstd_file = generated_dir / "model.FCStd"
|
8 |
obj_file = generated_dir / "model.obj"
|
9 |
|
10 |
+
# Remove stale files (optional cleanup)
|
11 |
for file in ["generated_model.FCStd", "generated_model.obj"]:
|
12 |
fpath = generated_dir / file
|
13 |
if fpath.exists():
|
|
|
48 |
}
|
49 |
""") as demo:
|
50 |
|
51 |
+
gr.Markdown("<h1 style='text-align: center;'>CADomatic - FreeCAD Script Generator</h1>")
|
52 |
gr.Markdown("Generate 3D models by describing them in plain English. Powered by FreeCAD and LLMs.")
|
53 |
|
54 |
input_text = gr.Textbox(
|
|
|
73 |
|
74 |
gr.HTML("""
|
75 |
<div class='footer-text'>
|
76 |
+
<strong>Note:</strong> CADomatic is still under development and needs refinement. For best results, run locally. Toggle view in the .FCStd file for full geometry.<br>
|
77 |
+
Please refresh and run if there is no preview.<br>
|
78 |
View the source on <a href="https://github.com/yas1nsyed/CADomatic" target="_blank">GitHub</a>.
|
79 |
</div>
|
80 |
""")
|
app/process.py
CHANGED
@@ -33,7 +33,6 @@ fcstd_path_str = str(FCSTD_PATH).replace("\\", "\\\\")
|
|
33 |
obj_path_str = str(OBJ_PATH).replace("\\", "\\\\")
|
34 |
preview_path_str = str(PREVIEW_PATH).replace("\\", "\\\\")
|
35 |
|
36 |
-
# Updated export logic with debug logging
|
37 |
EXPORT_SNIPPET = f"""
|
38 |
import Mesh
|
39 |
import os
|
@@ -79,7 +78,6 @@ def generate_script_and_run(user_input: str):
|
|
79 |
example_code = example_prompt_path.read_text(encoding="utf-8").strip()
|
80 |
app_prompt = app_prompt_path.read_text(encoding="utf-8").strip()
|
81 |
|
82 |
-
# Build prompt
|
83 |
prompt = (
|
84 |
f"{base_instruction}\n\n"
|
85 |
f"{example_code}\n\n"
|
@@ -87,19 +85,14 @@ def generate_script_and_run(user_input: str):
|
|
87 |
f"User request: {user_input.strip()}"
|
88 |
)
|
89 |
|
90 |
-
# Generate LLM code
|
91 |
generated_code = prompt_llm(prompt)
|
92 |
|
93 |
-
# Auto-inject App.newDocument if missing
|
94 |
if "App.newDocument" not in generated_code:
|
95 |
generated_code = "App.newDocument('Unnamed')\n" + generated_code
|
96 |
|
97 |
-
# Clean up markdown formatting
|
98 |
-
# Clean up markdown formatting
|
99 |
if generated_code.startswith("```"):
|
100 |
generated_code = generated_code[generated_code.find("\n") + 1:].rsplit("```", 1)[0]
|
101 |
|
102 |
-
# Unwrap if __name__ == "__main__" blocks (FreeCAD won't execute them)
|
103 |
if "__name__" in generated_code and "def " in generated_code:
|
104 |
lines = generated_code.splitlines()
|
105 |
in_main = False
|
@@ -109,27 +102,20 @@ def generate_script_and_run(user_input: str):
|
|
109 |
in_main = True
|
110 |
continue
|
111 |
if in_main:
|
112 |
-
# Remove leading indentation (usually 4 spaces)
|
113 |
unwrapped.append(line[4:] if line.startswith(" ") else line)
|
114 |
else:
|
115 |
unwrapped.append(line)
|
116 |
generated_code = "\n".join(unwrapped)
|
117 |
|
118 |
-
# Create output folder if needed
|
119 |
GEN_DIR.mkdir(exist_ok=True)
|
120 |
|
121 |
-
# Build final script with export logic
|
122 |
full_script = f"{generated_code.strip()}\n\n{EXPORT_SNIPPET}"
|
123 |
-
|
124 |
-
# Write script
|
125 |
GEN_SCRIPT.write_text(full_script, encoding="utf-8")
|
126 |
|
127 |
-
# Delete old outputs
|
128 |
for path in [FCSTD_PATH, OBJ_PATH, PREVIEW_PATH]:
|
129 |
if path.exists():
|
130 |
path.unlink()
|
131 |
|
132 |
-
# Run FreeCAD
|
133 |
freecad_cmd = get_freecad_cmd()
|
134 |
try:
|
135 |
result = subprocess.run(
|
@@ -140,7 +126,6 @@ def generate_script_and_run(user_input: str):
|
|
140 |
timeout=60
|
141 |
)
|
142 |
|
143 |
-
# Log output
|
144 |
(GEN_DIR / "run_stdout.txt").write_text(result.stdout or "", encoding="utf-8")
|
145 |
(GEN_DIR / "run_stderr.txt").write_text(result.stderr or "", encoding="utf-8")
|
146 |
|
|
|
33 |
obj_path_str = str(OBJ_PATH).replace("\\", "\\\\")
|
34 |
preview_path_str = str(PREVIEW_PATH).replace("\\", "\\\\")
|
35 |
|
|
|
36 |
EXPORT_SNIPPET = f"""
|
37 |
import Mesh
|
38 |
import os
|
|
|
78 |
example_code = example_prompt_path.read_text(encoding="utf-8").strip()
|
79 |
app_prompt = app_prompt_path.read_text(encoding="utf-8").strip()
|
80 |
|
|
|
81 |
prompt = (
|
82 |
f"{base_instruction}\n\n"
|
83 |
f"{example_code}\n\n"
|
|
|
85 |
f"User request: {user_input.strip()}"
|
86 |
)
|
87 |
|
|
|
88 |
generated_code = prompt_llm(prompt)
|
89 |
|
|
|
90 |
if "App.newDocument" not in generated_code:
|
91 |
generated_code = "App.newDocument('Unnamed')\n" + generated_code
|
92 |
|
|
|
|
|
93 |
if generated_code.startswith("```"):
|
94 |
generated_code = generated_code[generated_code.find("\n") + 1:].rsplit("```", 1)[0]
|
95 |
|
|
|
96 |
if "__name__" in generated_code and "def " in generated_code:
|
97 |
lines = generated_code.splitlines()
|
98 |
in_main = False
|
|
|
102 |
in_main = True
|
103 |
continue
|
104 |
if in_main:
|
|
|
105 |
unwrapped.append(line[4:] if line.startswith(" ") else line)
|
106 |
else:
|
107 |
unwrapped.append(line)
|
108 |
generated_code = "\n".join(unwrapped)
|
109 |
|
|
|
110 |
GEN_DIR.mkdir(exist_ok=True)
|
111 |
|
|
|
112 |
full_script = f"{generated_code.strip()}\n\n{EXPORT_SNIPPET}"
|
|
|
|
|
113 |
GEN_SCRIPT.write_text(full_script, encoding="utf-8")
|
114 |
|
|
|
115 |
for path in [FCSTD_PATH, OBJ_PATH, PREVIEW_PATH]:
|
116 |
if path.exists():
|
117 |
path.unlink()
|
118 |
|
|
|
119 |
freecad_cmd = get_freecad_cmd()
|
120 |
try:
|
121 |
result = subprocess.run(
|
|
|
126 |
timeout=60
|
127 |
)
|
128 |
|
|
|
129 |
(GEN_DIR / "run_stdout.txt").write_text(result.stdout or "", encoding="utf-8")
|
130 |
(GEN_DIR / "run_stderr.txt").write_text(result.stderr or "", encoding="utf-8")
|
131 |
|