jeongsoo commited on
Commit
fafa650
·
1 Parent(s): d4d4ad6
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -7,6 +7,10 @@ import time
7
  def connect_server(ngrok_url):
8
  """서버 연결 시도"""
9
  try:
 
 
 
 
10
  # 마지막 슬래시 제거
11
  if ngrok_url.endswith('/'):
12
  ngrok_url = ngrok_url[:-1]
@@ -15,11 +19,11 @@ def connect_server(ngrok_url):
15
  response = requests.get(f"{ngrok_url}/health", timeout=5)
16
 
17
  if response.status_code == 200:
18
- return True, f"서버 연결 성공: {ngrok_url}", ngrok_url
19
  else:
20
- return False, f"서버 응답 오류: HTTP {response.status_code}", None
21
  except RequestException as e:
22
- return False, f"서버 연결 실패: {str(e)}", None
23
 
24
  def check_status(ngrok_url):
25
  """서버 상태 확인"""
@@ -68,7 +72,7 @@ def echo_test(ngrok_url, message):
68
  def get_devices(ngrok_url):
69
  """장치 목록 조회"""
70
  if not ngrok_url:
71
- return "서버 URL이 설정되지 않았습니다. 먼저 서버에 연결하세요.", []
72
 
73
  try:
74
  response = requests.get(f"{ngrok_url}/api/devices", timeout=5)
@@ -84,11 +88,11 @@ def get_devices(ngrok_url):
84
  device_type = device.get("type", "Unknown")
85
  device_options.append((f"{device_name} ({device_type})", device_id))
86
 
87
- return json.dumps(result_json, indent=2, ensure_ascii=False), gr.Dropdown(choices=device_options, value=device_options[0][1] if device_options else None)
88
  else:
89
- return f"장치 목록 조회 실패: HTTP {response.status_code}\n{response.text}", gr.Dropdown(choices=[])
90
  except RequestException as e:
91
- return f"장치 목록 조회 중 오류 발생: {str(e)}", gr.Dropdown(choices=[])
92
 
93
  def get_device_info(ngrok_url, device_id):
94
  """장치 상세 정보 조회"""
@@ -111,7 +115,7 @@ def get_device_info(ngrok_url, device_id):
111
  def get_programs(ngrok_url):
112
  """프로그램 목록 조회"""
113
  if not ngrok_url:
114
- return "서버 URL이 설정되지 않았습니다. 먼저 서버에 연결하세요.", []
115
 
116
  try:
117
  response = requests.get(f"{ngrok_url}/api/programs", timeout=5)
@@ -127,11 +131,11 @@ def get_programs(ngrok_url):
127
  program_desc = program.get("description", "")
128
  program_options.append((f"{program_name} - {program_desc}", program_id))
129
 
130
- return json.dumps(result_json, indent=2, ensure_ascii=False), gr.Dropdown(choices=program_options, value=program_options[0][1] if program_options else None)
131
  else:
132
- return f"프로그램 목록 조회 실패: HTTP {response.status_code}\n{response.text}", gr.Dropdown(choices=[])
133
  except RequestException as e:
134
- return f"프로그램 목록 조회 중 오류 발생: {str(e)}", gr.Dropdown(choices=[])
135
 
136
  def execute_program(ngrok_url, program_id):
137
  """프로그램 실행"""
@@ -152,7 +156,7 @@ def execute_program(ngrok_url, program_id):
152
  return f"프로그램 실행 중 오류 발생: {str(e)}"
153
 
154
  # UI 구성
155
- with gr.Blocks(title="LocalPCAgent 제어 인터페이스", theme=gr.themes.Soft()) as demo:
156
  gr.Markdown("# LocalPCAgent 제어 인터페이스")
157
  gr.Markdown("로컬 PC의 기능을 원격으로 제어하고 관리할 수 있는 웹 인터페이스입니다.")
158
 
@@ -160,7 +164,7 @@ with gr.Blocks(title="LocalPCAgent 제어 인터페이스", theme=gr.themes.Soft
160
  server_url = gr.State("")
161
 
162
  # 서버 연결 섹션
163
- with gr.Box():
164
  gr.Markdown("## 서버 연결")
165
  with gr.Row():
166
  ngrok_url_input = gr.Textbox(
@@ -175,13 +179,13 @@ with gr.Blocks(title="LocalPCAgent 제어 인터페이스", theme=gr.themes.Soft
175
  # 작업 메뉴 탭
176
  with gr.Tabs() as tabs:
177
  # 1. 기본 기능 탭
178
- with gr.Tab(label="기본 기능"):
179
- with gr.Box():
180
  gr.Markdown("### 서버 상태 확인")
181
  check_status_btn = gr.Button("상태 확인", variant="primary")
182
  status_result = gr.Textbox(label="결과", interactive=False, lines=10)
183
 
184
- with gr.Box():
185
  gr.Markdown("### 에코 테스트")
186
  echo_message = gr.Textbox(
187
  label="메시지",
@@ -192,26 +196,26 @@ with gr.Blocks(title="LocalPCAgent 제어 인터페이스", theme=gr.themes.Soft
192
  echo_result = gr.Textbox(label="결과", interactive=False, lines=10)
193
 
194
  # 2. 장치 관리 탭
195
- with gr.Tab(label="장치 관리"):
196
- with gr.Box():
197
  gr.Markdown("### 장치 목록 조회")
198
  get_devices_btn = gr.Button("장치 목록 가져오기", variant="primary")
199
  devices_result = gr.Textbox(label="결과", interactive=False, lines=15)
200
 
201
- with gr.Box():
202
  gr.Markdown("### 장치 상세 정보")
203
  device_dropdown = gr.Dropdown(label="장치 선택", choices=[], interactive=True)
204
  get_device_info_btn = gr.Button("장치 정보 가져오기", variant="primary")
205
  device_info_result = gr.Textbox(label="결과", interactive=False, lines=15)
206
 
207
  # 3. 프로그램 실행 탭
208
- with gr.Tab(label="프로그램 실행"):
209
- with gr.Box():
210
  gr.Markdown("### 프로그램 목록 조회")
211
  get_programs_btn = gr.Button("프로그램 목록 가져오기", variant="primary")
212
  programs_result = gr.Textbox(label="결과", interactive=False, lines=15)
213
 
214
- with gr.Box():
215
  gr.Markdown("### 프로그램 실행")
216
  program_dropdown = gr.Dropdown(label="프로그램 선택", choices=[], interactive=True)
217
  execute_program_btn = gr.Button("프로그램 실행", variant="primary")
@@ -238,6 +242,11 @@ with gr.Blocks(title="LocalPCAgent 제어 인터페이스", theme=gr.themes.Soft
238
  )
239
 
240
  # 장치 관리 이벤트
 
 
 
 
 
241
  get_devices_btn.click(
242
  fn=get_devices,
243
  inputs=[server_url],
@@ -251,6 +260,11 @@ with gr.Blocks(title="LocalPCAgent 제어 인터페이스", theme=gr.themes.Soft
251
  )
252
 
253
  # 프로그램 실행 이벤트
 
 
 
 
 
254
  get_programs_btn.click(
255
  fn=get_programs,
256
  inputs=[server_url],
 
7
  def connect_server(ngrok_url):
8
  """서버 연결 시도"""
9
  try:
10
+ # 입력이 비어있는지 확인
11
+ if not ngrok_url:
12
+ return False, "서버 URL을 입력해주세요!", ""
13
+
14
  # 마지막 슬래시 제거
15
  if ngrok_url.endswith('/'):
16
  ngrok_url = ngrok_url[:-1]
 
19
  response = requests.get(f"{ngrok_url}/health", timeout=5)
20
 
21
  if response.status_code == 200:
22
+ return True, f"서버 연결 성공!", ngrok_url
23
  else:
24
+ return False, f"서버 응답 오류: HTTP {response.status_code}", ""
25
  except RequestException as e:
26
+ return False, f"서버 연결 실패: {str(e)}", ""
27
 
28
  def check_status(ngrok_url):
29
  """서버 상태 확인"""
 
72
  def get_devices(ngrok_url):
73
  """장치 목록 조회"""
74
  if not ngrok_url:
75
+ return "서버 URL이 설정되지 않았습니다. 먼저 서버에 연결하세요.", None
76
 
77
  try:
78
  response = requests.get(f"{ngrok_url}/api/devices", timeout=5)
 
88
  device_type = device.get("type", "Unknown")
89
  device_options.append((f"{device_name} ({device_type})", device_id))
90
 
91
+ return json.dumps(result_json, indent=2, ensure_ascii=False), device_options
92
  else:
93
+ return f"장치 목록 조회 실패: HTTP {response.status_code}\n{response.text}", None
94
  except RequestException as e:
95
+ return f"장치 목록 조회 중 오류 발생: {str(e)}", None
96
 
97
  def get_device_info(ngrok_url, device_id):
98
  """장치 상세 정보 조회"""
 
115
  def get_programs(ngrok_url):
116
  """프로그램 목록 조회"""
117
  if not ngrok_url:
118
+ return "서버 URL이 설정되지 않았습니다. 먼저 서버에 연결하세요.", None
119
 
120
  try:
121
  response = requests.get(f"{ngrok_url}/api/programs", timeout=5)
 
131
  program_desc = program.get("description", "")
132
  program_options.append((f"{program_name} - {program_desc}", program_id))
133
 
134
+ return json.dumps(result_json, indent=2, ensure_ascii=False), program_options
135
  else:
136
+ return f"프로그램 목록 조회 실패: HTTP {response.status_code}\n{response.text}", None
137
  except RequestException as e:
138
+ return f"프로그램 목록 조회 중 오류 발생: {str(e)}", None
139
 
140
  def execute_program(ngrok_url, program_id):
141
  """프로그램 실행"""
 
156
  return f"프로그램 실행 중 오류 발생: {str(e)}"
157
 
158
  # UI 구성
159
+ with gr.Blocks(title="LocalPCAgent 제어 인터페이스") as demo:
160
  gr.Markdown("# LocalPCAgent 제어 인터페이스")
161
  gr.Markdown("로컬 PC의 기능을 원격으로 제어하고 관리할 수 있는 웹 인터페이스입니다.")
162
 
 
164
  server_url = gr.State("")
165
 
166
  # 서버 연결 섹션
167
+ with gr.Group():
168
  gr.Markdown("## 서버 연결")
169
  with gr.Row():
170
  ngrok_url_input = gr.Textbox(
 
179
  # 작업 메뉴 탭
180
  with gr.Tabs() as tabs:
181
  # 1. 기본 기능 탭
182
+ with gr.TabItem("기본 기능"):
183
+ with gr.Group():
184
  gr.Markdown("### 서버 상태 확인")
185
  check_status_btn = gr.Button("상태 확인", variant="primary")
186
  status_result = gr.Textbox(label="결과", interactive=False, lines=10)
187
 
188
+ with gr.Group():
189
  gr.Markdown("### 에코 테스트")
190
  echo_message = gr.Textbox(
191
  label="메시지",
 
196
  echo_result = gr.Textbox(label="결과", interactive=False, lines=10)
197
 
198
  # 2. 장치 관리 탭
199
+ with gr.TabItem("장치 관리"):
200
+ with gr.Group():
201
  gr.Markdown("### 장치 목록 조회")
202
  get_devices_btn = gr.Button("장치 목록 가져오기", variant="primary")
203
  devices_result = gr.Textbox(label="결과", interactive=False, lines=15)
204
 
205
+ with gr.Group():
206
  gr.Markdown("### 장치 상세 정보")
207
  device_dropdown = gr.Dropdown(label="장치 선택", choices=[], interactive=True)
208
  get_device_info_btn = gr.Button("장치 정보 가져오기", variant="primary")
209
  device_info_result = gr.Textbox(label="결과", interactive=False, lines=15)
210
 
211
  # 3. 프로그램 실행 탭
212
+ with gr.TabItem("프로그램 실행"):
213
+ with gr.Group():
214
  gr.Markdown("### 프로그램 목록 조회")
215
  get_programs_btn = gr.Button("프로그램 목록 가져오기", variant="primary")
216
  programs_result = gr.Textbox(label="결과", interactive=False, lines=15)
217
 
218
+ with gr.Group():
219
  gr.Markdown("### 프로그램 실행")
220
  program_dropdown = gr.Dropdown(label="프로그램 선택", choices=[], interactive=True)
221
  execute_program_btn = gr.Button("프로그램 실행", variant="primary")
 
242
  )
243
 
244
  # 장치 관리 이벤트
245
+ def update_device_dropdown(devices_result, choices):
246
+ if choices:
247
+ return gr.Dropdown.update(choices=choices)
248
+ return gr.Dropdown.update(choices=[])
249
+
250
  get_devices_btn.click(
251
  fn=get_devices,
252
  inputs=[server_url],
 
260
  )
261
 
262
  # 프로그램 실행 이벤트
263
+ def update_program_dropdown(programs_result, choices):
264
+ if choices:
265
+ return gr.Dropdown.update(choices=choices)
266
+ return gr.Dropdown.update(choices=[])
267
+
268
  get_programs_btn.click(
269
  fn=get_programs,
270
  inputs=[server_url],