Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import json
|
| 4 |
import base64
|
| 5 |
-
import copy
|
| 6 |
-
import time
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
from specklepy.api.client import SpeckleClient
|
| 10 |
-
from specklepy.api.credentials import get_default_account
|
| 11 |
from specklepy.transports.server import ServerTransport
|
| 12 |
from specklepy.api import operations
|
| 13 |
from specklepy.objects import Base
|
|
@@ -15,16 +11,36 @@ from specklepy.objects import Base
|
|
| 15 |
# Initialize Speckle client with your token
|
| 16 |
speckle_token = os.getenv('SPECKLE_TOKEN')
|
| 17 |
speckle_branch = os.getenv('SPECKLE_BRANCH')
|
| 18 |
-
|
| 19 |
|
| 20 |
CLIENT = SpeckleClient(host="https://speckle.xyz/")
|
| 21 |
CLIENT.authenticate_with_token(token=speckle_token)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def process_data(image, text):
|
| 24 |
# Process image: Resize and convert to base64
|
| 25 |
img_str = ""
|
| 26 |
if image is not None:
|
| 27 |
-
image = Image.open(image)
|
| 28 |
# Resize image, maintaining aspect ratio, and max width 1024 pixels
|
| 29 |
base_width = 1024
|
| 30 |
w_percent = (base_width / float(image.size[0]))
|
|
@@ -36,49 +52,22 @@ def process_data(image, text):
|
|
| 36 |
image.save(buffered, format="JPEG")
|
| 37 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 38 |
|
| 39 |
-
# Prepare
|
| 40 |
obj = Base()
|
| 41 |
obj["image"] = img_str
|
| 42 |
obj["text"] = text
|
| 43 |
|
| 44 |
# Send data to Speckle
|
| 45 |
-
|
| 46 |
-
send_to_speckle(data)
|
| 47 |
-
|
| 48 |
-
# For now, we'll just return the JSON to display it
|
| 49 |
-
return json.dumps(data)
|
| 50 |
|
| 51 |
-
def updateSpeckleStream(stream_id,
|
| 52 |
-
branch_name,
|
| 53 |
-
client,
|
| 54 |
-
data_object,
|
| 55 |
-
commit_message="Updated the data object",
|
| 56 |
-
):
|
| 57 |
-
# set stream and branch
|
| 58 |
-
branch = client.branch.get(stream_id, branch_name)
|
| 59 |
-
# Get transport
|
| 60 |
-
transport = ServerTransport(client=client, stream_id=stream_id)
|
| 61 |
-
# Send the data object to the speckle stream
|
| 62 |
-
object_id = operations.send(data_object, [transport])
|
| 63 |
-
|
| 64 |
-
# Create a new commit with the new object
|
| 65 |
-
commit_id = client.commit.create(
|
| 66 |
-
stream_id,
|
| 67 |
-
object_id= object_id,
|
| 68 |
-
message=commit_message,
|
| 69 |
-
branch_name=branch_name,
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
return commit_id
|
| 73 |
-
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
-
gr.Markdown("### Upload Image and
|
| 76 |
with gr.Row():
|
| 77 |
-
image = gr.Image(type="
|
| 78 |
-
|
| 79 |
submit_btn = gr.Button("Submit")
|
| 80 |
-
output = gr.Textbox(label="
|
| 81 |
|
| 82 |
-
submit_btn.click(fn=process_data, inputs=[image,
|
| 83 |
|
| 84 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
import base64
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
from specklepy.api.client import SpeckleClient
|
|
|
|
| 7 |
from specklepy.transports.server import ServerTransport
|
| 8 |
from specklepy.api import operations
|
| 9 |
from specklepy.objects import Base
|
|
|
|
| 11 |
# Initialize Speckle client with your token
|
| 12 |
speckle_token = os.getenv('SPECKLE_TOKEN')
|
| 13 |
speckle_branch = os.getenv('SPECKLE_BRANCH')
|
| 14 |
+
speckle_stream_id = os.getenv('SPECKLE_STREAM_ID')
|
| 15 |
|
| 16 |
CLIENT = SpeckleClient(host="https://speckle.xyz/")
|
| 17 |
CLIENT.authenticate_with_token(token=speckle_token)
|
| 18 |
|
| 19 |
+
def send_to_speckle(stream_id, branch_name, client, data_object):
|
| 20 |
+
try:
|
| 21 |
+
# Get transport
|
| 22 |
+
transport = ServerTransport(client=client, stream_id=stream_id)
|
| 23 |
+
|
| 24 |
+
# Send the data object to the speckle stream
|
| 25 |
+
object_id = operations.send(data_object, [transport])
|
| 26 |
+
|
| 27 |
+
# Create a new commit with the new object
|
| 28 |
+
commit_id = client.commit.create(
|
| 29 |
+
stream_id=stream_id,
|
| 30 |
+
object_id=object_id,
|
| 31 |
+
branch_name=branch_name,
|
| 32 |
+
message="Updated the data object from Gradio app",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return f"Success! Data sent to Speckle stream. Commit ID: {commit_id}"
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"An error occurred: {e}"
|
| 38 |
+
|
| 39 |
def process_data(image, text):
|
| 40 |
# Process image: Resize and convert to base64
|
| 41 |
img_str = ""
|
| 42 |
if image is not None:
|
| 43 |
+
image = Image.open(BytesIO(image))
|
| 44 |
# Resize image, maintaining aspect ratio, and max width 1024 pixels
|
| 45 |
base_width = 1024
|
| 46 |
w_percent = (base_width / float(image.size[0]))
|
|
|
|
| 52 |
image.save(buffered, format="JPEG")
|
| 53 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 54 |
|
| 55 |
+
# Prepare data object
|
| 56 |
obj = Base()
|
| 57 |
obj["image"] = img_str
|
| 58 |
obj["text"] = text
|
| 59 |
|
| 60 |
# Send data to Speckle
|
| 61 |
+
return send_to_speckle(speckle_stream_id, speckle_branch, CLIENT, obj)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("### Upload Image and Enter Text")
|
| 65 |
with gr.Row():
|
| 66 |
+
image = gr.Image(type="pil", label="Add Image")
|
| 67 |
+
text = gr.TextArea(label="Enter Text or Speak")
|
| 68 |
submit_btn = gr.Button("Submit")
|
| 69 |
+
output = gr.Textbox(label="Status", visible=False) # Set visible to False to hide output
|
| 70 |
|
| 71 |
+
submit_btn.click(fn=process_data, inputs=[image, text], outputs=output)
|
| 72 |
|
| 73 |
demo.launch()
|