Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -81,10 +81,11 @@ def summarize_code(app_content: str):
|
|
81 |
]
|
82 |
|
83 |
try:
|
84 |
-
|
85 |
-
|
86 |
except Exception as e:
|
87 |
-
|
|
|
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 |
-
|
106 |
-
|
107 |
except Exception as e:
|
108 |
-
|
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 |
-
|
121 |
-
|
122 |
except Exception as e:
|
123 |
-
|
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.-]
|
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 |
-
|
159 |
-
response += chunk.choices[0].delta.content or ""
|
160 |
-
yield response
|
161 |
except Exception as e:
|
162 |
-
|
|
|
|
|
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 |
-
.
|
183 |
-
|
184 |
-
font-weight: bold !important;
|
185 |
}
|
186 |
-
.
|
187 |
-
|
188 |
-
|
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(
|
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(
|
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 |
-
|
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())
|