File size: 1,886 Bytes
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
import gradio as gr
import speech_recognition as sr
from PIL import Image
import io
import base64
import json

def process_data(image, audio):
    # Process image: Resize and convert to base64
    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 = io.BytesIO()
        image.save(buffered, format="JPEG")
        img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
    else:
        img_str = ""

    # Process audio: Convert speech to text
    if 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 data
    data = 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 it
    return data

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()