Spaces:
Runtime error
Runtime error
added autocad
Browse files
app.py
CHANGED
|
@@ -1,70 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 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 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import logging
|
| 7 |
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
+
# Define model and checkpoint paths
|
| 13 |
+
MODEL_PATH = "microsoft/CADFusion/v1_1"
|
| 14 |
+
CHECKPOINT_PATH = "./exp/model_ckpt/v1_1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Load model and tokenizer
|
| 17 |
+
try:
|
| 18 |
+
logger.info("Loading tokenizer...")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 20 |
+
logger.info("Loading model...")
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
MODEL_PATH,
|
| 23 |
+
torch_dtype=torch.float16,
|
| 24 |
+
device_map="auto",
|
| 25 |
+
trust_remote_code=True
|
| 26 |
+
)
|
| 27 |
+
logger.info("Model and tokenizer loaded successfully.")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"Error loading model or tokenizer: {e}")
|
| 30 |
+
raise e
|
| 31 |
|
| 32 |
+
# Function to generate CAD model from text description
|
| 33 |
+
def generate_cad_model(text_description):
|
| 34 |
+
try:
|
| 35 |
+
# Tokenize input
|
| 36 |
+
inputs = tokenizer(text_description, return_tensors="pt").to(model.device)
|
| 37 |
+
|
| 38 |
+
# Generate output
|
| 39 |
+
outputs = model.generate(
|
| 40 |
+
**inputs,
|
| 41 |
+
max_length=512,
|
| 42 |
+
num_return_sequences=1,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
temperature=0.7,
|
| 45 |
+
top_p=0.9
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Decode output
|
| 49 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
+
|
| 51 |
+
# Parse generated text to extract CAD model data (assuming JSON-like output)
|
| 52 |
+
try:
|
| 53 |
+
cad_data = json.loads(generated_text)
|
| 54 |
+
return json.dumps(cad_data, indent=2)
|
| 55 |
+
except json.JSONDecodeError:
|
| 56 |
+
return generated_text # Return raw text if JSON parsing fails
|
| 57 |
+
except Exception as e:
|
| 58 |
+
logger.error(f"Error during generation: {e}")
|
| 59 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Gradio interface
|
| 62 |
+
def create_gradio_interface():
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# CADFusion: Text-to-CAD Generation")
|
| 65 |
+
gr.Markdown("Enter a textual description of the CAD model you want to generate. For example: 'A 3D model of a chair with four legs and a curved backrest.'")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
text_input = gr.Textbox(
|
| 70 |
+
label="Text Description",
|
| 71 |
+
placeholder="Enter your CAD model description here...",
|
| 72 |
+
lines=5
|
| 73 |
+
)
|
| 74 |
+
submit_button = gr.Button("Generate CAD Model")
|
| 75 |
+
|
| 76 |
+
with gr.Column():
|
| 77 |
+
output_text = gr.Textbox(
|
| 78 |
+
label="Generated CAD Model (JSON or Text)",
|
| 79 |
+
placeholder="Generated output will appear here...",
|
| 80 |
+
lines=10
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
submit_button.click(
|
| 84 |
+
fn=generate_cad_model,
|
| 85 |
+
inputs=text_input,
|
| 86 |
+
outputs=output_text
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
gr.Markdown("""
|
| 90 |
+
**Note**:
|
| 91 |
+
- CADFusion is for research purposes only. Generated models may not be technically accurate and require validation.
|
| 92 |
+
- Ensure descriptions are clear and specific for best results.
|
| 93 |
+
- For more details, visit the [CADFusion GitHub repo](https://github.com/microsoft/CADFusion).
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
return demo
|
| 97 |
|
| 98 |
+
# Launch Gradio app
|
| 99 |
if __name__ == "__main__":
|
| 100 |
+
demo = create_gradio_interface()
|
| 101 |
+
demo.launch()
|