vincentiusyoshuac commited on
Commit
17ea9fc
1 Parent(s): fd44661

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +41 -28
streamlit_app.py CHANGED
@@ -4,6 +4,7 @@ import uuid
4
  import requests
5
  import json
6
  from typing import Dict, Optional
 
7
 
8
  class VideoProcessor(VideoProcessorBase):
9
  def recv(self, frame):
@@ -11,30 +12,45 @@ class VideoProcessor(VideoProcessorBase):
11
  return img
12
 
13
  class WebRTCConnection:
14
- def __init__(self, server_url: str):
15
- self.server_url = server_url
16
  self.peer_id: Optional[str] = None
17
 
18
  def register_peer(self, peer_id: str) -> Dict:
19
- response = requests.post(
20
- f"{self.server_url}/register_peer",
21
- json={"peer_id": peer_id}
22
- )
23
- return response.json()
 
 
 
 
 
24
 
25
  def send_offer(self, peer_id: str, offer: Dict) -> Dict:
26
- response = requests.post(
27
- f"{self.server_url}/send_offer",
28
- json={"peer_id": peer_id, "offer": offer}
29
- )
30
- return response.json()
 
 
 
 
 
31
 
32
  def get_answer(self, peer_id: str) -> Dict:
33
- response = requests.get(
34
- f"{self.server_url}/get_answer",
35
- params={"peer_id": peer_id}
36
- )
37
- return response.json()
 
 
 
 
 
38
 
39
  def create_video_interface():
40
  st.set_page_config(
@@ -46,7 +62,7 @@ def create_video_interface():
46
 
47
  # Initialize session state
48
  if 'connection' not in st.session_state:
49
- st.session_state.connection = WebRTCConnection("http://localhost:7860")
50
  if 'status' not in st.session_state:
51
  st.session_state.status = "Initializing..."
52
  if 'peer_id' not in st.session_state:
@@ -83,15 +99,12 @@ def create_video_interface():
83
 
84
  if st.button("Connect", key="connect_button", use_container_width=True):
85
  if st.session_state.peer_id:
86
- try:
87
- response = st.session_state.connection.register_peer(
88
- st.session_state.peer_id
89
- )
90
- if response.get("status") == "registered":
91
- st.session_state.status = "Connected"
92
- st.experimental_rerun()
93
- except Exception as e:
94
- st.error(f"Connection failed: {str(e)}")
95
  else:
96
  st.error("Please enter or generate a Peer ID")
97
 
@@ -144,7 +157,7 @@ def create_video_interface():
144
 
145
  if st.button("End Call", key="end_call", use_container_width=True):
146
  st.session_state.status = "Call ended"
147
- st.experimental_rerun()
148
 
149
  if __name__ == "__main__":
150
  create_video_interface()
 
4
  import requests
5
  import json
6
  from typing import Dict, Optional
7
+ import time
8
 
9
  class VideoProcessor(VideoProcessorBase):
10
  def recv(self, frame):
 
12
  return img
13
 
14
  class WebRTCConnection:
15
+ def __init__(self):
16
+ self.server_url = "http://localhost:7860"
17
  self.peer_id: Optional[str] = None
18
 
19
  def register_peer(self, peer_id: str) -> Dict:
20
+ try:
21
+ response = requests.post(
22
+ f"{self.server_url}/register_peer",
23
+ json={"peer_id": peer_id},
24
+ timeout=5 # Add timeout
25
+ )
26
+ return response.json()
27
+ except requests.exceptions.RequestException as e:
28
+ st.error(f"Connection error: {str(e)}")
29
+ return {"status": "error", "message": str(e)}
30
 
31
  def send_offer(self, peer_id: str, offer: Dict) -> Dict:
32
+ try:
33
+ response = requests.post(
34
+ f"{self.server_url}/send_offer",
35
+ json={"peer_id": peer_id, "offer": offer},
36
+ timeout=5
37
+ )
38
+ return response.json()
39
+ except requests.exceptions.RequestException as e:
40
+ st.error(f"Connection error: {str(e)}")
41
+ return {"status": "error", "message": str(e)}
42
 
43
  def get_answer(self, peer_id: str) -> Dict:
44
+ try:
45
+ response = requests.get(
46
+ f"{self.server_url}/get_answer",
47
+ params={"peer_id": peer_id},
48
+ timeout=5
49
+ )
50
+ return response.json()
51
+ except requests.exceptions.RequestException as e:
52
+ st.error(f"Connection error: {str(e)}")
53
+ return {"status": "error", "message": str(e)}
54
 
55
  def create_video_interface():
56
  st.set_page_config(
 
62
 
63
  # Initialize session state
64
  if 'connection' not in st.session_state:
65
+ st.session_state.connection = WebRTCConnection()
66
  if 'status' not in st.session_state:
67
  st.session_state.status = "Initializing..."
68
  if 'peer_id' not in st.session_state:
 
99
 
100
  if st.button("Connect", key="connect_button", use_container_width=True):
101
  if st.session_state.peer_id:
102
+ response = st.session_state.connection.register_peer(
103
+ st.session_state.peer_id
104
+ )
105
+ if response.get("status") == "registered":
106
+ st.session_state.status = "Connected"
107
+ st.rerun() # Using st.rerun() instead of experimental_rerun
 
 
 
108
  else:
109
  st.error("Please enter or generate a Peer ID")
110
 
 
157
 
158
  if st.button("End Call", key="end_call", use_container_width=True):
159
  st.session_state.status = "Call ended"
160
+ st.rerun() # Using st.rerun() instead of experimental_rerun
161
 
162
  if __name__ == "__main__":
163
  create_video_interface()