Spaces:
Runtime error
Runtime error
File size: 2,786 Bytes
0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 03876a5 0ca4d23 |
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 |
import gradio as gr
import os
import json
import base64
import copy
import time
from PIL import Image
from io import BytesIO
from specklepy.api.client import SpeckleClient
from specklepy.api.credentials import get_default_account
from specklepy.transports.server import ServerTransport
from specklepy.api import operations
from specklepy.objects import Base
# Initialize Speckle client with your token
speckle_token = os.getenv('SPECKLE_TOKEN')
speckle_branch = os.getenv('SPECKLE_BRANCH')
speckle_streamID = os.getenv('SPECKLE_STREAM_ID')
CLIENT = SpeckleClient(host="https://speckle.xyz/")
CLIENT.authenticate_with_token(token=speckle_token)
def process_data(image, text):
# Process image: Resize and convert to base64
img_str = ""
if image is not None:
image = Image.open(image)
# Resize image, maintaining aspect ratio, and max width 1024 pixels
base_width = 1024
w_percent = (base_width / float(image.size[0]))
h_size = int((float(image.size[1]) * float(w_percent)))
image = image.resize((base_width, h_size), Image.ANTIALIAS)
# Convert to base64
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
# Prepare JSON data
obj = Base()
obj["image"] = img_str
obj["text"] = text
# Send data to Speckle
commit_id = updateSpeckleStream(speckle_streamID, speckle_branch, client, obj)
send_to_speckle(data)
# For now, we'll just return the JSON to display it
return json.dumps(data)
def updateSpeckleStream(stream_id,
branch_name,
client,
data_object,
commit_message="Updated the data object",
):
# set stream and branch
branch = client.branch.get(stream_id, branch_name)
# Get transport
transport = ServerTransport(client=client, stream_id=stream_id)
# Send the data object to the speckle stream
object_id = operations.send(data_object, [transport])
# Create a new commit with the new object
commit_id = client.commit.create(
stream_id,
object_id= object_id,
message=commit_message,
branch_name=branch_name,
)
return commit_id
with gr.Blocks() as demo:
gr.Markdown("### Upload Image and Record Voice Message")
with gr.Row():
image = gr.Image(type="file", label="Upload Image")
audio = gr.Audio(source="microphone", type="file", label="Record Voice")
submit_btn = gr.Button("Submit")
output = gr.Textbox(label="JSON Output")
submit_btn.click(fn=process_data, inputs=[image, audio], outputs=output)
demo.launch()
|