Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
107 |
-
|
108 |
-
|
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 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
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.
|
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
|
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
|
307 |
-
|
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=[
|
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:
|