ginipick commited on
Commit
15cf0dc
ยท
verified ยท
1 Parent(s): 65d9911

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -89
app.py CHANGED
@@ -122,8 +122,6 @@ def explain_usage(app_content: str):
122
  except Exception as e:
123
  return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
124
 
125
-
126
-
127
  def analyze_space(url: str, progress=gr.Progress()):
128
  try:
129
  space_id = url.split('spaces/')[-1]
@@ -157,16 +155,13 @@ def analyze_space(url: str, progress=gr.Progress()):
157
  print(traceback.format_exc())
158
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", None, "", "", "", ""
159
 
160
-
161
  def respond(message: str, chat_history: List[Dict[str, str]], max_tokens: int, temperature: float, top_p: float) -> str:
162
  system_message = """๋‹น์‹ ์€ ํ—ˆ๊น…ํŽ˜์ด์Šค์— ํŠนํ™”๋œ AI ์ฝ”๋”ฉ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ์นœ์ ˆํ•˜๊ณ  ์ƒ์„ธํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”.
163
  Gradio ํŠน์„ฑ์„ ์ •ํ™•ํžˆ ์ธ์‹ํ•˜๊ณ  Requirements.txt ๋ˆ„๋ฝ์—†์ด ์ฝ”๋”ฉ๊ณผ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
164
  ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ๋…ธ๋ ฅํ•˜์„ธ์š”."""
165
 
166
  messages = [{"role": "system", "content": system_message}]
167
- for chat in chat_history:
168
- messages.append({"role": "user", "content": chat["content"]})
169
- messages.append({"role": "assistant", "content": chat["content"]})
170
  messages.append({"role": "user", "content": message})
171
 
172
  try:
@@ -205,6 +200,8 @@ def create_ui():
205
  padding: 10px 20px;
206
  margin: 0;
207
  transition: background-color 0.3s;
 
 
208
  }
209
  .tab-nav button:hover {
210
  background-color: #2980b9;
@@ -212,19 +209,6 @@ def create_ui():
212
  .tab-nav button.selected {
213
  color: #2c3e50 !important;
214
  background-color: #ecf0f1;
215
- font-weight: bold;
216
- }
217
- .file-button {
218
- background-color: #f0f0f0;
219
- border: 1px solid #ddd;
220
- padding: 5px 10px;
221
- margin: 2px 0;
222
- cursor: pointer;
223
- text-align: left;
224
- width: 100%;
225
- }
226
- .file-button:hover {
227
- background-color: #e0e0e0;
228
  }
229
  input[type="text"], textarea {
230
  color: #2c3e50 !important;
@@ -232,11 +216,10 @@ def create_ui():
232
  }
233
  """
234
 
235
-
236
- with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
237
  gr.Markdown("# HuggingFace Space Analyzer")
238
 
239
- with gr.Tabs(elem_classes="main-tabs") as tabs:
240
  with gr.TabItem("๋ถ„์„"):
241
  with gr.Row():
242
  with gr.Column(scale=6): # ์™ผ์ชฝ 60%
@@ -254,13 +237,10 @@ def create_ui():
254
 
255
  with gr.Group(elem_classes="output-group scroll-lock"):
256
  tree_view_output = gr.Textbox(label="ํŒŒ์ผ ๊ตฌ์กฐ (Tree View)", lines=20)
257
-
258
- with gr.Group(elem_classes="output-group scroll-lock"):
259
- file_buttons = gr.HTML(label="ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ")
260
 
261
  with gr.Column(scale=4): # ์˜ค๋ฅธ์ชฝ 40%
262
  with gr.Group(elem_classes="output-group full-height"):
263
- code_tabs = gr.Tabs(elem_classes="code-tabs")
264
  with code_tabs:
265
  app_py_tab = gr.TabItem("app.py")
266
  with app_py_tab:
@@ -270,7 +250,7 @@ def create_ui():
270
  requirements_content = gr.Textbox(label="requirements.txt", lines=30)
271
 
272
  with gr.TabItem("AI ์ฝ”๋”ฉ"):
273
- chatbot = gr.Chatbot(label="๋Œ€ํ™”", type="messages") # ์—ฌ๊ธฐ๋ฅผ 'messages'๋กœ ์ˆ˜์ •
274
  msg = gr.Textbox(label="๋ฉ”์‹œ์ง€")
275
 
276
  # ์ˆจ๊ฒจ์ง„ ์ƒํƒœ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ •
@@ -290,82 +270,27 @@ def create_ui():
290
  gr.Examples(examples, inputs=msg)
291
 
292
  def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
 
293
  bot_message = respond(message, chat_history, max_tokens, temperature, top_p)
294
- chat_history.append((message, bot_message)) # ํŠœํ”Œ ํ˜•์‹์œผ๋กœ ๋ณ€๊ฒฝ
 
295
  return "", chat_history
296
 
297
  msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
298
 
299
-
300
  space_id_state = gr.State()
301
  tree_structure_state = gr.State()
302
 
303
- def update_file_buttons(tree_structure, space_id):
304
- if tree_structure is None:
305
- return ""
306
-
307
- def get_files(node):
308
- files = []
309
- if node["type"] == "file":
310
- files.append(node)
311
- elif node["type"] == "directory":
312
- for child in node.get("children", []):
313
- files.extend(get_files(child))
314
- return files
315
-
316
- files = get_files(tree_structure)
317
- buttons_html = "<div style='display: flex; flex-direction: column;'>"
318
- for file in files:
319
- buttons_html += f"<button class='file-button' onclick='openFile(\"{file['path']}\", \"{space_id}\")'>{file['path']}</button>"
320
- buttons_html += "</div>"
321
- return buttons_html
322
-
323
- def open_file(file_path: str, space_id: str):
324
- content = get_file_content(space_id, file_path)
325
- file_name = file_path.split('/')[-1]
326
- if file_name == "requirements.txt":
327
- return gr.Tabs.update(selected="requirements.txt"), gr.Textbox.update(value=content, label=file_name)
328
- else:
329
- return gr.Tabs.update(selected=file_name), gr.Code.update(value=content, language="python" if file_name.endswith('.py') else "plaintext", label=file_name)
330
-
331
  analyze_button.click(
332
  analyze_space,
333
  inputs=[url_input],
334
  outputs=[app_py_content, tree_view_output, tree_structure_state, space_id_state, summary_output, analysis_output, usage_output]
335
- ).then(
336
- update_file_buttons,
337
- inputs=[tree_structure_state, space_id_state],
338
- outputs=[file_buttons]
339
  ).then(
340
  lambda space_id: get_file_content(space_id, "requirements.txt"),
341
  inputs=[space_id_state],
342
  outputs=[requirements_content]
343
  )
344
 
345
- file_path_input = gr.Textbox(visible=False)
346
- space_id_input = gr.Textbox(visible=False)
347
-
348
- file_path_input.change(
349
- open_file,
350
- inputs=[file_path_input, space_id_input],
351
- outputs=[code_tabs, code_tabs]
352
- )
353
-
354
- # JavaScript ์ฝ”๋“œ๋ฅผ HTML์— ์ง์ ‘ ์‚ฝ์ž…
355
- gr.HTML("""
356
- <script>
357
- function openFile(path, spaceId) {
358
- const filePathInput = document.querySelector('input[data-testid="file_path_input"]');
359
- const spaceIdInput = document.querySelector('input[data-testid="space_id_input"]');
360
- if (filePathInput && spaceIdInput) {
361
- filePathInput.value = path;
362
- spaceIdInput.value = spaceId;
363
- filePathInput.dispatchEvent(new Event('change'));
364
- }
365
- }
366
- </script>
367
- """)
368
-
369
  return demo
370
 
371
  except Exception as e:
@@ -373,7 +298,6 @@ def create_ui():
373
  print(traceback.format_exc())
374
  raise
375
 
376
-
377
  if __name__ == "__main__":
378
  try:
379
  print("Starting HuggingFace Space Analyzer...")
@@ -386,8 +310,8 @@ if __name__ == "__main__":
386
 
387
  print("Launching Gradio app...")
388
  demo.launch(
389
- server_name="0.0.0.0", # ๋ชจ๋“  ๋„คํŠธ์›Œํฌ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •
390
- server_port=7860, # ๊ธฐ๋ณธ Gradio ํฌํŠธ
391
  share=False,
392
  debug=True,
393
  show_api=False
@@ -399,4 +323,4 @@ if __name__ == "__main__":
399
  print(traceback.format_exc())
400
  raise
401
 
402
- print("Script execution completed.")
 
122
  except Exception as e:
123
  return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
124
 
 
 
125
  def analyze_space(url: str, progress=gr.Progress()):
126
  try:
127
  space_id = url.split('spaces/')[-1]
 
155
  print(traceback.format_exc())
156
  return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", None, "", "", "", ""
157
 
 
158
  def respond(message: str, chat_history: List[Dict[str, str]], max_tokens: int, temperature: float, top_p: float) -> str:
159
  system_message = """๋‹น์‹ ์€ ํ—ˆ๊น…ํŽ˜์ด์Šค์— ํŠนํ™”๋œ AI ์ฝ”๋”ฉ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ์นœ์ ˆํ•˜๊ณ  ์ƒ์„ธํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”.
160
  Gradio ํŠน์„ฑ์„ ์ •ํ™•ํžˆ ์ธ์‹ํ•˜๊ณ  Requirements.txt ๋ˆ„๋ฝ์—†์ด ์ฝ”๋”ฉ๊ณผ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
161
  ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ๋…ธ๋ ฅํ•˜์„ธ์š”."""
162
 
163
  messages = [{"role": "system", "content": system_message}]
164
+ messages.extend(chat_history)
 
 
165
  messages.append({"role": "user", "content": message})
166
 
167
  try:
 
200
  padding: 10px 20px;
201
  margin: 0;
202
  transition: background-color 0.3s;
203
+ font-size: 16px;
204
+ font-weight: bold;
205
  }
206
  .tab-nav button:hover {
207
  background-color: #2980b9;
 
209
  .tab-nav button.selected {
210
  color: #2c3e50 !important;
211
  background-color: #ecf0f1;
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  }
213
  input[type="text"], textarea {
214
  color: #2c3e50 !important;
 
216
  }
217
  """
218
 
219
+ with gr.Blocks(css=css, theme=gr.themes.Default()) as demo:
 
220
  gr.Markdown("# HuggingFace Space Analyzer")
221
 
222
+ with gr.Tabs() as tabs:
223
  with gr.TabItem("๋ถ„์„"):
224
  with gr.Row():
225
  with gr.Column(scale=6): # ์™ผ์ชฝ 60%
 
237
 
238
  with gr.Group(elem_classes="output-group scroll-lock"):
239
  tree_view_output = gr.Textbox(label="ํŒŒ์ผ ๊ตฌ์กฐ (Tree View)", lines=20)
 
 
 
240
 
241
  with gr.Column(scale=4): # ์˜ค๋ฅธ์ชฝ 40%
242
  with gr.Group(elem_classes="output-group full-height"):
243
+ code_tabs = gr.Tabs()
244
  with code_tabs:
245
  app_py_tab = gr.TabItem("app.py")
246
  with app_py_tab:
 
250
  requirements_content = gr.Textbox(label="requirements.txt", lines=30)
251
 
252
  with gr.TabItem("AI ์ฝ”๋”ฉ"):
253
+ chatbot = gr.Chatbot(label="๋Œ€ํ™”")
254
  msg = gr.Textbox(label="๋ฉ”์‹œ์ง€")
255
 
256
  # ์ˆจ๊ฒจ์ง„ ์ƒํƒœ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ •
 
270
  gr.Examples(examples, inputs=msg)
271
 
272
  def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
273
+
274
  bot_message = respond(message, chat_history, max_tokens, temperature, top_p)
275
+ chat_history.append({"role": "user", "content": message})
276
+ chat_history.append({"role": "assistant", "content": bot_message})
277
  return "", chat_history
278
 
279
  msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
280
 
 
281
  space_id_state = gr.State()
282
  tree_structure_state = gr.State()
283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  analyze_button.click(
285
  analyze_space,
286
  inputs=[url_input],
287
  outputs=[app_py_content, tree_view_output, tree_structure_state, space_id_state, summary_output, analysis_output, usage_output]
 
 
 
 
288
  ).then(
289
  lambda space_id: get_file_content(space_id, "requirements.txt"),
290
  inputs=[space_id_state],
291
  outputs=[requirements_content]
292
  )
293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  return demo
295
 
296
  except Exception as e:
 
298
  print(traceback.format_exc())
299
  raise
300
 
 
301
  if __name__ == "__main__":
302
  try:
303
  print("Starting HuggingFace Space Analyzer...")
 
310
 
311
  print("Launching Gradio app...")
312
  demo.launch(
313
+ server_name="0.0.0.0",
314
+ server_port=7860,
315
  share=False,
316
  debug=True,
317
  show_api=False
 
323
  print(traceback.format_exc())
324
  raise
325
 
326
+ print("Script execution completed.")