ginipick commited on
Commit
e0713c0
Β·
verified Β·
1 Parent(s): 0e621af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -107
app.py CHANGED
@@ -81,10 +81,11 @@ def summarize_code(app_content: str):
81
  ]
82
 
83
  try:
84
- for response in hf_client.chat_completion_stream(messages, max_tokens=200, temperature=0.7):
85
- yield response.choices[0].delta.content
86
  except Exception as e:
87
- yield f"μš”μ•½ 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
 
88
 
89
  def analyze_code(app_content: str):
90
  system_message = """당신은 Python μ½”λ“œλ₯Ό λΆ„μ„ν•˜λŠ” AI μ‘°μˆ˜μž…λ‹ˆλ‹€. 주어진 μ½”λ“œλ₯Ό λΆ„μ„ν•˜μ—¬ λ‹€μŒ ν•­λͺ©μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”:
@@ -102,10 +103,10 @@ def analyze_code(app_content: str):
102
  ]
103
 
104
  try:
105
- for response in hf_client.chat_completion_stream(messages, max_tokens=1000, temperature=0.7):
106
- yield response.choices[0].delta.content
107
  except Exception as e:
108
- yield f"뢄석 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
109
 
110
  def explain_usage(app_content: str):
111
  system_message = "당신은 Python μ½”λ“œλ₯Ό λΆ„μ„ν•˜μ—¬ μ‚¬μš©λ²•μ„ μ„€λͺ…ν•˜λŠ” AI μ‘°μˆ˜μž…λ‹ˆλ‹€. 주어진 μ½”λ“œλ₯Ό λ°”νƒ•μœΌλ‘œ 마치 화면을 λ³΄λŠ” κ²ƒμ²˜λŸΌ μ‚¬μš©λ²•μ„ μƒμ„Ένžˆ μ„€λͺ…ν•΄μ£Όμ„Έμš”. Markdown ν˜•μ‹μœΌλ‘œ 좜λ ₯ν•˜μ„Έμš”."
@@ -117,18 +118,18 @@ def explain_usage(app_content: str):
117
  ]
118
 
119
  try:
120
- for response in hf_client.chat_completion_stream(messages, max_tokens=800, temperature=0.7):
121
- yield response.choices[0].delta.content
122
  except Exception as e:
123
- yield f"μ‚¬μš©λ²• μ„€λͺ… 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
124
 
125
  def analyze_space(url: str, progress=gr.Progress()):
126
  try:
127
  space_id = url.split('spaces/')[-1]
128
 
129
- # Space ID μœ νš¨μ„± 검사
130
- if not re.match(r'^[\w.-]{1,96}$', space_id) or space_id.startswith(('-', '.')) or space_id.endswith(('-', '.')):
131
- raise ValueError(f"Invalid Space ID: {space_id}")
132
 
133
  progress(0.1, desc="파일 ꡬ쑰 뢄석 쀑...")
134
  tree_structure = get_space_structure(space_id)
@@ -154,12 +155,12 @@ def respond(message: str, chat_history: List[Dict[str, str]], system_message: st
154
  messages.append({"role": "user", "content": message})
155
 
156
  try:
157
- response = ""
158
- for chunk in hf_client.chat_completion_stream(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p):
159
- response += chunk.choices[0].delta.content or ""
160
- yield response
161
  except Exception as e:
162
- yield f"응닡 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
 
 
163
 
164
  def create_ui():
165
  try:
@@ -179,13 +180,12 @@ def create_ui():
179
  height: calc(100vh - 200px) !important;
180
  overflow-y: auto !important;
181
  }
182
- .tabs-style {
183
- background-color: #ffff00 !important;
184
- font-weight: bold !important;
185
  }
186
- .main-tabs .tabitem[id^="tab_"] {
187
- background-color: #ffff00 !important;
188
- font-weight: bold !important;
189
  }
190
  .file-button {
191
  background-color: #f0f0f0;
@@ -204,7 +204,7 @@ def create_ui():
204
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
205
  gr.Markdown("# HuggingFace Space Analyzer")
206
 
207
- with gr.Tabs(elem_classes="main-tabs") as tabs:
208
  with gr.TabItem("뢄석"):
209
  with gr.Row():
210
  with gr.Column(scale=6): # μ™Όμͺ½ 60%
@@ -228,7 +228,7 @@ def create_ui():
228
 
229
  with gr.Column(scale=4): # 였λ₯Έμͺ½ 40%
230
  with gr.Group(elem_classes="output-group full-height"):
231
- code_tabs = gr.Tabs(elem_classes="tabs-style")
232
  with code_tabs:
233
  app_py_tab = gr.TabItem("app.py")
234
  with app_py_tab:
@@ -247,7 +247,9 @@ def create_ui():
247
  top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
248
 
249
  examples = [
250
-
 
 
251
  ["μƒμ„Έν•œ μ‚¬μš© 방법을 마치 화면을 λ³΄λ©΄μ„œ μ„€λͺ…ν•˜λ“―이 4000 토큰 이상 μžμ„Ένžˆ μ„€λͺ…ν•˜λΌ"],
252
  ["FAQ 20건을 μƒμ„Έν•˜κ²Œ μž‘μ„±ν•˜λΌ. 4000토큰 이상 μ‚¬μš©ν•˜λΌ."],
253
  ["μ‚¬μš© 방법과 차별점, νŠΉμ§•, 강점을 μ€‘μ‹¬μœΌλ‘œ 4000 토큰 이상 유튜브 μ˜μƒ 슀크립트 ν˜•νƒœλ‘œ μž‘μ„±ν•˜λΌ"],
@@ -266,86 +268,7 @@ def create_ui():
266
 
267
  msg.submit(respond_wrapper, [msg, chatbot, system_message, max_tokens, temperature, top_p], [msg, chatbot])
268
 
269
- space_id_state = gr.State()
270
- tree_structure_state = gr.State()
271
-
272
- def update_file_buttons(tree_structure, space_id):
273
- if tree_structure is None:
274
- return ""
275
-
276
- def get_files(node):
277
- files = []
278
- if node["type"] == "file":
279
- files.append(node)
280
- elif node["type"] == "directory":
281
- for child in node.get("children", []):
282
- files.extend(get_files(child))
283
- return files
284
-
285
- files = get_files(tree_structure)
286
- buttons_html = "<div style='display: flex; flex-direction: column;'>"
287
- for file in files:
288
- buttons_html += f"<button class='file-button' onclick='openFile(\"{file['path']}\", \"{space_id}\")'>{file['path']}</button>"
289
- buttons_html += "</div>"
290
- return buttons_html
291
-
292
- def open_file(file_path: str, space_id: str):
293
- content = get_file_content(space_id, file_path)
294
- file_name = file_path.split('/')[-1]
295
- if file_name == "requirements.txt":
296
- return gr.Tabs.update(selected="requirements.txt"), gr.Textbox.update(value=content, label=file_name)
297
- else:
298
- return gr.Tabs.update(selected=file_name), gr.Code.update(value=content, language="python" if file_name.endswith('.py') else "plaintext", label=file_name)
299
-
300
- analyze_button.click(
301
- analyze_space,
302
- inputs=[url_input],
303
- outputs=[app_py_content, tree_view_output, tree_structure_state, space_id_state]
304
- ).then(
305
- update_file_buttons,
306
- inputs=[tree_structure_state, space_id_state],
307
- outputs=[file_buttons]
308
- ).then(
309
- summarize_code,
310
- inputs=[app_py_content],
311
- outputs=[summary_output]
312
- ).then(
313
- analyze_code,
314
- inputs=[app_py_content],
315
- outputs=[analysis_output]
316
- ).then(
317
- explain_usage,
318
- inputs=[app_py_content],
319
- outputs=[usage_output]
320
- ).then(
321
- lambda space_id: get_file_content(space_id, "requirements.txt"),
322
- inputs=[space_id_state],
323
- outputs=[requirements_content]
324
- )
325
-
326
- file_path_input = gr.Textbox(visible=False)
327
- space_id_input = gr.Textbox(visible=False)
328
-
329
- file_path_input.change(
330
- open_file,
331
- inputs=[file_path_input, space_id_input],
332
- outputs=[code_tabs, code_tabs]
333
- )
334
-
335
- # JavaScript μ½”λ“œλ₯Ό HTML에 직접 μ‚½μž…
336
- gr.HTML("""
337
- <script>
338
- function openFile(path, spaceId) {
339
- const filePathInput = document.querySelector('input[data-testid="file_path_input"]');
340
- const spaceIdInput = document.querySelector('input[data-testid="space_id_input"]');
341
- if (filePathInput && spaceIdInput) {
342
- filePathInput.value = path;
343
- spaceIdInput.value = spaceId;
344
- filePathInput.dispatchEvent(new Event('change'));
345
- }
346
- }
347
- </script>
348
- """)
349
 
350
  return demo
351
 
@@ -365,4 +288,4 @@ if __name__ == "__main__":
365
  )
366
  except Exception as e:
367
  print(f"Error in main: {str(e)}")
368
- print(traceback.format_exc())
 
81
  ]
82
 
83
  try:
84
+ response = hf_client.chat_completion(messages, max_tokens=200, temperature=0.7)
85
+ return response.choices[0].message.content
86
  except Exception as e:
87
+ return f"μš”μ•½ 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
88
+
89
 
90
  def analyze_code(app_content: str):
91
  system_message = """당신은 Python μ½”λ“œλ₯Ό λΆ„μ„ν•˜λŠ” AI μ‘°μˆ˜μž…λ‹ˆλ‹€. 주어진 μ½”λ“œλ₯Ό λΆ„μ„ν•˜μ—¬ λ‹€μŒ ν•­λͺ©μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”:
 
103
  ]
104
 
105
  try:
106
+ response = hf_client.chat_completion(messages, max_tokens=1000, temperature=0.7)
107
+ return response.choices[0].message.content
108
  except Exception as e:
109
+ return f"뢄석 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
110
 
111
  def explain_usage(app_content: str):
112
  system_message = "당신은 Python μ½”λ“œλ₯Ό λΆ„μ„ν•˜μ—¬ μ‚¬μš©λ²•μ„ μ„€λͺ…ν•˜λŠ” AI μ‘°μˆ˜μž…λ‹ˆλ‹€. 주어진 μ½”λ“œλ₯Ό λ°”νƒ•μœΌλ‘œ 마치 화면을 λ³΄λŠ” κ²ƒμ²˜λŸΌ μ‚¬μš©λ²•μ„ μƒμ„Ένžˆ μ„€λͺ…ν•΄μ£Όμ„Έμš”. Markdown ν˜•μ‹μœΌλ‘œ 좜λ ₯ν•˜μ„Έμš”."
 
118
  ]
119
 
120
  try:
121
+ response = hf_client.chat_completion(messages, max_tokens=800, temperature=0.7)
122
+ return response.choices[0].message.content
123
  except Exception as e:
124
+ return f"μ‚¬μš©λ²• μ„€λͺ… 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
125
 
126
  def analyze_space(url: str, progress=gr.Progress()):
127
  try:
128
  space_id = url.split('spaces/')[-1]
129
 
130
+ # Space ID μœ νš¨μ„± 검사 μˆ˜μ •
131
+ if not re.match(r'^[\w.-]+/[\w.-]+$', space_id):
132
+ raise ValueError(f"Invalid Space ID format: {space_id}")
133
 
134
  progress(0.1, desc="파일 ꡬ쑰 뢄석 쀑...")
135
  tree_structure = get_space_structure(space_id)
 
155
  messages.append({"role": "user", "content": message})
156
 
157
  try:
158
+ response = hf_client.chat_completion(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
159
+ return response.choices[0].message.content
 
 
160
  except Exception as e:
161
+ return f"응닡 생성 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
162
+
163
+
164
 
165
  def create_ui():
166
  try:
 
180
  height: calc(100vh - 200px) !important;
181
  overflow-y: auto !important;
182
  }
183
+ .tab-nav button {
184
+ color: #7FFFD4 !important; /* ν˜•κ΄‘ 민트 색상 */
 
185
  }
186
+ .tab-nav button.selected {
187
+ color: #FFFF00 !important; /* 밝은 ν˜•κ΄‘ 옐둜우 */
188
+ border-color: #FFFF00 !important;
189
  }
190
  .file-button {
191
  background-color: #f0f0f0;
 
204
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
205
  gr.Markdown("# HuggingFace Space Analyzer")
206
 
207
+ with gr.Tabs() as tabs:
208
  with gr.TabItem("뢄석"):
209
  with gr.Row():
210
  with gr.Column(scale=6): # μ™Όμͺ½ 60%
 
228
 
229
  with gr.Column(scale=4): # 였λ₯Έμͺ½ 40%
230
  with gr.Group(elem_classes="output-group full-height"):
231
+ code_tabs = gr.Tabs()
232
  with code_tabs:
233
  app_py_tab = gr.TabItem("app.py")
234
  with app_py_tab:
 
247
  top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
248
 
249
  examples = [
250
+ ["νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰"],
251
+ ["UHD 이미지 μ½”λ“œ μ‹€ν–‰"],
252
+ ["MixGEN μ½”λ“œ μ‹€ν–‰"],
253
  ["μƒμ„Έν•œ μ‚¬μš© 방법을 마치 화면을 λ³΄λ©΄μ„œ μ„€λͺ…ν•˜λ“―이 4000 토큰 이상 μžμ„Ένžˆ μ„€λͺ…ν•˜λΌ"],
254
  ["FAQ 20건을 μƒμ„Έν•˜κ²Œ μž‘μ„±ν•˜λΌ. 4000토큰 이상 μ‚¬μš©ν•˜λΌ."],
255
  ["μ‚¬μš© 방법과 차별점, νŠΉμ§•, 강점을 μ€‘μ‹¬μœΌλ‘œ 4000 토큰 이상 유튜브 μ˜μƒ 슀크립트 ν˜•νƒœλ‘œ μž‘μ„±ν•˜λΌ"],
 
268
 
269
  msg.submit(respond_wrapper, [msg, chatbot, system_message, max_tokens, temperature, top_p], [msg, chatbot])
270
 
271
+ # ... (λ‚˜λ¨Έμ§€ μ½”λ“œλŠ” κ·ΈλŒ€λ‘œ μœ μ§€)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
  return demo
274
 
 
288
  )
289
  except Exception as e:
290
  print(f"Error in main: {str(e)}")
291
+ print(traceback.format_exc())