Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011123 unmodified lines161718192021222324252627282930313233343536373839404142434445464748import gradio as grimport speech_recognition as srfrom PIL import Imageimport ioimport base64import jsondef process_data(image, audio):# Process image: Resize and convert to base64if image is not None:image = Image.open(image)# Resize image, maintaining aspect ratio, and max width 1024 pixels3 unmodified linesimage = image.resize((base_width, h_size), Image.ANTIALIAS)# Convert to base64buffered = io.BytesIO()image.save(buffered, format="JPEG")img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')else:img_str = ""# Process audio: Convert speech to textif audio is not None:recognizer = sr.Recognizer()with sr.AudioFile(audio) as source:audio_data = recognizer.record(source)try:text = recognizer.recognize_google(audio_data)except sr.UnknownValueError:text = "Could not understand audio"except sr.RequestError as e:text = f"Could not request results; {e}"else:text = ""# Prepare JSON datadata = json.dumps({"image": img_str, "text": text})# Here you would add your code to send `data` to the Speckle stream# For now, we'll just return the JSON to display itreturn datawith gr.Blocks() as demo:gr.Markdown("### Upload Image and Record Voice Message")with gr.Row():123456789101112131415161718192021222324252627283 unmodified lines323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import gradio as grimport osimport jsonimport base64import copyimport timefrom PIL import Imagefrom io import BytesIOfrom specklepy.api.client import SpeckleClientfrom specklepy.api.credentials import get_default_accountfrom specklepy.transports.server import ServerTransportfrom specklepy.api import operationsfrom specklepy.objects import Base# Initialize Speckle client with your tokenspeckle_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 base64img_str = ""if image is not None:image = Image.open(image)# Resize image, maintaining aspect ratio, and max width 1024 pixels3 unmodified linesimage = image.resize((base_width, h_size), Image.ANTIALIAS)# Convert to base64buffered = BytesIO()image.save(buffered, format="JPEG")img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')# Prepare JSON dataobj = Base()obj["image"] = img_strobj["text"] = text# Send data to Specklecommit_id = updateSpeckleStream(speckle_streamID, speckle_branch, client, obj)send_to_speckle(data)# For now, we'll just return the JSON to display itreturn json.dumps(data)def updateSpeckleStream(stream_id,branch_name,client,data_object,commit_message="Updated the data object",):# set stream and branchbranch = client.branch.get(stream_id, branch_name)# Get transporttransport = ServerTransport(client=client, stream_id=stream_id)# Send the data object to the speckle streamobject_id = operations.send(data_object, [transport])# Create a new commit with the new objectcommit_id = client.commit.create(stream_id,object_id= object_id,message=commit_message,branch_name=branch_name,)return commit_idwith gr.Blocks() as demo:gr.Markdown("### Upload Image and Record Voice Message")with gr.Row():
app.py+53-25