ginipick commited on
Commit
4b231fa
ยท
verified ยท
1 Parent(s): 0276d24

Update app-backup10.py

Browse files
Files changed (1) hide show
  1. app-backup10.py +190 -95
app-backup10.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient, HfApi
3
  import os
4
  import requests
5
- from typing import List, Dict, Union
6
  import traceback
7
  from PIL import Image
8
  from io import BytesIO
@@ -11,6 +11,7 @@ from gradio_client import Client
11
  import time
12
  import threading
13
  import json
 
14
 
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
@@ -61,13 +62,16 @@ def get_space_structure(space_id: str) -> Dict:
61
  return {"error": f"API request error: {str(e)}"}
62
 
63
  def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
64
- formatted = f"{indent}{tree_data['name']}\n"
65
- if tree_data["type"] == "directory":
66
- for child in sorted(tree_data.get("children", []), key=lambda x: (x["type"] != "directory", x["name"])):
 
 
 
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
 
@@ -82,7 +86,7 @@ def summarize_code(app_content: str) -> str:
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. ๊ธฐ๋Šฅ์  ํšจ์šฉ์„ฑ ๋ฐ ๊ฐ€์น˜
@@ -103,7 +107,7 @@ def analyze_code(app_content: str) -> str:
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
 
@@ -118,33 +122,73 @@ def explain_usage(app_content: str) -> str:
118
  except Exception as e:
119
  return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  def analyze_space(url: str, progress=gr.Progress()):
122
  try:
123
  space_id = url.split('spaces/')[-1]
124
 
 
 
 
 
125
  progress(0.1, desc="ํŒŒ์ผ ๊ตฌ์กฐ ๋ถ„์„ ์ค‘...")
126
  tree_structure = get_space_structure(space_id)
 
 
127
  tree_view = format_tree_structure(tree_structure)
128
 
129
  progress(0.3, desc="app.py ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘...")
130
  app_content = get_file_content(space_id, "app.py")
131
 
132
- progress(0.4, desc="์ฝ”๋“œ ์š”์•ฝ ์ค‘...")
133
  summary = summarize_code(app_content)
134
 
135
- progress(0.6, desc="์ฝ”๋“œ ๋ถ„์„ ์ค‘...")
136
  analysis = analyze_code(app_content)
137
 
138
- progress(0.8, desc="์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘...")
139
  usage = explain_usage(app_content)
140
 
 
 
 
141
  progress(1.0, desc="์™„๋ฃŒ")
142
- return summary, analysis, usage, app_content, tree_view, tree_structure, space_id
143
  except Exception as e:
144
  print(f"Error in analyze_space: {str(e)}")
145
  print(traceback.format_exc())
146
- return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", "", "", "", None, ""
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  def create_ui():
150
  try:
@@ -160,107 +204,142 @@ def create_ui():
160
  overflow-y: auto !important;
161
  max-height: calc((100vh - 200px) / 5) !important;
162
  }
 
 
 
 
163
  .full-height {
164
- height: calc(100vh - 200px) !important;
165
  overflow-y: auto !important;
166
  }
167
- """
168
-
169
- js = """
170
- function openFile(path, spaceId) {
171
- const filePathInput = document.querySelector('input[data-testid="file_path_input"]');
172
- const spaceIdInput = document.querySelector('input[data-testid="space_id_input"]');
173
- if (filePathInput && spaceIdInput) {
174
- filePathInput.value = path;
175
- spaceIdInput.value = spaceId;
176
- filePathInput.dispatchEvent(new Event('change'));
177
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  }
179
  """
180
 
181
- with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
182
- gr.Markdown("# HuggingFace Space Analyzer")
183
 
184
- with gr.Row():
185
- with gr.Column(scale=6): # ์™ผ์ชฝ 60%
186
- url_input = gr.Textbox(label="HuggingFace Space URL")
187
- analyze_button = gr.Button("๋ถ„์„")
188
-
189
- with gr.Group(elem_classes="output-group scroll-lock"):
190
- summary_output = gr.Markdown(label="์š”์•ฝ (3์ค„ ์ด๋‚ด)")
191
-
192
- with gr.Group(elem_classes="output-group scroll-lock"):
193
- analysis_output = gr.Markdown(label="๋ถ„์„")
194
-
195
- with gr.Group(elem_classes="output-group scroll-lock"):
196
- usage_output = gr.Markdown(label="์‚ฌ์šฉ๋ฒ•")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
- with gr.Group(elem_classes="output-group scroll-lock"):
199
- tree_view_output = gr.Textbox(label="ํŒŒ์ผ ๊ตฌ์กฐ (Tree View)", lines=20)
 
 
200
 
201
- with gr.Group(elem_classes="output-group scroll-lock"):
202
- file_buttons = gr.Dataframe(
203
- headers=["ํŒŒ์ผ ์ด๋ฆ„", "์—ด๊ธฐ"],
204
- datatype=["str", "html"],
205
- col_count=(2, "fixed"),
206
- label="ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ"
207
- )
208
-
209
- with gr.Column(scale=4): # ์˜ค๋ฅธ์ชฝ 40%
210
- with gr.Group(elem_classes="output-group full-height"):
211
- code_tabs = gr.Tabs()
212
- with code_tabs:
213
- app_py_tab = gr.TabItem("app.py")
214
- with app_py_tab:
215
- app_py_content = gr.Code(language="python", label="app.py", lines=30)
216
 
217
- space_id_state = gr.State()
218
- tree_structure_state = gr.State()
219
 
220
- def update_file_buttons(tree_structure, space_id):
221
- if tree_structure is None:
222
- return []
223
-
224
- def get_files(node):
225
- files = []
226
- if node["type"] == "file":
227
- files.append(node)
228
- elif node["type"] == "directory":
229
- for child in node.get("children", []):
230
- files.extend(get_files(child))
231
- return files
232
 
233
- files = get_files(tree_structure)
234
- return [[file["path"], f'<button onclick="openFile(\'{file["path"]}\', \'{space_id}\')">์—ด๊ธฐ</button>'] for file in files]
235
 
236
- def open_file(file_path: str, space_id: str):
237
- content = get_file_content(space_id, file_path)
238
- return gr.Tabs.update(selected=file_path), gr.Code(value=content, language="python", label=file_path, lines=30)
239
 
240
  analyze_button.click(
241
  analyze_space,
242
  inputs=[url_input],
243
- outputs=[summary_output, analysis_output, usage_output, app_py_content, tree_view_output, tree_structure_state, space_id_state]
244
  ).then(
245
- update_file_buttons,
246
- inputs=[tree_structure_state, space_id_state],
247
- outputs=[file_buttons]
248
- )
249
-
250
- file_path_input = gr.Textbox(visible=False)
251
- space_id_input = gr.Textbox(visible=False)
252
-
253
- def handle_file_open(file_path, space_id):
254
- return file_path, space_id
255
-
256
- file_path_input.change(
257
- open_file,
258
- inputs=[file_path_input, space_id_input],
259
- outputs=[code_tabs, code_tabs]
260
  )
261
 
262
- # JavaScript ์ฝ”๋“œ๋ฅผ HTML์— ์ง์ ‘ ์‚ฝ์ž…
263
- gr.HTML(f"<script>{js}</script>")
264
 
265
  return demo
266
 
@@ -271,9 +350,25 @@ def create_ui():
271
 
272
  if __name__ == "__main__":
273
  try:
 
274
  demo = create_ui()
 
 
 
275
  demo.queue()
276
- demo.launch()
 
 
 
 
 
 
 
 
 
 
277
  except Exception as e:
278
  print(f"Error in main: {str(e)}")
279
- print(traceback.format_exc())
 
 
 
2
  from huggingface_hub import InferenceClient, HfApi
3
  import os
4
  import requests
5
+ from typing import List, Dict, Union, Tuple
6
  import traceback
7
  from PIL import Image
8
  from io import BytesIO
 
11
  import time
12
  import threading
13
  import json
14
+ import re
15
 
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
 
62
  return {"error": f"API request error: {str(e)}"}
63
 
64
  def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
65
+ if "error" in tree_data:
66
+ return tree_data["error"]
67
+
68
+ formatted = f"{indent}{'๐Ÿ“' if tree_data.get('type') == 'directory' else '๐Ÿ“„'} {tree_data.get('name', 'Unknown')}\n"
69
+ if tree_data.get("type") == "directory":
70
+ for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
71
  formatted += format_tree_structure(child, indent + " ")
72
  return formatted
73
 
74
+ def summarize_code(app_content: str):
75
  system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
76
  user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ์š”์•ฝํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
77
 
 
86
  except Exception as e:
87
  return f"์š”์•ฝ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
88
 
89
+ def analyze_code(app_content: str):
90
  system_message = """๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋‹ค์Œ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:
91
  A. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ
92
  B. ๊ธฐ๋Šฅ์  ํšจ์šฉ์„ฑ ๋ฐ ๊ฐ€์น˜
 
107
  except Exception as e:
108
  return f"๋ถ„์„ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
109
 
110
+ def explain_usage(app_content: str):
111
  system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋ฒ•์„ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."
112
  user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
113
 
 
122
  except Exception as e:
123
  return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
124
 
125
+ def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
126
+ """
127
+ ์ฝ”๋“œ ๋‚ด์šฉ์— ๋”ฐ๋ผ lines ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
128
+
129
+ Parameters:
130
+ - code_content (str): ์ฝ”๋“œ ํ…์ŠคํŠธ ๋‚ด์šฉ
131
+ - min_lines (int): ์ตœ์†Œ lines ์ˆ˜
132
+ - max_lines (int): ์ตœ๋Œ€ lines ์ˆ˜
133
+
134
+ Returns:
135
+ - int: ์„ค์ •๋œ lines ์ˆ˜
136
+ """
137
+ # ์ฝ”๋“œ์˜ ์ค„ ์ˆ˜ ๊ณ„์‚ฐ
138
+ num_lines = len(code_content.split('\n'))
139
+ # ์ค„ ์ˆ˜๊ฐ€ min_lines๋ณด๋‹ค ์ ๋‹ค๋ฉด min_lines ์‚ฌ์šฉ, max_lines๋ณด๋‹ค ํฌ๋ฉด max_lines ์‚ฌ์šฉ
140
+ return min(max(num_lines, min_lines), max_lines)
141
+
142
  def analyze_space(url: str, progress=gr.Progress()):
143
  try:
144
  space_id = url.split('spaces/')[-1]
145
 
146
+ # Space ID ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ˆ˜์ •
147
+ if not re.match(r'^[\w.-]+/[\w.-]+$', space_id):
148
+ raise ValueError(f"Invalid Space ID format: {space_id}")
149
+
150
  progress(0.1, desc="ํŒŒ์ผ ๊ตฌ์กฐ ๋ถ„์„ ์ค‘...")
151
  tree_structure = get_space_structure(space_id)
152
+ if "error" in tree_structure:
153
+ raise ValueError(tree_structure["error"])
154
  tree_view = format_tree_structure(tree_structure)
155
 
156
  progress(0.3, desc="app.py ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘...")
157
  app_content = get_file_content(space_id, "app.py")
158
 
159
+ progress(0.5, desc="์ฝ”๋“œ ์š”์•ฝ ์ค‘...")
160
  summary = summarize_code(app_content)
161
 
162
+ progress(0.7, desc="์ฝ”๋“œ ๋ถ„์„ ์ค‘...")
163
  analysis = analyze_code(app_content)
164
 
165
+ progress(0.9, desc="์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘...")
166
  usage = explain_usage(app_content)
167
 
168
+ # ์ค„ ์ˆ˜ ๊ณ„์‚ฐํ•˜์—ฌ lines ์„ค์ •
169
+ app_py_lines = adjust_lines_for_code(app_content)
170
+
171
  progress(1.0, desc="์™„๋ฃŒ")
172
+ return app_content, tree_view, tree_structure, space_id, summary, analysis, usage, app_py_lines
173
  except Exception as e:
174
  print(f"Error in analyze_space: {str(e)}")
175
  print(traceback.format_exc())
176
+ return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", None, "", "", "", "", 10
177
 
178
+ def respond(message: str, chat_history: List[Dict[str, str]], max_tokens: int, temperature: float, top_p: float) -> str:
179
+ system_message = """๋‹น์‹ ์€ ํ—ˆ๊น…ํŽ˜์ด์Šค์— ํŠนํ™”๋œ AI ์ฝ”๋”ฉ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ์นœ์ ˆํ•˜๊ณ  ์ƒ์„ธํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”.
180
+ Gradio ํŠน์„ฑ์„ ์ •ํ™•ํžˆ ์ธ์‹ํ•˜๊ณ  Requirements.txt ๋ˆ„๋ฝ์—†์ด ์ฝ”๋”ฉ๊ณผ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
181
+ ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ๋…ธ๋ ฅํ•˜์„ธ์š”."""
182
+
183
+ messages = [{"role": "system", "content": system_message}]
184
+ messages.extend(chat_history)
185
+ messages.append({"role": "user", "content": message})
186
+
187
+ try:
188
+ response = hf_client.chat_completion(messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
189
+ return response.choices[0].message.content
190
+ except Exception as e:
191
+ return f"์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
192
 
193
  def create_ui():
194
  try:
 
204
  overflow-y: auto !important;
205
  max-height: calc((100vh - 200px) / 5) !important;
206
  }
207
+ .tree-view-scroll {
208
+ overflow-y: auto !important;
209
+ max-height: calc((100vh - 200px) / 2) !important;
210
+ }
211
  .full-height {
212
+ height: calc(200em * 1.2) !important;
213
  overflow-y: auto !important;
214
  }
215
+ .code-box {
216
+ overflow-x: auto !important;
217
+ overflow-y: auto !important;
218
+ white-space: pre !important;
219
+ word-wrap: normal !important;
220
+ height: 100% !important;
221
+ }
222
+ .code-box > div {
223
+ min-width: 100% !important;
224
+ }
225
+ .code-box > div > textarea {
226
+ word-break: normal !important;
227
+ overflow-wrap: normal !important;
228
+ }
229
+ .tab-nav {
230
+ background-color: #2c3e50;
231
+ border-radius: 5px 5px 0 0;
232
+ overflow: hidden;
233
+ }
234
+ .tab-nav button {
235
+ color: #ecf0f1 !important;
236
+ background-color: #34495e;
237
+ border: none;
238
+ padding: 10px 20px;
239
+ margin: 0;
240
+ transition: background-color 0.3s;
241
+ font-size: 16px;
242
+ font-weight: bold;
243
+ }
244
+ .tab-nav button:hover {
245
+ background-color: #2980b9;
246
+ }
247
+ .tab-nav button.selected {
248
+ color: #2c3e50 !important;
249
+ background-color: #ecf0f1;
250
+ }
251
+ input[type="text"], textarea {
252
+ color: #2c3e50 !important;
253
+ background-color: #ecf0f1 !important;
254
  }
255
  """
256
 
257
+ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
258
+ gr.Markdown("# Mouse: HuggingFace")
259
 
260
+ with gr.Tabs() as tabs:
261
+ with gr.TabItem("๋ถ„์„"):
262
+ with gr.Row():
263
+ with gr.Column(scale=6): # ์™ผ์ชฝ 60%
264
+ url_input = gr.Textbox(label="HuggingFace Space URL")
265
+ analyze_button = gr.Button("๋ถ„์„")
266
+
267
+ with gr.Group(elem_classes="output-group scroll-lock"):
268
+ summary_output = gr.Markdown(label="์š”์•ฝ (3์ค„ ์ด๋‚ด)")
269
+
270
+ with gr.Group(elem_classes="output-group scroll-lock"):
271
+ analysis_output = gr.Markdown(label="๋ถ„์„")
272
+
273
+ with gr.Group(elem_classes="output-group scroll-lock"):
274
+ usage_output = gr.Markdown(label="์‚ฌ์šฉ๋ฒ•")
275
+
276
+ with gr.Group(elem_classes="output-group tree-view-scroll"): # ํŠธ๋ฆฌ ๋ทฐ ์Šคํฌ๋กค ์ถ”๊ฐ€
277
+ tree_view_output = gr.Textbox(label="ํŒŒ์ผ ๊ตฌ์กฐ (Tree View)", lines=30)
278
+
279
+ with gr.Column(scale=4): # ์˜ค๋ฅธ์ชฝ 40%
280
+ with gr.Group(elem_classes="output-group full-height"):
281
+ code_tabs = gr.Tabs()
282
+ with code_tabs:
283
+ app_py_tab = gr.TabItem("app.py")
284
+ with app_py_tab:
285
+ app_py_content = gr.Code(
286
+ language="python",
287
+ label="app.py",
288
+ lines=200,
289
+ elem_classes="full-height code-box"
290
+ )
291
+ requirements_tab = gr.TabItem("requirements.txt")
292
+ with requirements_tab:
293
+ requirements_content = gr.Textbox(
294
+ label="requirements.txt",
295
+ lines=200,
296
+ elem_classes="full-height code-box"
297
+ )
298
+
299
+ with gr.TabItem("AI ์ฝ”๋”ฉ"):
300
+ chatbot = gr.Chatbot(label="๋Œ€ํ™”", type='messages')
301
+ msg = gr.Textbox(label="๋ฉ”์‹œ์ง€")
302
 
303
+ # ์ˆจ๊ฒจ์ง„ ์ƒํƒœ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ •
304
+ max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens", visible=False)
305
+ temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature", visible=False)
306
+ top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P", visible=False)
307
 
308
+ examples = [
309
+ ["์ƒ์„ธํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋ฉด์„œ ์„ค๋ช…ํ•˜๋“ฏ์ด 4000 ํ† ํฐ ์ด์ƒ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋ผ"],
310
+ ["FAQ 20๊ฑด์„ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋ผ. 4000ํ† ํฐ ์ด์ƒ ์‚ฌ์šฉํ•˜๋ผ."],
311
+ ["์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ฐจ๋ณ„์ , ํŠน์ง•, ๊ฐ•์ ์„ ์ค‘์‹ฌ์œผ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์œ ํŠœ๋ธŒ ์˜์ƒ ์Šคํฌ๋ฆฝํŠธ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋ผ"],
312
+ ["๋ณธ ์„œ๋น„์Šค๋ฅผ SEO ์ตœ์ ํ™”ํ•˜์—ฌ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ(๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ, ๊ธฐ์กด ์œ ์‚ฌ ์„œ๋น„์Šค์™€ ๋น„๊ตํ•˜์—ฌ ํŠน์žฅ์ , ํ™œ์šฉ์ฒ˜, ๊ฐ€์น˜, ๊ธฐ๋Œ€ํšจ๊ณผ, ๊ฒฐ๋ก ์„ ํฌํ•จ)๋กœ 4000 ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ"],
313
+ ["ํŠนํ—ˆ ์ถœ์›์— ํ™œ์šฉํ•  ๊ธฐ์ˆ  ๋ฐ ๋น„์ฆˆ๋‹ˆ์Šค๋ชจ๋ธ ์ธก๋ฉด์„ ํฌํ•จํ•˜์—ฌ ํŠนํ—ˆ ์ถœ์›์„œ ๊ตฌ์„ฑ์— ๋งž๊ฒŒ ํ˜์‹ ์ ์ธ ์ฐฝ์˜ ๋ฐœ๋ช… ๋‚ด์šฉ์„ ์ค‘์‹ฌ์œผ๋กœ 4000ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ."],
314
+ ["๊ณ„์† ์ด์–ด์„œ ๋‹ต๋ณ€ํ•˜๋ผ"],
315
+ ]
 
 
 
 
 
 
 
316
 
317
+ gr.Examples(examples, inputs=msg)
 
318
 
319
+ def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
320
+ bot_message = respond(message, chat_history, max_tokens, temperature, top_p)
321
+ chat_history.append({"role": "user", "content": message})
322
+ chat_history.append({"role": "assistant", "content": bot_message})
323
+ return "", chat_history
 
 
 
 
 
 
 
324
 
325
+ msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
 
326
 
327
+ space_id_state = gr.State()
328
+ tree_structure_state = gr.State()
329
+ app_py_content_lines = gr.State()
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, app_py_content_lines]
335
  ).then(
336
+ lambda space_id: get_file_content(space_id, "requirements.txt"),
337
+ inputs=[space_id_state],
338
+ outputs=[requirements_content]
 
 
 
 
 
 
 
 
 
 
 
 
339
  )
340
 
341
+ # lines ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ์„ค์ •
342
+ app_py_content.change(lambda lines: gr.update(lines=lines), inputs=[app_py_content_lines], outputs=[app_py_content])
343
 
344
  return demo
345
 
 
350
 
351
  if __name__ == "__main__":
352
  try:
353
+ print("Starting HuggingFace Space Analyzer...")
354
  demo = create_ui()
355
+ print("UI created successfully.")
356
+
357
+ print("Configuring Gradio queue...")
358
  demo.queue()
359
+ print("Gradio queue configured.")
360
+
361
+ print("Launching Gradio app...")
362
+ demo.launch(
363
+ server_name="0.0.0.0",
364
+ server_port=7860,
365
+ share=False,
366
+ debug=True,
367
+ show_api=False
368
+ )
369
+ print("Gradio app launched successfully.")
370
  except Exception as e:
371
  print(f"Error in main: {str(e)}")
372
+ print("Detailed error information:")
373
+ print(traceback.format_exc())
374
+ raise