serJD's picture
Update app.py
03876a5 verified
raw
history blame
2.79 kB
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()