-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio.py
96 lines (81 loc) · 2.8 KB
/
gradio.py
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
86
87
88
89
90
91
92
93
94
95
96
import gradio as gr
import cv2
import numpy as np
import pyautogui
import pyaudio
import threading
import time
import simplepeer
import uuid
# Global variables
audio_recording = False
audio_stream = None
audio_data = []
# Audio recording function
def start_audio_recording():
global audio_recording, audio_stream, audio_data
if not audio_recording:
audio_recording = True
audio_data = []
audio_stream = pyaudio.PyAudio().open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024)
print("Audio recording started.")
# Start thread to record audio in the background
threading.Thread(target=record_audio).start()
else:
print("Audio is already recording.")
def stop_audio_recording():
global audio_recording, audio_stream
if audio_recording:
audio_recording = False
audio_stream.stop_stream()
audio_stream.close()
print("Audio recording stopped.")
else:
print("No audio recording to stop.")
def record_audio():
global audio_stream, audio_data
while audio_recording:
audio_chunk = np.frombuffer(audio_stream.read(1024), dtype=np.int16)
audio_data.append(audio_chunk)
# Screen Capture for WebRTC
def capture_screen():
while True:
screenshot = pyautogui.screenshot()
frame = np.array(screenshot)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
yield frame
# Function to start the peer connection and handle screen share
def start_screen_sharing(audio_status):
global peer
if audio_status == "Start Audio":
start_audio_recording()
elif audio_status == "Stop Audio":
stop_audio_recording()
print("Screen sharing started")
peer = simplepeer({
"initiator": True,
"trickle": False,
})
# Prepare the video stream for the peer connection
for frame in capture_screen():
peer.signal(frame)
time.sleep(0.1) # Control frame rate
return "Screen sharing started"
# Gradio Interface
def interface():
with gr.Blocks() as demo:
gr.Markdown("### Real-Time Screen Sharing with Audio Capture")
with gr.Row():
screen_share_button = gr.Button("Start Screen Share")
audio_button = gr.Button("Start Audio")
status = gr.Textbox(value="No Status Yet", label="Current Status", interactive=False)
screen_share_button.click(start_screen_sharing, inputs=[audio_button], outputs=status)
audio_button.click(start_audio_recording, inputs=None, outputs=None)
demo.launch()
# Run Gradio Interface
if __name__ == "__main__":
interface()