ginipick commited on
Commit
f00b06e
ยท
verified ยท
1 Parent(s): c49fd90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -74
app.py CHANGED
@@ -67,56 +67,11 @@ def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
67
  formatted += format_tree_structure(child, indent + " ")
68
  return formatted
69
 
70
- def summarize_code(app_content: str) -> str:
71
- system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
72
- user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ์š”์•ฝํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
73
-
74
- messages = [
75
- {"role": "system", "content": system_message},
76
- {"role": "user", "content": user_message}
77
- ]
78
-
79
- try:
80
- response = hf_client.chat_completion(messages, max_tokens=200, temperature=0.7)
81
- return response.choices[0].message.content
82
- except Exception as e:
83
- return f"์š”์•ฝ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
84
 
85
- def analyze_code(app_content: str) -> str:
86
- system_message = """๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋‹ค์Œ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:
87
- A. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ
88
- B. ๊ธฐ๋Šฅ์  ํšจ์šฉ์„ฑ ๋ฐ ๊ฐ€์น˜
89
- C. ํŠน์žฅ์ 
90
- D. ์ ์šฉ ๋Œ€์ƒ ๋ฐ ํƒ€๊ฒŸ
91
- E. ๊ธฐ๋Œ€ํšจ๊ณผ
92
- ๊ธฐ์กด ๋ฐ ์œ ์‚ฌ ํ”„๋กœ์ ํŠธ์™€ ๋น„๊ตํ•˜์—ฌ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."""
93
- user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
94
-
95
- messages = [
96
- {"role": "system", "content": system_message},
97
- {"role": "user", "content": user_message}
98
- ]
99
-
100
- try:
101
- response = hf_client.chat_completion(messages, max_tokens=1000, temperature=0.7)
102
- return response.choices[0].message.content
103
- except Exception as e:
104
- return f"๋ถ„์„ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
105
 
106
- def explain_usage(app_content: str) -> str:
107
- system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋ฒ•์„ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."
108
- user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
109
-
110
- messages = [
111
- {"role": "system", "content": system_message},
112
- {"role": "user", "content": user_message}
113
- ]
114
-
115
- try:
116
- response = hf_client.chat_completion(messages, max_tokens=800, temperature=0.7)
117
- return response.choices[0].message.content
118
- except Exception as e:
119
- return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
120
 
121
  def analyze_space(url: str, progress=gr.Progress()):
122
  try:
@@ -177,6 +132,75 @@ def respond(
177
 
178
 
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  def create_ui():
181
  try:
182
  css = """
@@ -196,26 +220,31 @@ def create_ui():
196
  overflow-y: auto !important;
197
  }
198
  .tabs-style {
199
- background-color: #ffff00;
 
200
  }
201
- """
202
-
203
- js = """
204
- function openFile(path, spaceId) {
205
- const filePathInput = document.querySelector('input[data-testid="file_path_input"]');
206
- const spaceIdInput = document.querySelector('input[data-testid="space_id_input"]');
207
- if (filePathInput && spaceIdInput) {
208
- filePathInput.value = path;
209
- spaceIdInput.value = spaceId;
210
- filePathInput.dispatchEvent(new Event('change'));
211
- }
 
 
 
 
212
  }
213
  """
214
 
215
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
216
  gr.Markdown("# HuggingFace Space Analyzer")
217
 
218
- with gr.Tabs() as tabs:
219
  with gr.TabItem("๋ถ„์„"):
220
  with gr.Row():
221
  with gr.Column(scale=6): # ์™ผ์ชฝ 60%
@@ -246,7 +275,7 @@ def create_ui():
246
  app_py_content = gr.Code(language="python", label="app.py", lines=30)
247
  requirements_tab = gr.TabItem("requirements.txt")
248
  with requirements_tab:
249
- requirements_content = gr.Textbox(label="requirements.txt", lines=30)
250
 
251
  with gr.TabItem("AI ์ฝ”๋”ฉ"):
252
  chatbot = gr.Chatbot(label="๋Œ€ํ™”", type="messages")
@@ -296,26 +325,36 @@ def create_ui():
296
  files = get_files(tree_structure)
297
  buttons_html = "<div style='display: flex; flex-direction: column;'>"
298
  for file in files:
299
- buttons_html += f"<button onclick=\"openFile('{file['path']}', '{space_id}')\" style='margin: 5px 0; padding: 5px; cursor: pointer;'>{file['path']}</button>"
300
  buttons_html += "</div>"
301
  return buttons_html
302
 
303
  def open_file(file_path: str, space_id: str):
304
  content = get_file_content(space_id, file_path)
305
  file_name = file_path.split('/')[-1]
306
- if file_name == "requirements.txt":
307
- return gr.Tabs.update(selected="requirements.txt"), gr.Textbox.update(value=content, label=file_name)
308
- else:
309
- return gr.Tabs.update(selected=file_name), gr.Code.update(value=content, language="python" if file_name.endswith('.py') else "plaintext", label=file_name)
310
 
311
  analyze_button.click(
312
  analyze_space,
313
  inputs=[url_input],
314
- outputs=[summary_output, analysis_output, usage_output, app_py_content, tree_view_output, tree_structure_state, space_id_state]
315
  ).then(
316
  update_file_buttons,
317
  inputs=[tree_structure_state, space_id_state],
318
  outputs=[file_buttons]
 
 
 
 
 
 
 
 
 
 
 
 
319
  ).then(
320
  lambda space_id: get_file_content(space_id, "requirements.txt"),
321
  inputs=[space_id_state],
@@ -325,18 +364,12 @@ def create_ui():
325
  file_path_input = gr.Textbox(visible=False)
326
  space_id_input = gr.Textbox(visible=False)
327
 
328
- def handle_file_open(file_path, space_id):
329
- return file_path, space_id
330
-
331
  file_path_input.change(
332
  open_file,
333
  inputs=[file_path_input, space_id_input],
334
  outputs=[code_tabs, code_tabs]
335
  )
336
 
337
- # JavaScript ์ฝ”๋“œ๋ฅผ HTML์— ์ง์ ‘ ์‚ฝ์ž…
338
- gr.HTML(f"<script>{js}</script>")
339
-
340
  return demo
341
 
342
  except Exception as e:
 
67
  formatted += format_tree_structure(child, indent + " ")
68
  return formatted
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+
73
+
74
+
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  def analyze_space(url: str, progress=gr.Progress()):
77
  try:
 
132
 
133
 
134
 
135
+ def summarize_code(app_content: str):
136
+ system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
137
+ user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ์š”์•ฝํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
138
+
139
+ messages = [
140
+ {"role": "system", "content": system_message},
141
+ {"role": "user", "content": user_message}
142
+ ]
143
+
144
+ try:
145
+ for response in hf_client.chat_completion_stream(messages, max_tokens=200, temperature=0.7):
146
+ yield response.choices[0].delta.content
147
+ except Exception as e:
148
+ yield f"์š”์•ฝ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
149
+
150
+ def analyze_code(app_content: str):
151
+ system_message = """๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋‹ค์Œ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:
152
+ A. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ
153
+ B. ๊ธฐ๋Šฅ์  ํšจ์šฉ์„ฑ ๋ฐ ๊ฐ€์น˜
154
+ C. ํŠน์žฅ์ 
155
+ D. ์ ์šฉ ๋Œ€์ƒ ๋ฐ ํƒ€๊ฒŸ
156
+ E. ๊ธฐ๋Œ€ํšจ๊ณผ
157
+ ๊ธฐ์กด ๋ฐ ์œ ์‚ฌ ํ”„๋กœ์ ํŠธ์™€ ๋น„๊ตํ•˜์—ฌ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."""
158
+ user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
159
+
160
+ messages = [
161
+ {"role": "system", "content": system_message},
162
+ {"role": "user", "content": user_message}
163
+ ]
164
+
165
+ try:
166
+ for response in hf_client.chat_completion_stream(messages, max_tokens=1000, temperature=0.7):
167
+ yield response.choices[0].delta.content
168
+ except Exception as e:
169
+ yield f"๋ถ„์„ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
170
+
171
+ def explain_usage(app_content: str):
172
+ system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋ฒ•์„ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."
173
+ user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
174
+
175
+ messages = [
176
+ {"role": "system", "content": system_message},
177
+ {"role": "user", "content": user_message}
178
+ ]
179
+
180
+ try:
181
+ for response in hf_client.chat_completion_stream(messages, max_tokens=800, temperature=0.7):
182
+ yield response.choices[0].delta.content
183
+ except Exception as e:
184
+ yield f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
185
+
186
+ def analyze_space(url: str, progress=gr.Progress()):
187
+ try:
188
+ space_id = url.split('spaces/')[-1]
189
+
190
+ progress(0.1, desc="ํŒŒ์ผ ๊ตฌ์กฐ ๋ถ„์„ ์ค‘...")
191
+ tree_structure = get_space_structure(space_id)
192
+ tree_view = format_tree_structure(tree_structure)
193
+
194
+ progress(0.3, desc="app.py ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘...")
195
+ app_content = get_file_content(space_id, "app.py")
196
+
197
+ progress(1.0, desc="์™„๋ฃŒ")
198
+ return app_content, tree_view, tree_structure, space_id
199
+ except Exception as e:
200
+ print(f"Error in analyze_space: {str(e)}")
201
+ print(traceback.format_exc())
202
+ return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", None, ""
203
+
204
  def create_ui():
205
  try:
206
  css = """
 
220
  overflow-y: auto !important;
221
  }
222
  .tabs-style {
223
+ background-color: #ffff00 !important;
224
+ font-weight: bold !important;
225
  }
226
+ .main-tabs .tabitem[id^="tab_"] {
227
+ background-color: #ffff00 !important;
228
+ font-weight: bold !important;
229
+ }
230
+ .file-button {
231
+ background-color: #f0f0f0;
232
+ border: 1px solid #ddd;
233
+ padding: 5px 10px;
234
+ margin: 2px 0;
235
+ cursor: pointer;
236
+ text-align: left;
237
+ width: 100%;
238
+ }
239
+ .file-button:hover {
240
+ background-color: #e0e0e0;
241
  }
242
  """
243
 
244
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
245
  gr.Markdown("# HuggingFace Space Analyzer")
246
 
247
+ with gr.Tabs(elem_classes="main-tabs") as tabs:
248
  with gr.TabItem("๋ถ„์„"):
249
  with gr.Row():
250
  with gr.Column(scale=6): # ์™ผ์ชฝ 60%
 
275
  app_py_content = gr.Code(language="python", label="app.py", lines=30)
276
  requirements_tab = gr.TabItem("requirements.txt")
277
  with requirements_tab:
278
+ requirements_content = gr.Code(language="text", label="requirements.txt", lines=30)
279
 
280
  with gr.TabItem("AI ์ฝ”๋”ฉ"):
281
  chatbot = gr.Chatbot(label="๋Œ€ํ™”", type="messages")
 
325
  files = get_files(tree_structure)
326
  buttons_html = "<div style='display: flex; flex-direction: column;'>"
327
  for file in files:
328
+ buttons_html += f"<button class='file-button' onclick='openFile(\"{file['path']}\", \"{space_id}\")'>{file['path']}</button>"
329
  buttons_html += "</div>"
330
  return buttons_html
331
 
332
  def open_file(file_path: str, space_id: str):
333
  content = get_file_content(space_id, file_path)
334
  file_name = file_path.split('/')[-1]
335
+ language = "python" if file_name.endswith('.py') else "text"
336
+ return gr.Tabs.update(selected=file_name), gr.Code.update(value=content, language=language, label=file_name)
 
 
337
 
338
  analyze_button.click(
339
  analyze_space,
340
  inputs=[url_input],
341
+ outputs=[app_py_content, tree_view_output, tree_structure_state, space_id_state]
342
  ).then(
343
  update_file_buttons,
344
  inputs=[tree_structure_state, space_id_state],
345
  outputs=[file_buttons]
346
+ ).then(
347
+ summarize_code,
348
+ inputs=[app_py_content],
349
+ outputs=[summary_output]
350
+ ).then(
351
+ analyze_code,
352
+ inputs=[app_py_content],
353
+ outputs=[analysis_output]
354
+ ).then(
355
+ explain_usage,
356
+ inputs=[app_py_content],
357
+ outputs=[usage_output]
358
  ).then(
359
  lambda space_id: get_file_content(space_id, "requirements.txt"),
360
  inputs=[space_id_state],
 
364
  file_path_input = gr.Textbox(visible=False)
365
  space_id_input = gr.Textbox(visible=False)
366
 
 
 
 
367
  file_path_input.change(
368
  open_file,
369
  inputs=[file_path_input, space_id_input],
370
  outputs=[code_tabs, code_tabs]
371
  )
372
 
 
 
 
373
  return demo
374
 
375
  except Exception as e: