Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,6 @@ from huggingface_hub import HfApi
|
|
7 |
import requests
|
8 |
import re
|
9 |
import traceback
|
10 |
-
import time
|
11 |
-
import threading
|
12 |
-
import json
|
13 |
|
14 |
# HuggingFace ๊ด๋ จ API ํค (์คํ์ด์ค ๋ถ์ ์ฉ)
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -20,9 +17,6 @@ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
20 |
genai.configure(api_key=GEMINI_API_KEY)
|
21 |
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")
|
22 |
|
23 |
-
# --------------------------------------------------
|
24 |
-
# ํ์ผ ๋ฐ ์คํ์ด์ค ๋ถ์ ๊ด๋ จ ํจ์๋ค (๊ธฐ์กด ์ฝ๋ ์ ์ง)
|
25 |
-
# --------------------------------------------------
|
26 |
def get_headers():
|
27 |
if not HF_TOKEN:
|
28 |
raise ValueError("Hugging Face token not found in environment variables")
|
@@ -74,10 +68,6 @@ def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
|
|
74 |
formatted += format_tree_structure(child, indent + " ")
|
75 |
return formatted
|
76 |
|
77 |
-
def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
|
78 |
-
num_lines = len(code_content.split('\n'))
|
79 |
-
return min(max(num_lines, min_lines), max_lines)
|
80 |
-
|
81 |
def analyze_space(url: str, progress=gr.Progress()):
|
82 |
try:
|
83 |
space_id = url.split('spaces/')[-1]
|
@@ -96,58 +86,82 @@ def analyze_space(url: str, progress=gr.Progress()):
|
|
96 |
analysis = analyze_code(app_content)
|
97 |
progress(0.9, desc="์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค...")
|
98 |
usage = explain_usage(app_content)
|
99 |
-
|
|
|
100 |
progress(1.0, desc="์๋ฃ")
|
101 |
-
return app_content, tree_view, tree_structure, space_id, summary, analysis, usage,
|
102 |
except Exception as e:
|
103 |
print(f"Error in analyze_space: {str(e)}")
|
104 |
print(traceback.format_exc())
|
105 |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", None, "", "", "", "", 10
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
ChatMessage(role="system", content=system_message),
|
124 |
ChatMessage(role="user", content=user_message)
|
125 |
]
|
126 |
-
chat_history = format_chat_history(
|
127 |
chat = model.start_chat(history=chat_history)
|
128 |
-
|
129 |
try:
|
130 |
for chunk in chat.send_message(user_message, stream=True):
|
131 |
parts = chunk.candidates[0].content.parts
|
132 |
if len(parts) == 2:
|
133 |
-
|
|
|
134 |
else:
|
135 |
-
|
136 |
-
|
|
|
137 |
except Exception as e:
|
138 |
return f"LLM ํธ์ถ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
139 |
|
140 |
-
def summarize_code(app_content: str)
|
141 |
-
|
142 |
-
|
143 |
try:
|
144 |
-
return gemini_chat_completion(
|
145 |
except Exception as e:
|
146 |
return f"์์ฝ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
147 |
|
148 |
-
def analyze_code(app_content: str)
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
151 |
"A. ๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ\n"
|
152 |
"B. ๊ธฐ๋ฅ์ ํจ์ฉ์ฑ ๋ฐ ๊ฐ์น\n"
|
153 |
"C. ํน์ฅ์ \n"
|
@@ -155,328 +169,247 @@ def analyze_code(app_content: str) -> str:
|
|
155 |
"E. ๊ธฐ๋ํจ๊ณผ\n"
|
156 |
"๊ธฐ์กด ๋ฐ ์ ์ฌ ํ๋ก์ ํธ์ ๋น๊ตํ์ฌ ๋ถ์ํด์ฃผ์ธ์. Markdown ํ์์ผ๋ก ์ถ๋ ฅํ์ธ์."
|
157 |
)
|
158 |
-
|
159 |
try:
|
160 |
-
return gemini_chat_completion(
|
161 |
except Exception as e:
|
162 |
return f"๋ถ์ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
163 |
|
164 |
-
def explain_usage(app_content: str)
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
try:
|
168 |
-
return gemini_chat_completion(
|
169 |
except Exception as e:
|
170 |
return f"์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
171 |
|
172 |
-
|
|
|
|
|
|
|
173 |
"""
|
174 |
-
|
175 |
"""
|
176 |
-
flat = []
|
177 |
-
for item in messages:
|
178 |
-
if isinstance(item, list):
|
179 |
-
flat.extend(flatten_messages(item))
|
180 |
-
else:
|
181 |
-
flat.append(item)
|
182 |
-
return flat
|
183 |
-
|
184 |
-
def convert_chat_history(messages: List[Any]) -> List[Tuple[str, str]]:
|
185 |
-
"""
|
186 |
-
๋ฉ์์ง ๋ชฉ๋ก์ ๊ฐ ํญ๋ชฉ์ด ChatMessage ๊ฐ์ฒด๋ผ๋ฉด (user, assistant) ํํ๋ก,
|
187 |
-
์ด๋ฏธ ํํ์ธ ๊ฒฝ์ฐ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
|
188 |
-
"""
|
189 |
-
flat_messages = flatten_messages(messages)
|
190 |
-
conv = []
|
191 |
-
i = 0
|
192 |
-
while i < len(flat_messages):
|
193 |
-
if isinstance(flat_messages[i], tuple):
|
194 |
-
conv.append(flat_messages[i])
|
195 |
-
i += 1
|
196 |
-
elif hasattr(flat_messages[i], "role"):
|
197 |
-
if flat_messages[i].role == "user":
|
198 |
-
user_text = flat_messages[i].content
|
199 |
-
bot_text = ""
|
200 |
-
if i + 1 < len(flat_messages) and hasattr(flat_messages[i+1], "role") and flat_messages[i+1].role == "assistant":
|
201 |
-
bot_text = flat_messages[i+1].content
|
202 |
-
i += 2
|
203 |
-
else:
|
204 |
-
i += 1
|
205 |
-
conv.append((user_text, bot_text))
|
206 |
-
else:
|
207 |
-
conv.append(("", flat_messages[i].content))
|
208 |
-
i += 1
|
209 |
-
else:
|
210 |
-
i += 1
|
211 |
-
return conv
|
212 |
-
|
213 |
-
def convert_to_chatmessage(history: List[Tuple[str, str]]) -> List[ChatMessage]:
|
214 |
-
"""
|
215 |
-
ํํ ๋ชฉ๋ก์ ChatMessage ๊ฐ์ฒด ๋ชฉ๋ก์ผ๋ก ๋ณํํฉ๋๋ค.
|
216 |
-
"""
|
217 |
-
new_history = []
|
218 |
-
for tup in history:
|
219 |
-
if tup[0]:
|
220 |
-
new_history.append(ChatMessage(role="user", content=tup[0]))
|
221 |
-
if tup[1]:
|
222 |
-
new_history.append(ChatMessage(role="assistant", content=tup[1]))
|
223 |
-
return new_history
|
224 |
-
|
225 |
-
def stream_gemini_response(user_message: str, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
226 |
if not user_message.strip():
|
227 |
-
|
228 |
-
|
|
|
|
|
229 |
return
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
|
|
247 |
)
|
|
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
if len(parts) == 2 and not thinking_complete:
|
254 |
-
thought_buffer += current_chunk
|
255 |
-
print(f"\n=== Complete Thought ===\n{thought_buffer}")
|
256 |
-
messages[-1] = ChatMessage(
|
257 |
-
role="assistant",
|
258 |
-
content=thought_buffer,
|
259 |
-
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
260 |
-
)
|
261 |
-
yield messages
|
262 |
-
|
263 |
-
response_buffer = parts[1].text
|
264 |
-
print(f"\n=== Starting Response ===\n{response_buffer}")
|
265 |
-
messages.append(
|
266 |
-
ChatMessage(
|
267 |
-
role="assistant",
|
268 |
-
content=response_buffer
|
269 |
-
)
|
270 |
-
)
|
271 |
-
thinking_complete = True
|
272 |
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
280 |
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
288 |
-
)
|
289 |
-
yield messages
|
290 |
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
|
|
297 |
role="assistant",
|
298 |
-
content=
|
|
|
299 |
)
|
300 |
-
|
301 |
-
|
|
|
302 |
|
303 |
-
def
|
304 |
"""
|
305 |
-
|
306 |
"""
|
307 |
-
|
308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
|
310 |
-
def
|
311 |
-
|
312 |
-
|
|
|
|
|
|
|
|
|
313 |
|
314 |
-
def respond_wrapper(message,
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
|
|
|
|
|
|
320 |
|
321 |
# --------------------------------------------------
|
322 |
-
# Gradio UI
|
323 |
# --------------------------------------------------
|
324 |
def create_ui():
|
325 |
try:
|
326 |
css = """
|
327 |
-
|
328 |
-
body {
|
329 |
-
background-color: #f9f9f9;
|
330 |
-
font-family: 'Helvetica Neue', Arial, sans-serif;
|
331 |
-
color: #333;
|
332 |
-
}
|
333 |
-
/* ํ๋จ ํธํฐ ์จ๊น */
|
334 |
-
footer { visibility: hidden; }
|
335 |
-
|
336 |
-
/* ์ถ๋ ฅ ๊ทธ๋ฃน ์คํ์ผ: ๋ฐ์ ๋ฐฐ๊ฒฝ, ๋ถ๋๋ฌ์ด ํ
๋๋ฆฌ์ ๊ทธ๋ฆผ์ */
|
337 |
-
.output-group {
|
338 |
-
border: 1px solid #ccc;
|
339 |
-
border-radius: 8px;
|
340 |
-
padding: 15px;
|
341 |
-
margin-bottom: 20px;
|
342 |
-
background-color: #ffffff;
|
343 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
344 |
-
}
|
345 |
-
/* ์คํฌ๋กค ์์ญ ์ค์ */
|
346 |
-
.scroll-lock {
|
347 |
-
overflow-y: auto !important;
|
348 |
-
max-height: 300px !important;
|
349 |
-
}
|
350 |
-
.tree-view-scroll {
|
351 |
-
overflow-y: auto !important;
|
352 |
-
max-height: 400px !important;
|
353 |
-
}
|
354 |
-
.full-height {
|
355 |
-
height: 80vh !important;
|
356 |
-
overflow-y: auto !important;
|
357 |
-
}
|
358 |
-
/* ์ฝ๋ ๋ฐ์ค ์คํ์ผ: ๋ชจ๋
ธ์คํ์ด์ค ํฐํธ์ ๋ฐ์ ๋ฐฐ๊ฒฝ */
|
359 |
-
.code-box {
|
360 |
-
overflow-x: auto !important;
|
361 |
-
overflow-y: auto !important;
|
362 |
-
white-space: pre !important;
|
363 |
-
background-color: #f5f5f5;
|
364 |
-
border-radius: 4px;
|
365 |
-
padding: 10px;
|
366 |
-
font-family: 'Courier New', Courier, monospace;
|
367 |
-
}
|
368 |
-
.code-box > div { min-width: 100% !important; }
|
369 |
-
.code-box > div > textarea {
|
370 |
-
word-break: normal !important;
|
371 |
-
overflow-wrap: normal !important;
|
372 |
-
}
|
373 |
-
/* ํญ ๋ด๋น๊ฒ์ด์
์คํ์ผ: ๋จ์ํ๊ณ ๊น๋ํ ๋์์ธ */
|
374 |
-
.tab-nav {
|
375 |
-
background-color: #ffffff;
|
376 |
-
border-bottom: 1px solid #ccc;
|
377 |
-
display: flex;
|
378 |
-
}
|
379 |
-
.tab-nav button {
|
380 |
-
background: none;
|
381 |
-
border: none;
|
382 |
-
padding: 10px 20px;
|
383 |
-
margin: 0;
|
384 |
-
cursor: pointer;
|
385 |
-
font-size: 16px;
|
386 |
-
color: #555;
|
387 |
-
transition: color 0.3s, border-bottom 0.3s;
|
388 |
-
}
|
389 |
-
.tab-nav button:hover,
|
390 |
-
.tab-nav button.selected {
|
391 |
-
color: #000;
|
392 |
-
border-bottom: 2px solid #007BFF;
|
393 |
-
}
|
394 |
-
/* ์
๋ ฅ์ฐฝ ๋ฐ ํ
์คํธ ์์ญ ์คํ์ผ */
|
395 |
-
input[type="text"], textarea {
|
396 |
-
color: #333;
|
397 |
-
background-color: #fff;
|
398 |
-
border: 1px solid #ccc;
|
399 |
-
border-radius: 4px;
|
400 |
-
padding: 8px;
|
401 |
-
}
|
402 |
"""
|
403 |
|
404 |
-
with gr.Blocks(
|
405 |
-
gr.Markdown("# MOUSE: Space Research Thinking"
|
406 |
-
|
407 |
-
with gr.Tabs()
|
408 |
with gr.TabItem("๋ถ์"):
|
409 |
with gr.Row():
|
410 |
-
with gr.Column(
|
411 |
-
url_input = gr.Textbox(label="HuggingFace Space URL"
|
412 |
-
analyze_button = gr.Button("๋ถ์"
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
label="app.py",
|
434 |
-
lines=200,
|
435 |
-
elem_classes="full-height code-box"
|
436 |
-
)
|
437 |
-
with gr.TabItem("requirements.txt"):
|
438 |
-
requirements_content = gr.Textbox(
|
439 |
-
label="requirements.txt",
|
440 |
-
lines=200,
|
441 |
-
elem_classes="full-height code-box"
|
442 |
-
)
|
443 |
|
444 |
with gr.TabItem("AI ์ฝ๋์ฑ"):
|
445 |
-
gr.Markdown("##
|
446 |
-
|
|
|
447 |
chatbot = gr.Chatbot(
|
448 |
label="๋ํ",
|
449 |
-
elem_classes="output-group",
|
450 |
height=400
|
451 |
)
|
452 |
|
453 |
-
msg = gr.Textbox(
|
454 |
-
|
|
|
|
|
|
|
455 |
# ์จ๊ฒจ์ง ํ๋ผ๋ฏธํฐ
|
456 |
-
max_tokens = gr.Slider(
|
457 |
-
|
458 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
459 |
|
460 |
examples = [
|
461 |
-
["์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์
|
462 |
-
["FAQ 20๊ฑด์
|
463 |
-
["
|
464 |
-
["
|
465 |
-
["
|
466 |
-
["
|
467 |
-
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"],
|
468 |
]
|
469 |
gr.Examples(examples, inputs=msg)
|
470 |
|
471 |
-
|
472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
473 |
with gr.TabItem("Recommended Best"):
|
474 |
gr.Markdown(
|
475 |
-
"Discover
|
476 |
-
elem_id="recommended-best"
|
477 |
)
|
478 |
|
479 |
-
#
|
480 |
space_id_state = gr.State()
|
481 |
tree_structure_state = gr.State()
|
482 |
app_py_content_lines = gr.State()
|
@@ -484,15 +417,26 @@ def create_ui():
|
|
484 |
analyze_button.click(
|
485 |
analyze_space,
|
486 |
inputs=[url_input],
|
487 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
488 |
).then(
|
489 |
lambda space_id: get_file_content(space_id, "requirements.txt"),
|
490 |
inputs=[space_id_state],
|
491 |
outputs=[requirements_content]
|
|
|
|
|
|
|
|
|
492 |
)
|
493 |
|
494 |
-
app_py_content.change(lambda lines: gr.update(lines=lines), inputs=[app_py_content_lines], outputs=[app_py_content])
|
495 |
-
|
496 |
return demo
|
497 |
|
498 |
except Exception as e:
|
@@ -504,21 +448,9 @@ if __name__ == "__main__":
|
|
504 |
try:
|
505 |
print("Starting HuggingFace Space Analyzer...")
|
506 |
demo = create_ui()
|
507 |
-
print("UI created successfully.")
|
508 |
-
print("Configuring Gradio queue...")
|
509 |
demo.queue()
|
510 |
-
|
511 |
-
print("Launching Gradio app...")
|
512 |
-
demo.launch(
|
513 |
-
server_name="0.0.0.0",
|
514 |
-
server_port=7860,
|
515 |
-
share=False,
|
516 |
-
debug=True,
|
517 |
-
show_api=False
|
518 |
-
)
|
519 |
-
print("Gradio app launched successfully.")
|
520 |
except Exception as e:
|
521 |
print(f"Error in main: {str(e)}")
|
522 |
-
print("Detailed error information:")
|
523 |
print(traceback.format_exc())
|
524 |
raise
|
|
|
7 |
import requests
|
8 |
import re
|
9 |
import traceback
|
|
|
|
|
|
|
10 |
|
11 |
# HuggingFace ๊ด๋ จ API ํค (์คํ์ด์ค ๋ถ์ ์ฉ)
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
17 |
genai.configure(api_key=GEMINI_API_KEY)
|
18 |
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")
|
19 |
|
|
|
|
|
|
|
20 |
def get_headers():
|
21 |
if not HF_TOKEN:
|
22 |
raise ValueError("Hugging Face token not found in environment variables")
|
|
|
68 |
formatted += format_tree_structure(child, indent + " ")
|
69 |
return formatted
|
70 |
|
|
|
|
|
|
|
|
|
71 |
def analyze_space(url: str, progress=gr.Progress()):
|
72 |
try:
|
73 |
space_id = url.split('spaces/')[-1]
|
|
|
86 |
analysis = analyze_code(app_content)
|
87 |
progress(0.9, desc="์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค...")
|
88 |
usage = explain_usage(app_content)
|
89 |
+
# lines ์ ์กฐ์
|
90 |
+
lines_for_app_py = adjust_lines_for_code(app_content)
|
91 |
progress(1.0, desc="์๋ฃ")
|
92 |
+
return app_content, tree_view, tree_structure, space_id, summary, analysis, usage, lines_for_app_py
|
93 |
except Exception as e:
|
94 |
print(f"Error in analyze_space: {str(e)}")
|
95 |
print(traceback.format_exc())
|
96 |
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", None, "", "", "", "", 10
|
97 |
|
98 |
+
def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
|
99 |
+
num_lines = len(code_content.split('\n'))
|
100 |
+
return min(max(num_lines, min_lines), max_lines)
|
101 |
+
|
102 |
+
# -----------------------------
|
103 |
+
# LLM (Gemini) ๊ด๋ จ ํจ์
|
104 |
+
# -----------------------------
|
105 |
+
from gradio import ChatMessage
|
106 |
+
|
107 |
def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
|
108 |
+
"""
|
109 |
+
ChatMessage ๋ชฉ๋ก์ Gemini ๋ชจ๋ธ์ด ์ดํดํ ์ ์๋ ํ์์ผ๋ก ๋ณํ
|
110 |
+
(Thinking ๋ฉํ๋ฐ์ดํฐ ํฌํจ ๋ฉ์์ง๋ ์ ์ธ)
|
111 |
+
"""
|
112 |
+
formatted = []
|
113 |
+
for m in messages:
|
114 |
+
# 'Thinking' metadata๊ฐ ์์ผ๋ฉด ๋ฌด์
|
115 |
+
if hasattr(m, "metadata") and m.metadata:
|
116 |
+
continue
|
117 |
+
role = "assistant" if m.role == "assistant" else "user"
|
118 |
+
formatted.append({"role": role, "parts": [m.content or ""]})
|
119 |
+
return formatted
|
120 |
+
|
121 |
+
import google.generativeai as genai
|
122 |
+
|
123 |
+
def gemini_chat_completion(
|
124 |
+
system_message: str,
|
125 |
+
user_message: str,
|
126 |
+
max_tokens: int = 200,
|
127 |
+
temperature: float = 0.7
|
128 |
+
) -> str:
|
129 |
+
# ChatMessage ๊ฐ์ฒด๋ก ๋ณํ
|
130 |
+
init_msgs = [
|
131 |
ChatMessage(role="system", content=system_message),
|
132 |
ChatMessage(role="user", content=user_message)
|
133 |
]
|
134 |
+
chat_history = format_chat_history(init_msgs)
|
135 |
chat = model.start_chat(history=chat_history)
|
136 |
+
final = ""
|
137 |
try:
|
138 |
for chunk in chat.send_message(user_message, stream=True):
|
139 |
parts = chunk.candidates[0].content.parts
|
140 |
if len(parts) == 2:
|
141 |
+
# Thinking + ์ต์ข
์๋ต ํํ๋ก ๋ค์ด์จ ๊ฒฝ์ฐ
|
142 |
+
final += parts[1].text
|
143 |
else:
|
144 |
+
# ๊ทธ๋ฅ ์๋ต
|
145 |
+
final += parts[0].text
|
146 |
+
return final.strip()
|
147 |
except Exception as e:
|
148 |
return f"LLM ํธ์ถ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
149 |
|
150 |
+
def summarize_code(app_content: str):
|
151 |
+
system_msg = "๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ์์ฝํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ 3์ค ์ด๋ด๋ก ๊ฐ๊ฒฐํ๊ฒ ์์ฝํด์ฃผ์ธ์."
|
152 |
+
user_msg = f"๋ค์ Python ์ฝ๋๋ฅผ 3์ค ์ด๋ด๋ก ์์ฝํด์ฃผ์ธ์:\n\n{app_content}"
|
153 |
try:
|
154 |
+
return gemini_chat_completion(system_msg, user_msg, max_tokens=200, temperature=0.7)
|
155 |
except Exception as e:
|
156 |
return f"์์ฝ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
157 |
|
158 |
+
def analyze_code(app_content: str):
|
159 |
+
# ์์คํ
ํ๋กฌํํธ์ '๋ฅ์ฝํน' ์๋ด๋ฌธ ์ถ๊ฐ
|
160 |
+
system_msg = (
|
161 |
+
"You are a deep thinking AI. You may use extremely long chains of thought to deeply consider the problem "
|
162 |
+
"and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. "
|
163 |
+
"You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem. "
|
164 |
+
"๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ ๋ถ์ํ์ฌ ์๋น์ค์ ํจ์ฉ์ฑ๊ณผ ํ์ฉ ์ธก๋ฉด์์ ๋ค์ ํญ๋ชฉ์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์:\n"
|
165 |
"A. ๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ\n"
|
166 |
"B. ๊ธฐ๋ฅ์ ํจ์ฉ์ฑ ๋ฐ ๊ฐ์น\n"
|
167 |
"C. ํน์ฅ์ \n"
|
|
|
169 |
"E. ๊ธฐ๋ํจ๊ณผ\n"
|
170 |
"๊ธฐ์กด ๋ฐ ์ ์ฌ ํ๋ก์ ํธ์ ๋น๊ตํ์ฌ ๋ถ์ํด์ฃผ์ธ์. Markdown ํ์์ผ๋ก ์ถ๋ ฅํ์ธ์."
|
171 |
)
|
172 |
+
user_msg = f"๋ค์ Python ์ฝ๋๋ฅผ ๋ถ์ํด์ฃผ์ธ์:\n\n{app_content}"
|
173 |
try:
|
174 |
+
return gemini_chat_completion(system_msg, user_msg, max_tokens=1000, temperature=0.7)
|
175 |
except Exception as e:
|
176 |
return f"๋ถ์ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
177 |
|
178 |
+
def explain_usage(app_content: str):
|
179 |
+
# ์์คํ
ํ๋กฌํํธ์ '๋ฅ์ฝํน' ์๋ด๋ฌธ ์ถ๊ฐ
|
180 |
+
system_msg = (
|
181 |
+
"You are a deep thinking AI. You may use extremely long chains of thought to deeply consider the problem "
|
182 |
+
"and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. "
|
183 |
+
"You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem. "
|
184 |
+
"๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ์ฌ ์ฌ์ฉ๋ฒ์ ์ค๋ช
ํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ๋ง์น ํ๋ฉด์ ๋ณด๋ ๊ฒ์ฒ๋ผ ์ฌ์ฉ๋ฒ์ ์์ธํ ์ค๋ช
ํด์ฃผ์ธ์. Markdown ํ์์ผ๋ก ์ถ๋ ฅํ์ธ์."
|
185 |
+
)
|
186 |
+
user_msg = f"๋ค์ Python ์ฝ๋์ ์ฌ์ฉ๋ฒ์ ์ค๋ช
ํด์ฃผ์ธ์:\n\n{app_content}"
|
187 |
try:
|
188 |
+
return gemini_chat_completion(system_msg, user_msg, max_tokens=800, temperature=0.7)
|
189 |
except Exception as e:
|
190 |
return f"์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
191 |
|
192 |
+
# -----------------------------
|
193 |
+
# ์ค์ ๋ํ ์คํธ๋ฆฌ๋ฐ
|
194 |
+
# -----------------------------
|
195 |
+
def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
196 |
"""
|
197 |
+
conversation_state: ChatMessage ๊ฐ์ฒด๋ก๋ง ์ด๋ฃจ์ด์ง '๋ํ ์ด๋ ฅ' (Gradio State).
|
198 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
if not user_message.strip():
|
200 |
+
conversation_state.append(
|
201 |
+
ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed.")
|
202 |
+
)
|
203 |
+
yield conversation_state
|
204 |
return
|
205 |
|
206 |
+
print(f"\n=== New Request ===\nUser message: {user_message}")
|
207 |
+
|
208 |
+
# ๊ธฐ์กด ๋ํ๋ฅผ Gemini ํ์์ผ๋ก ๋ณํ
|
209 |
+
chat_history = format_chat_history(conversation_state)
|
210 |
+
chat = model.start_chat(history=chat_history)
|
211 |
+
|
212 |
+
response = chat.send_message(user_message, stream=True)
|
213 |
+
thought_buffer = ""
|
214 |
+
response_buffer = ""
|
215 |
+
thinking_complete = False
|
216 |
+
|
217 |
+
# 'Thinking' ํ์์ฉ
|
218 |
+
conversation_state.append(
|
219 |
+
ChatMessage(
|
220 |
+
role="assistant",
|
221 |
+
content="",
|
222 |
+
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
223 |
)
|
224 |
+
)
|
225 |
|
226 |
+
for chunk in response:
|
227 |
+
parts = chunk.candidates[0].content.parts
|
228 |
+
current_chunk = parts[0].text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
|
230 |
+
if len(parts) == 2 and not thinking_complete:
|
231 |
+
# ์ฒซ ๋ฒ์งธ๊ฐ '์๊ฐ', ๋ ๋ฒ์งธ๊ฐ ์ต์ข
์๋ต
|
232 |
+
thought_buffer += current_chunk
|
233 |
+
print(f"\n=== Complete Thought ===\n{thought_buffer}")
|
234 |
+
conversation_state[-1] = ChatMessage(
|
235 |
+
role="assistant",
|
236 |
+
content=thought_buffer,
|
237 |
+
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
238 |
+
)
|
239 |
+
yield conversation_state
|
240 |
|
241 |
+
response_buffer = parts[1].text
|
242 |
+
print(f"\n=== Starting Response ===\n{response_buffer}")
|
243 |
+
conversation_state.append(
|
244 |
+
ChatMessage(role="assistant", content=response_buffer)
|
245 |
+
)
|
246 |
+
thinking_complete = True
|
|
|
|
|
|
|
247 |
|
248 |
+
elif thinking_complete:
|
249 |
+
# ์ด๋ฏธ ์๊ฐ์ด ๋๋ฌ์ผ๋ฏ๋ก ์ต์ข
์๋ต ๋์
|
250 |
+
response_buffer += current_chunk
|
251 |
+
print(f"\n=== Response Chunk ===\n{current_chunk}")
|
252 |
+
conversation_state[-1] = ChatMessage(
|
253 |
+
role="assistant",
|
254 |
+
content=response_buffer
|
255 |
+
)
|
256 |
|
257 |
+
else:
|
258 |
+
# ์์ง ์๊ฐ ์ค
|
259 |
+
thought_buffer += current_chunk
|
260 |
+
print(f"\n=== Thinking Chunk ===\n{current_chunk}")
|
261 |
+
conversation_state[-1] = ChatMessage(
|
262 |
role="assistant",
|
263 |
+
content=thought_buffer,
|
264 |
+
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
265 |
)
|
266 |
+
yield conversation_state
|
267 |
+
|
268 |
+
print(f"\n=== Final Response ===\n{response_buffer}")
|
269 |
|
270 |
+
def convert_to_display_tuples(messages: List[ChatMessage]) -> List[Tuple[str, str]]:
|
271 |
"""
|
272 |
+
ํ๋ฉด์ ํ์ํ๊ธฐ ์ํด (user, assistant) ํํ ๋ชฉ๋ก์ผ๋ก ๋ณํ
|
273 |
"""
|
274 |
+
result = []
|
275 |
+
i = 0
|
276 |
+
while i < len(messages):
|
277 |
+
if messages[i].role == "user":
|
278 |
+
user_text = messages[i].content
|
279 |
+
assistant_text = ""
|
280 |
+
if i + 1 < len(messages) and messages[i+1].role == "assistant":
|
281 |
+
assistant_text = messages[i+1].content
|
282 |
+
i += 2
|
283 |
+
else:
|
284 |
+
i += 1
|
285 |
+
result.append((user_text, assistant_text))
|
286 |
+
else:
|
287 |
+
# assistant ๋จ๋
|
288 |
+
result.append(("", messages[i].content))
|
289 |
+
i += 1
|
290 |
+
return result
|
291 |
|
292 |
+
def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
|
293 |
+
"""
|
294 |
+
์ฌ์ฉ์๊ฐ ๋ฉ์์ง๋ฅผ ์
๋ ฅํ ๋ ํธ์ถ.
|
295 |
+
ChatMessage ๋ฆฌ์คํธ(conversation_state)์ user ๋ฉ์์ง๋ฅผ ์ถ๊ฐํ ๋ค ๋ฐํ.
|
296 |
+
"""
|
297 |
+
conversation_state.append(ChatMessage(role="user", content=msg))
|
298 |
+
return "", conversation_state
|
299 |
|
300 |
+
def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
|
301 |
+
"""
|
302 |
+
์ ์ ๋ฉ์์ง๋ฅผ ๋ฐ์ Gemini์๊ฒ ์์ฒญ(์คํธ๋ฆฌ๋ฐ)ํ๊ณ , ๋ํ ์ด๋ ฅ์ ์
๋ฐ์ดํธ ํ
|
303 |
+
ํ๋ฉด์๋ (user, assistant) ํํ์ ๋ฐํํ๋ค.
|
304 |
+
"""
|
305 |
+
# ์คํธ๋ฆฌ๋ฐ ์๋ต
|
306 |
+
for updated_messages in stream_gemini_response(message, conversation_state):
|
307 |
+
# ํ๋ฉด ํ์์ฉ (user, assistant) ํํ๋ก ๋ณํ
|
308 |
+
yield "", convert_to_display_tuples(updated_messages)
|
309 |
|
310 |
# --------------------------------------------------
|
311 |
+
# Gradio UI
|
312 |
# --------------------------------------------------
|
313 |
def create_ui():
|
314 |
try:
|
315 |
css = """
|
316 |
+
footer {visibility: hidden;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
"""
|
318 |
|
319 |
+
with gr.Blocks(css=css) as demo:
|
320 |
+
gr.Markdown("# MOUSE: Space Research Thinking")
|
321 |
+
|
322 |
+
with gr.Tabs():
|
323 |
with gr.TabItem("๋ถ์"):
|
324 |
with gr.Row():
|
325 |
+
with gr.Column():
|
326 |
+
url_input = gr.Textbox(label="HuggingFace Space URL")
|
327 |
+
analyze_button = gr.Button("๋ถ์")
|
328 |
+
|
329 |
+
summary_output = gr.Markdown(label="์์ฝ")
|
330 |
+
analysis_output = gr.Markdown(label="๋ถ์")
|
331 |
+
usage_output = gr.Markdown(label="์ฌ์ฉ๋ฒ")
|
332 |
+
tree_view_output = gr.Textbox(label="ํ์ผ ๊ตฌ์กฐ", lines=20)
|
333 |
+
|
334 |
+
with gr.Column():
|
335 |
+
code_tabs = gr.Tabs()
|
336 |
+
with code_tabs:
|
337 |
+
with gr.TabItem("app.py"):
|
338 |
+
app_py_content = gr.Code(
|
339 |
+
language="python",
|
340 |
+
label="app.py",
|
341 |
+
lines=50
|
342 |
+
)
|
343 |
+
with gr.TabItem("requirements.txt"):
|
344 |
+
requirements_content = gr.Textbox(
|
345 |
+
label="requirements.txt",
|
346 |
+
lines=50
|
347 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
348 |
|
349 |
with gr.TabItem("AI ์ฝ๋์ฑ"):
|
350 |
+
gr.Markdown("## ์์ ๋ฅผ ์
๋ ฅ ๋๋ ์์ค ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ๊ณ ์ง๋ฌธํ์ธ์")
|
351 |
+
|
352 |
+
# Chatbot์ ๋จ์ง ์ถ๋ ฅ๋ง ๋ด๋น(ํํ์ ๋ฐ์ ํ์)
|
353 |
chatbot = gr.Chatbot(
|
354 |
label="๋ํ",
|
|
|
355 |
height=400
|
356 |
)
|
357 |
|
358 |
+
msg = gr.Textbox(
|
359 |
+
label="๋ฉ์์ง",
|
360 |
+
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์..."
|
361 |
+
)
|
362 |
+
|
363 |
# ์จ๊ฒจ์ง ํ๋ผ๋ฏธํฐ
|
364 |
+
max_tokens = gr.Slider(
|
365 |
+
minimum=1, maximum=8000,
|
366 |
+
value=4000, label="Max Tokens",
|
367 |
+
visible=False
|
368 |
+
)
|
369 |
+
temperature = gr.Slider(
|
370 |
+
minimum=0, maximum=1,
|
371 |
+
value=0.7, label="Temperature",
|
372 |
+
visible=False
|
373 |
+
)
|
374 |
+
top_p = gr.Slider(
|
375 |
+
minimum=0, maximum=1,
|
376 |
+
value=0.9, label="Top P",
|
377 |
+
visible=False
|
378 |
+
)
|
379 |
|
380 |
examples = [
|
381 |
+
["์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ 4000 ํ ํฐ ์ด์ ์์ธํ ์ค๋ช
"],
|
382 |
+
["FAQ 20๊ฑด์ 4000 ํ ํฐ ์ด์ ์์ฑ"],
|
383 |
+
["๊ธฐ์ ์ฐจ๋ณ์ , ๊ฐ์ ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์ค๋ช
"],
|
384 |
+
["ํนํ ์ถ์์ ํ์ฉ ๊ฐ๋ฅํ ํ์ ์์ด๋์ด๋ฅผ 4000 ํ ํฐ ์ด์ ์์ฑ"],
|
385 |
+
["๋
ผ๋ฌธ ํ์์ผ๋ก 4000 ํ ํฐ ์ด์ ์์ฑ"],
|
386 |
+
["๊ณ์ ์ด์ด์ ๋ต๋ณํ๋ผ"]
|
|
|
387 |
]
|
388 |
gr.Examples(examples, inputs=msg)
|
389 |
|
390 |
+
# ๋ํ ์ํ(์ฑํ
๊ธฐ๋ก)๋ ChatMessage ๊ฐ์ฒด๋ก๋ง ์ ์ง
|
391 |
+
conversation_state = gr.State([])
|
392 |
+
|
393 |
+
# ์ด๋ฒคํธ ์ฒด์ธ
|
394 |
+
# 1) ์ ์ ๋ฉ์์ง ์
๋ ฅ -> user_submit_message -> (์
๋ ฅ์ฐฝ ๋น์ฐ๊ณ , ChatMessage์ถ๊ฐ)
|
395 |
+
# 2) respond_wrapper ํธ์ถ -> Gemini ์คํธ๋ฆฌ๋ฐ -> ChatMessage ๊ฐฑ์ -> ํํ ๋ณํํ์ฌ chatbot์ ํ์
|
396 |
+
msg.submit(
|
397 |
+
user_submit_message,
|
398 |
+
inputs=[msg, conversation_state],
|
399 |
+
outputs=[msg, conversation_state],
|
400 |
+
queue=False
|
401 |
+
).then(
|
402 |
+
respond_wrapper,
|
403 |
+
inputs=[msg, conversation_state, max_tokens, temperature, top_p],
|
404 |
+
outputs=[msg, chatbot], # msg๋ ๋น์์ฃผ๊ณ , chatbot์๋ (user,assistant) ํํ ๋ฆฌ์คํธ
|
405 |
+
)
|
406 |
+
|
407 |
with gr.TabItem("Recommended Best"):
|
408 |
gr.Markdown(
|
409 |
+
"Discover recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard)."
|
|
|
410 |
)
|
411 |
|
412 |
+
# ๋ถ์ ๋ฒํผ ๋ก์ง
|
413 |
space_id_state = gr.State()
|
414 |
tree_structure_state = gr.State()
|
415 |
app_py_content_lines = gr.State()
|
|
|
417 |
analyze_button.click(
|
418 |
analyze_space,
|
419 |
inputs=[url_input],
|
420 |
+
outputs=[
|
421 |
+
app_py_content,
|
422 |
+
tree_view_output,
|
423 |
+
tree_structure_state,
|
424 |
+
space_id_state,
|
425 |
+
summary_output,
|
426 |
+
analysis_output,
|
427 |
+
usage_output,
|
428 |
+
app_py_content_lines
|
429 |
+
]
|
430 |
).then(
|
431 |
lambda space_id: get_file_content(space_id, "requirements.txt"),
|
432 |
inputs=[space_id_state],
|
433 |
outputs=[requirements_content]
|
434 |
+
).then(
|
435 |
+
lambda lines: gr.update(lines=lines),
|
436 |
+
inputs=[app_py_content_lines],
|
437 |
+
outputs=[app_py_content]
|
438 |
)
|
439 |
|
|
|
|
|
440 |
return demo
|
441 |
|
442 |
except Exception as e:
|
|
|
448 |
try:
|
449 |
print("Starting HuggingFace Space Analyzer...")
|
450 |
demo = create_ui()
|
|
|
|
|
451 |
demo.queue()
|
452 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, debug=True, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
453 |
except Exception as e:
|
454 |
print(f"Error in main: {str(e)}")
|
|
|
455 |
print(traceback.format_exc())
|
456 |
raise
|