Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -557,150 +557,4 @@ with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr
|
|
| 557 |
)
|
| 558 |
|
| 559 |
if __name__ == "__main__":
|
| 560 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 561 |
-
```
|
| 562 |
-
|
| 563 |
-
#### Updated `polygenix_connector.py`
|
| 564 |
-
This Blender addon is modified to send a multipart/form-data request to the Gradio API endpoint `/api/generate`, including the image file and parameters, with the API key in the headers. Since Gradio’s API typically expects a JSON payload but can handle file uploads, we’ll use a multipart request to align with the original FastAPI endpoint’s behavior, ensuring compatibility with the `run_full` function.
|
| 565 |
-
|
| 566 |
-
<xaiArtifact artifact_id="abd5354a-b0dd-402a-ac97-da6758e80d6e" artifact_version_id="294e2e52-e5ab-4c63-9067-5cc5b60ac63f" title="polygenix_connector.py" contentType="text/python">
|
| 567 |
-
```python
|
| 568 |
-
import bpy
|
| 569 |
-
import requests
|
| 570 |
-
import os
|
| 571 |
-
from bpy.types import Operator, Panel
|
| 572 |
-
from bpy.props import StringProperty, IntProperty, FloatProperty, BoolProperty
|
| 573 |
-
|
| 574 |
-
class PolyGenixAIOperator(Operator):
|
| 575 |
-
bl_idname = "polygenixai.generate_model"
|
| 576 |
-
bl_label = "Generate Model"
|
| 577 |
-
bl_description = "Generate a 3D model from an image using PolyGenixAI6.0 API"
|
| 578 |
-
|
| 579 |
-
image_path: StringProperty(
|
| 580 |
-
name="Image",
|
| 581 |
-
description="Path to the input image",
|
| 582 |
-
subtype='FILE_PATH'
|
| 583 |
-
)
|
| 584 |
-
seed: IntProperty(
|
| 585 |
-
name="Seed",
|
| 586 |
-
description="Random seed for generation",
|
| 587 |
-
default=0,
|
| 588 |
-
min=0
|
| 589 |
-
)
|
| 590 |
-
num_inference_steps: IntProperty(
|
| 591 |
-
name="Inference Steps",
|
| 592 |
-
description="Number of inference steps",
|
| 593 |
-
default=50,
|
| 594 |
-
min=8,
|
| 595 |
-
max=50
|
| 596 |
-
)
|
| 597 |
-
guidance_scale: FloatProperty(
|
| 598 |
-
name="Guidance Scale",
|
| 599 |
-
description="Guidance scale for generation",
|
| 600 |
-
default=7.5,
|
| 601 |
-
min=0.0,
|
| 602 |
-
max=20.0
|
| 603 |
-
)
|
| 604 |
-
simplify_mesh: BoolProperty(
|
| 605 |
-
name="Simplify Mesh",
|
| 606 |
-
description="Simplify the generated mesh",
|
| 607 |
-
default=True
|
| 608 |
-
)
|
| 609 |
-
target_face_num: IntProperty(
|
| 610 |
-
name="Target Face Number",
|
| 611 |
-
description="Target number of faces for simplified mesh",
|
| 612 |
-
default=100000,
|
| 613 |
-
min=10000,
|
| 614 |
-
max=1000000
|
| 615 |
-
)
|
| 616 |
-
|
| 617 |
-
def execute(self, context):
|
| 618 |
-
self.report({'INFO'}, "Generating model, please wait up to 3 minutes...")
|
| 619 |
-
api_url = "https://anvilinteractiv-polygenixai60.hf.space/api/generate"
|
| 620 |
-
api_key = "2065d2dc-d0d4-4432-8279-58d720bf2a72"
|
| 621 |
-
|
| 622 |
-
if not self.image_path:
|
| 623 |
-
self.report({'ERROR'}, "Please select an image file")
|
| 624 |
-
return {'CANCELLED'}
|
| 625 |
-
|
| 626 |
-
if not os.path.exists(self.image_path):
|
| 627 |
-
self.report({'ERROR'}, f"Image file not found: {self.image_path}")
|
| 628 |
-
return {'CANCELLED'}
|
| 629 |
-
|
| 630 |
-
try:
|
| 631 |
-
with open(self.image_path, 'rb') as image_file:
|
| 632 |
-
files = {'data[0]': (os.path.basename(self.image_path), image_file, 'image/jpeg')}
|
| 633 |
-
data = {
|
| 634 |
-
'data[1]': str(self.seed),
|
| 635 |
-
'data[2]': str(self.num_inference_steps),
|
| 636 |
-
'data[3]': str(self.guidance_scale),
|
| 637 |
-
'data[4]': str(self.simplify_mesh).lower(),
|
| 638 |
-
'data[5]': str(self.target_face_num)
|
| 639 |
-
}
|
| 640 |
-
headers = {'X-API-Key': api_key}
|
| 641 |
-
|
| 642 |
-
response = requests.post(api_url, files=files, data=data, headers=headers, timeout=300)
|
| 643 |
-
|
| 644 |
-
if response.status_code == 200:
|
| 645 |
-
result = response.json()
|
| 646 |
-
glb_url = result.get('data', {}).get('file_url')
|
| 647 |
-
if not glb_url:
|
| 648 |
-
self.report({'ERROR'}, "No file URL returned from API")
|
| 649 |
-
return {'CANCELLED'}
|
| 650 |
-
|
| 651 |
-
# Download the GLB file
|
| 652 |
-
full_glb_url = f"https://anvilinteractiv-polygenixai60.hf.space{glb_url}"
|
| 653 |
-
glb_response = requests.get(full_glb_url, timeout=60)
|
| 654 |
-
|
| 655 |
-
if glb_response.status_code != 200:
|
| 656 |
-
self.report({'ERROR'}, f"Failed to download GLB file: {glb_response.status_code}")
|
| 657 |
-
return {'CANCELLED'}
|
| 658 |
-
|
| 659 |
-
# Save GLB to temporary file
|
| 660 |
-
temp_glb_path = os.path.join(bpy.app.tempdir, f"polygenix_model_{os.urandom(8).hex()}.glb")
|
| 661 |
-
with open(temp_glb_path, 'wb') as f:
|
| 662 |
-
f.write(glb_response.content)
|
| 663 |
-
|
| 664 |
-
# Import GLB into Blender
|
| 665 |
-
bpy.ops.import_scene.gltf(filepath=temp_glb_path)
|
| 666 |
-
|
| 667 |
-
# Clean up temporary file
|
| 668 |
-
os.remove(temp_glb_path)
|
| 669 |
-
|
| 670 |
-
self.report({'INFO'}, "Model generated and imported successfully")
|
| 671 |
-
return {'FINISHED'}
|
| 672 |
-
else:
|
| 673 |
-
self.report({'ERROR'}, f"API Error: {response.status_code} - {response.text}")
|
| 674 |
-
return {'CANCELLED'}
|
| 675 |
-
|
| 676 |
-
except requests.exceptions.RequestException as e:
|
| 677 |
-
self.report({'ERROR'}, f"Request failed: {str(e)}")
|
| 678 |
-
return {'CANCELLED'}
|
| 679 |
-
except Exception as e:
|
| 680 |
-
self.report({'ERROR'}, f"Unexpected error: {str(e)}")
|
| 681 |
-
return {'CANCELLED'}
|
| 682 |
-
|
| 683 |
-
def invoke(self, context, event):
|
| 684 |
-
return context.window_manager.invoke_props_dialog(self)
|
| 685 |
-
|
| 686 |
-
class PolyGenixAIPanel(Panel):
|
| 687 |
-
bl_label = "PolyGenixAI6.0 Connector"
|
| 688 |
-
bl_idname = "PT_PolyGenixAI"
|
| 689 |
-
bl_space_type = 'VIEW_3D'
|
| 690 |
-
bl_region_type = 'UI'
|
| 691 |
-
bl_category = 'PolyGenix'
|
| 692 |
-
|
| 693 |
-
def draw(self, context):
|
| 694 |
-
layout = self.layout
|
| 695 |
-
layout.operator("polygenixai.generate_model")
|
| 696 |
-
|
| 697 |
-
def register():
|
| 698 |
-
bpy.utils.register_class(PolyGenixAIOperator)
|
| 699 |
-
bpy.utils.register_class(PolyGenixAIPanel)
|
| 700 |
-
|
| 701 |
-
def unregister():
|
| 702 |
-
bpy.utils.unregister_class(PolyGenixAIOperator)
|
| 703 |
-
bpy.utils.unregister_class(PolyGenixAIPanel)
|
| 704 |
-
|
| 705 |
-
if __name__ == "__main__":
|
| 706 |
-
register()
|
|
|
|
| 557 |
)
|
| 558 |
|
| 559 |
if __name__ == "__main__":
|
| 560 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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