Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import io
|
| 5 |
-
import base64
|
| 6 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Process image: Resize and convert to base64
|
|
|
|
| 10 |
if image is not None:
|
| 11 |
image = Image.open(image)
|
| 12 |
# Resize image, maintaining aspect ratio, and max width 1024 pixels
|
|
@@ -16,33 +32,45 @@ def process_data(image, audio):
|
|
| 16 |
image = image.resize((base_width, h_size), Image.ANTIALIAS)
|
| 17 |
|
| 18 |
# Convert to base64
|
| 19 |
-
buffered =
|
| 20 |
image.save(buffered, format="JPEG")
|
| 21 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 22 |
-
else:
|
| 23 |
-
img_str = ""
|
| 24 |
-
|
| 25 |
-
# Process audio: Convert speech to text
|
| 26 |
-
if audio is not None:
|
| 27 |
-
recognizer = sr.Recognizer()
|
| 28 |
-
with sr.AudioFile(audio) as source:
|
| 29 |
-
audio_data = recognizer.record(source)
|
| 30 |
-
try:
|
| 31 |
-
text = recognizer.recognize_google(audio_data)
|
| 32 |
-
except sr.UnknownValueError:
|
| 33 |
-
text = "Could not understand audio"
|
| 34 |
-
except sr.RequestError as e:
|
| 35 |
-
text = f"Could not request results; {e}"
|
| 36 |
-
else:
|
| 37 |
-
text = ""
|
| 38 |
|
| 39 |
# Prepare JSON data
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# Here you would add your code to send `data` to the Speckle stream
|
| 43 |
# For now, we'll just return the JSON to display it
|
| 44 |
-
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
gr.Markdown("### Upload Image and Record Voice Message")
|
| 48 |
with gr.Row():
|
|
|
|
| 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
|
| 14 |
|
| 15 |
+
# Initialize Speckle client with your token
|
| 16 |
+
speckle_token = os.getenv('SPECKLE_TOKEN')
|
| 17 |
+
speckle_branch = os.getenv('SPECKLE_BRANCH')
|
| 18 |
+
speckle_streamID = os.getenv('SPECKLE_STREAM_ID')
|
| 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
|
|
|
|
| 32 |
image = image.resize((base_width, h_size), Image.ANTIALIAS)
|
| 33 |
|
| 34 |
# Convert to base64
|
| 35 |
+
buffered = BytesIO()
|
| 36 |
image.save(buffered, format="JPEG")
|
| 37 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Prepare JSON data
|
| 40 |
+
obj = Base()
|
| 41 |
+
obj["image"] = img_str
|
| 42 |
+
obj["text"] = text
|
| 43 |
+
|
| 44 |
+
# Send data to Speckle
|
| 45 |
+
commit_id = updateSpeckleStream(speckle_streamID, speckle_branch, client, obj)
|
| 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 Record Voice Message")
|
| 76 |
with gr.Row():
|