Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,152 +1,152 @@
|
|
| 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 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
"""
|
| 105 |
return info
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import traceback
|
| 7 |
+
|
| 8 |
+
# Global model storage
|
| 9 |
+
models = {}
|
| 10 |
+
|
| 11 |
+
@spaces.GPU(duration=120)
|
| 12 |
+
def generate_code(image, model_choice, prompt_style):
|
| 13 |
+
"""Generate CADQuery code - single GPU function."""
|
| 14 |
+
if image is None:
|
| 15 |
+
return "β Please upload an image first."
|
| 16 |
+
|
| 17 |
+
# Create prompts
|
| 18 |
+
prompts = {
|
| 19 |
+
"Simple": "Generate CADQuery Python code for this 3D model:",
|
| 20 |
+
"Detailed": "Analyze this 3D CAD model and generate Python CADQuery code.\n\nRequirements:\n- Import cadquery as cq\n- Store result in 'result' variable\n- Use proper CADQuery syntax\n\nCode:",
|
| 21 |
+
"Chain-of-Thought": "Analyze this 3D CAD model step by step:\n\nStep 1: Identify the basic geometry\nStep 2: Note any features\nStep 3: Generate clean CADQuery Python code\n\n```python\nimport cadquery as cq\n\n# Generated code:"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Model mapping
|
| 26 |
+
model_map = {
|
| 27 |
+
"GLM-4.5V-AWQ": "QuantTrio/GLM-4.5V-AWQ",
|
| 28 |
+
"GLM-4.5V-FP8": "zai-org/GLM-4.5V-FP8",
|
| 29 |
+
"GLM-4.5V": "zai-org/GLM-4.5V"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
model_name = model_map[model_choice]
|
| 33 |
+
|
| 34 |
+
# Load or get cached model
|
| 35 |
+
if model_name not in models:
|
| 36 |
+
pipe = pipeline(
|
| 37 |
+
"image-text-to-text",
|
| 38 |
+
model=model_name,
|
| 39 |
+
device_map="auto",
|
| 40 |
+
torch_dtype=torch.float16,
|
| 41 |
+
trust_remote_code=True
|
| 42 |
+
)
|
| 43 |
+
models[model_name] = pipe
|
| 44 |
+
else:
|
| 45 |
+
pipe = models[model_name]
|
| 46 |
+
|
| 47 |
+
# Generate
|
| 48 |
+
messages = [{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": [
|
| 51 |
+
{"type": "image", "image": image},
|
| 52 |
+
{"type": "text", "text": prompts[prompt_style]}
|
| 53 |
+
]
|
| 54 |
+
}]
|
| 55 |
+
|
| 56 |
+
result = pipe(messages, max_new_tokens=512, temperature=0.7)
|
| 57 |
+
|
| 58 |
+
if isinstance(result, list) and len(result) > 0:
|
| 59 |
+
generated_text = result[0].get("generated_text", str(result))
|
| 60 |
+
else:
|
| 61 |
+
generated_text = str(result)
|
| 62 |
+
|
| 63 |
+
# Extract code
|
| 64 |
+
code = generated_text.strip()
|
| 65 |
+
if "```python" in code:
|
| 66 |
+
start = code.find("```python") + 9
|
| 67 |
+
end = code.find("```", start)
|
| 68 |
+
if end > start:
|
| 69 |
+
code = code[start:end].strip()
|
| 70 |
+
|
| 71 |
+
if "import cadquery" not in code:
|
| 72 |
+
code = "import cadquery as cq\n\n" + code
|
| 73 |
+
|
| 74 |
+
return f"""## π― Generated CADQuery Code
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
{code}
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
## π Info
|
| 81 |
+
- **Model**: {model_choice}
|
| 82 |
+
- **Prompt**: {prompt_style}
|
| 83 |
+
- **Device**: GPU
|
| 84 |
+
|
| 85 |
+
## π§ Usage
|
| 86 |
+
```bash
|
| 87 |
+
pip install cadquery
|
| 88 |
+
python your_script.py
|
| 89 |
+
```
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return f"β **Generation Failed**: {str(e)[:500]}"
|
| 94 |
+
|
| 95 |
+
def system_info():
|
| 96 |
+
"""Get system info - no GPU needed."""
|
| 97 |
+
info = f"""## π₯οΈ System Information
|
| 98 |
+
|
| 99 |
+
- **CUDA Available**: {torch.cuda.is_available()}
|
| 100 |
+
- **CUDA Devices**: {torch.cuda.device_count() if torch.cuda.is_available() else 0}
|
| 101 |
+
- **PyTorch Version**: {torch.__version__}
|
| 102 |
+
- **Device**: {"GPU" if torch.cuda.is_available() else "CPU"}
|
| 103 |
+
"""
|
|
|
|
| 104 |
return info
|
| 105 |
|
| 106 |
+
# Create interface
|
| 107 |
+
with gr.Blocks(title="GLM-4.5V CAD Generator", theme=gr.themes.Soft()) as demo:
|
| 108 |
+
gr.Markdown("""
|
| 109 |
+
# π§ GLM-4.5V CAD Generator
|
| 110 |
+
|
| 111 |
+
Generate CADQuery Python code from 3D CAD model images using GLM-4.5V models!
|
| 112 |
+
|
| 113 |
+
**Models**: GLM-4.5V-AWQ (fastest) | GLM-4.5V-FP8 (balanced) | GLM-4.5V (best quality)
|
| 114 |
+
""")
|
| 115 |
+
|
| 116 |
+
with gr.Tab("π Generate"):
|
| 117 |
+
with gr.Row():
|
| 118 |
+
with gr.Column():
|
| 119 |
+
image_input = gr.Image(type="pil", label="Upload CAD Model Image")
|
| 120 |
+
model_choice = gr.Dropdown(
|
| 121 |
+
choices=["GLM-4.5V-AWQ", "GLM-4.5V-FP8", "GLM-4.5V"],
|
| 122 |
+
value="GLM-4.5V-AWQ",
|
| 123 |
+
label="Select Model"
|
| 124 |
+
)
|
| 125 |
+
prompt_style = gr.Dropdown(
|
| 126 |
+
choices=["Simple", "Detailed", "Chain-of-Thought"],
|
| 127 |
+
value="Chain-of-Thought",
|
| 128 |
+
label="Prompt Style"
|
| 129 |
+
)
|
| 130 |
+
generate_btn = gr.Button("π Generate CADQuery Code", variant="primary")
|
| 131 |
+
|
| 132 |
+
with gr.Column():
|
| 133 |
+
output = gr.Markdown("Upload an image and click Generate!")
|
| 134 |
+
|
| 135 |
+
generate_btn.click(
|
| 136 |
+
fn=generate_code,
|
| 137 |
+
inputs=[image_input, model_choice, prompt_style],
|
| 138 |
+
outputs=output
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
with gr.Tab("βοΈ System"):
|
| 142 |
+
info_display = gr.Markdown()
|
| 143 |
+
refresh_btn = gr.Button("π Refresh")
|
| 144 |
+
|
| 145 |
+
demo.load(fn=system_info, outputs=info_display)
|
| 146 |
+
refresh_btn.click(fn=system_info, outputs=info_display)
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
print("π Starting GLM-4.5V CAD Generator...")
|
| 150 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 151 |
+
# Remove share=True for HF Spaces
|
| 152 |
+
demo.launch()
|