Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -68,7 +68,7 @@ def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
|
|
68 |
return tree_data["error"]
|
69 |
formatted = f"{indent}{'π' if tree_data.get('type') == 'directory' else 'π'} {tree_data.get('name', 'Unknown')}\n"
|
70 |
if tree_data.get("type") == "directory":
|
71 |
-
# λλ ν 리λ₯Ό λ¨Όμ , νμΌμ λμ€μ
|
72 |
for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
|
73 |
formatted += format_tree_structure(child, indent + " ")
|
74 |
return formatted
|
@@ -105,7 +105,6 @@ def analyze_space(url: str, progress=gr.Progress()):
|
|
105 |
progress(0.9, desc="μ¬μ©λ² μ€λͺ
μμ± μ€...")
|
106 |
usage = explain_usage(app_content)
|
107 |
|
108 |
-
# lines μ μ‘°μ
|
109 |
lines_for_app_py = adjust_lines_for_code(app_content)
|
110 |
|
111 |
progress(1.0, desc="μλ£")
|
@@ -124,31 +123,30 @@ def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int
|
|
124 |
num_lines = len(code_content.split('\n'))
|
125 |
return min(max(num_lines, min_lines), max_lines)
|
126 |
|
127 |
-
|
128 |
# --------------------------------------------------
|
129 |
-
# Gemini 2.0 Flash Thinking λͺ¨λΈ
|
130 |
# --------------------------------------------------
|
131 |
from gradio import ChatMessage
|
132 |
|
133 |
def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
|
134 |
"""
|
135 |
ChatMessage λͺ©λ‘μ Gemini λͺ¨λΈμ΄ μ΄ν΄ν μ μλ νμμΌλ‘ λ³ν
|
136 |
-
(Thinking
|
137 |
"""
|
138 |
formatted = []
|
139 |
for m in messages:
|
140 |
-
# 'Thinking'
|
141 |
-
if hasattr(m, "metadata") and m.metadata:
|
142 |
continue
|
143 |
role = "assistant" if m.role == "assistant" else "user"
|
144 |
formatted.append({"role": role, "parts": [m.content or ""]})
|
145 |
return formatted
|
146 |
|
147 |
|
|
|
|
|
148 |
def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
|
149 |
"""
|
150 |
-
μμ€ν
|
151 |
-
μ΅μ’
μλ΅ ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
152 |
"""
|
153 |
init_msgs = [
|
154 |
ChatMessage(role="system", content=system_message),
|
@@ -161,7 +159,6 @@ def gemini_chat_completion(system_message: str, user_message: str, max_tokens: i
|
|
161 |
for chunk in chat.send_message(user_message, stream=True):
|
162 |
parts = chunk.candidates[0].content.parts
|
163 |
if len(parts) == 2:
|
164 |
-
# Thinking + μ΅μ’
μλ΅
|
165 |
final += parts[1].text
|
166 |
else:
|
167 |
final += parts[0].text
|
@@ -180,7 +177,6 @@ def summarize_code(app_content: str):
|
|
180 |
|
181 |
|
182 |
def analyze_code(app_content: str):
|
183 |
-
# μμ€ν
ν둬ννΈμ 'λ₯μ½νΉ' μλ΄λ¬Έ μΆκ°
|
184 |
system_msg = (
|
185 |
"You are a deep thinking AI. You may use extremely long chains of thought to deeply consider the problem "
|
186 |
"and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. "
|
@@ -201,7 +197,6 @@ def analyze_code(app_content: str):
|
|
201 |
|
202 |
|
203 |
def explain_usage(app_content: str):
|
204 |
-
# μμ€ν
ν둬ννΈμ 'λ₯μ½νΉ' μλ΄λ¬Έ μΆκ°
|
205 |
system_msg = (
|
206 |
"You are a deep thinking AI. You may use extremely long chains of thought to deeply consider the problem "
|
207 |
"and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. "
|
@@ -217,25 +212,30 @@ def explain_usage(app_content: str):
|
|
217 |
|
218 |
def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
219 |
"""
|
220 |
-
|
221 |
-
|
222 |
"""
|
223 |
-
#
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
228 |
|
229 |
-
|
230 |
chat_history = format_chat_history(conversation_state)
|
231 |
chat = model.start_chat(history=chat_history)
|
232 |
-
|
233 |
response = chat.send_message(user_message, stream=True)
|
|
|
234 |
thought_buffer = ""
|
235 |
response_buffer = ""
|
236 |
thinking_complete = False
|
237 |
|
238 |
-
# 'Thinking' νμμ©
|
239 |
conversation_state.append(
|
240 |
ChatMessage(
|
241 |
role="assistant",
|
@@ -290,7 +290,7 @@ def stream_gemini_response(user_message: str, conversation_state: List[ChatMessa
|
|
290 |
conversation_state.append(
|
291 |
ChatMessage(
|
292 |
role="assistant",
|
293 |
-
content=f"I apologize, but
|
294 |
)
|
295 |
)
|
296 |
yield conversation_state
|
@@ -298,7 +298,7 @@ def stream_gemini_response(user_message: str, conversation_state: List[ChatMessa
|
|
298 |
|
299 |
def convert_to_display_tuples(messages: List[ChatMessage]) -> List[Tuple[str, str]]:
|
300 |
"""
|
301 |
-
|
302 |
"""
|
303 |
result = []
|
304 |
i = 0
|
@@ -321,8 +321,7 @@ def convert_to_display_tuples(messages: List[ChatMessage]) -> List[Tuple[str, st
|
|
321 |
|
322 |
def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
|
323 |
"""
|
324 |
-
μ¬μ©μκ° λ©μμ§λ₯Ό μ
λ ₯ν λ
|
325 |
-
ChatMessage 리μ€νΈ(conversation_state)μ user λ©μμ§λ₯Ό μΆκ°ν λ€ λ°ν.
|
326 |
"""
|
327 |
conversation_state.append(ChatMessage(role="user", content=msg))
|
328 |
# μ
λ ₯μ°½μ λΉμμ€
|
@@ -331,11 +330,9 @@ def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
|
|
331 |
|
332 |
def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
|
333 |
"""
|
334 |
-
|
335 |
-
νλ©΄μλ (user, assistant) ννμ λ°ννλ€.
|
336 |
"""
|
337 |
for updated_messages in stream_gemini_response(message, conversation_state):
|
338 |
-
# νλ©΄ νμμ© (user, assistant) ννλ‘ λ³ν
|
339 |
yield "", convert_to_display_tuples(updated_messages)
|
340 |
|
341 |
|
@@ -381,10 +378,11 @@ def create_ui():
|
|
381 |
with gr.TabItem("AI μ½λμ±"):
|
382 |
gr.Markdown("## μμ λ₯Ό μ
λ ₯ λλ μμ€ μ½λλ₯Ό λΆμ¬λ£κ³ μ§λ¬ΈνμΈμ")
|
383 |
|
384 |
-
# Chatbot
|
385 |
chatbot = gr.Chatbot(
|
386 |
label="λν",
|
387 |
-
height=400
|
|
|
388 |
)
|
389 |
|
390 |
msg = gr.Textbox(
|
@@ -392,7 +390,6 @@ def create_ui():
|
|
392 |
placeholder="λ©μμ§λ₯Ό μ
λ ₯νμΈμ..."
|
393 |
)
|
394 |
|
395 |
-
# μ¨κ²¨μ§ νλΌλ―Έν°
|
396 |
max_tokens = gr.Slider(
|
397 |
minimum=1, maximum=8000,
|
398 |
value=4000, label="Max Tokens",
|
@@ -419,12 +416,11 @@ def create_ui():
|
|
419 |
]
|
420 |
gr.Examples(examples, inputs=msg)
|
421 |
|
422 |
-
# λν μν(μ±ν
κΈ°λ‘)λ ChatMessage κ°μ²΄λ‘λ§
|
423 |
conversation_state = gr.State([])
|
424 |
|
425 |
-
#
|
426 |
-
#
|
427 |
-
# 2) respond_wrapper -> Gemini μ€νΈλ¦¬λ° -> λν state κ°±μ -> (user,assistant) νν λ³ν
|
428 |
msg.submit(
|
429 |
user_submit_message,
|
430 |
inputs=[msg, conversation_state],
|
@@ -433,7 +429,7 @@ def create_ui():
|
|
433 |
).then(
|
434 |
respond_wrapper,
|
435 |
inputs=[msg, conversation_state, max_tokens, temperature, top_p],
|
436 |
-
outputs=[msg, chatbot],
|
437 |
)
|
438 |
|
439 |
with gr.TabItem("Recommended Best"):
|
@@ -441,7 +437,7 @@ def create_ui():
|
|
441 |
"Discover recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard)."
|
442 |
)
|
443 |
|
444 |
-
# λΆμ
|
445 |
space_id_state = gr.State()
|
446 |
tree_structure_state = gr.State()
|
447 |
app_py_content_lines = gr.State()
|
|
|
68 |
return tree_data["error"]
|
69 |
formatted = f"{indent}{'π' if tree_data.get('type') == 'directory' else 'π'} {tree_data.get('name', 'Unknown')}\n"
|
70 |
if tree_data.get("type") == "directory":
|
71 |
+
# λλ ν 리λ₯Ό λ¨Όμ , νμΌμ λμ€μ νμ
|
72 |
for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
|
73 |
formatted += format_tree_structure(child, indent + " ")
|
74 |
return formatted
|
|
|
105 |
progress(0.9, desc="μ¬μ©λ² μ€λͺ
μμ± μ€...")
|
106 |
usage = explain_usage(app_content)
|
107 |
|
|
|
108 |
lines_for_app_py = adjust_lines_for_code(app_content)
|
109 |
|
110 |
progress(1.0, desc="μλ£")
|
|
|
123 |
num_lines = len(code_content.split('\n'))
|
124 |
return min(max(num_lines, min_lines), max_lines)
|
125 |
|
|
|
126 |
# --------------------------------------------------
|
127 |
+
# Gemini 2.0 Flash Thinking λͺ¨λΈ (LLM) ν¨μλ€
|
128 |
# --------------------------------------------------
|
129 |
from gradio import ChatMessage
|
130 |
|
131 |
def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
|
132 |
"""
|
133 |
ChatMessage λͺ©λ‘μ Gemini λͺ¨λΈμ΄ μ΄ν΄ν μ μλ νμμΌλ‘ λ³ν
|
134 |
+
(Thinking λ©νλ°μ΄ν°κ° μλ λ©μμ§λ 무μ)
|
135 |
"""
|
136 |
formatted = []
|
137 |
for m in messages:
|
138 |
+
if hasattr(m, "metadata") and m.metadata: # 'Thinking' λ©μμ§λ μ μΈ
|
|
|
139 |
continue
|
140 |
role = "assistant" if m.role == "assistant" else "user"
|
141 |
formatted.append({"role": role, "parts": [m.content or ""]})
|
142 |
return formatted
|
143 |
|
144 |
|
145 |
+
import google.generativeai as genai
|
146 |
+
|
147 |
def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
|
148 |
"""
|
149 |
+
μμ€ν
& μ μ λ©μμ§λ‘ Gemini λͺ¨λΈμκ² μ€νΈλ¦¬λ° μμ². μ΅μ’
ν
μ€νΈ λ°ν
|
|
|
150 |
"""
|
151 |
init_msgs = [
|
152 |
ChatMessage(role="system", content=system_message),
|
|
|
159 |
for chunk in chat.send_message(user_message, stream=True):
|
160 |
parts = chunk.candidates[0].content.parts
|
161 |
if len(parts) == 2:
|
|
|
162 |
final += parts[1].text
|
163 |
else:
|
164 |
final += parts[0].text
|
|
|
177 |
|
178 |
|
179 |
def analyze_code(app_content: str):
|
|
|
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. "
|
|
|
197 |
|
198 |
|
199 |
def explain_usage(app_content: str):
|
|
|
200 |
system_msg = (
|
201 |
"You are a deep thinking AI. You may use extremely long chains of thought to deeply consider the problem "
|
202 |
"and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. "
|
|
|
212 |
|
213 |
def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
214 |
"""
|
215 |
+
Geminiμ μ€νΈλ¦¬λ° μμ².
|
216 |
+
λΉ λ©μμ§λ μ¬κΈ°μ μ²λ¦¬(μλ¬ μμ΄)νλλ‘ ν¨.
|
217 |
"""
|
218 |
+
# λ§μ½ user_messageκ° μμ λΉ λ¬Έμμ΄μ΄λΌλ©΄, λͺ¨λΈ νΈμΆ λμ κ°λ¨ μλ΄
|
219 |
+
if not user_message.strip():
|
220 |
+
conversation_state.append(
|
221 |
+
ChatMessage(
|
222 |
+
role="assistant",
|
223 |
+
content="(Note: You sent an empty message. No LLM call was made.)"
|
224 |
+
)
|
225 |
+
)
|
226 |
+
yield conversation_state
|
227 |
+
return
|
228 |
|
229 |
+
print(f"\n=== New Request ===\nUser message: {user_message}")
|
230 |
chat_history = format_chat_history(conversation_state)
|
231 |
chat = model.start_chat(history=chat_history)
|
|
|
232 |
response = chat.send_message(user_message, stream=True)
|
233 |
+
|
234 |
thought_buffer = ""
|
235 |
response_buffer = ""
|
236 |
thinking_complete = False
|
237 |
|
238 |
+
# 'Thinking' νμμ© λ©μμ§ μΆκ°
|
239 |
conversation_state.append(
|
240 |
ChatMessage(
|
241 |
role="assistant",
|
|
|
290 |
conversation_state.append(
|
291 |
ChatMessage(
|
292 |
role="assistant",
|
293 |
+
content=f"I apologize, but encountered an error: {str(e)}"
|
294 |
)
|
295 |
)
|
296 |
yield conversation_state
|
|
|
298 |
|
299 |
def convert_to_display_tuples(messages: List[ChatMessage]) -> List[Tuple[str, str]]:
|
300 |
"""
|
301 |
+
ChatMessage 리μ€νΈ -> (user, assistant) νν 리μ€νΈ
|
302 |
"""
|
303 |
result = []
|
304 |
i = 0
|
|
|
321 |
|
322 |
def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
|
323 |
"""
|
324 |
+
μ¬μ©μκ° λ©μμ§λ₯Ό μ
λ ₯ν λ νΈμΆ
|
|
|
325 |
"""
|
326 |
conversation_state.append(ChatMessage(role="user", content=msg))
|
327 |
# μ
λ ₯μ°½μ λΉμμ€
|
|
|
330 |
|
331 |
def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
|
332 |
"""
|
333 |
+
Geminiμ μ€νΈλ¦¬λ° μμ² -> λν μ΄λ ₯μ κ°±μ -> (user, assistant) ννλ‘ λ³ννμ¬ νλ©΄μ νμ
|
|
|
334 |
"""
|
335 |
for updated_messages in stream_gemini_response(message, conversation_state):
|
|
|
336 |
yield "", convert_to_display_tuples(updated_messages)
|
337 |
|
338 |
|
|
|
378 |
with gr.TabItem("AI μ½λμ±"):
|
379 |
gr.Markdown("## μμ λ₯Ό μ
λ ₯ λλ μμ€ μ½λλ₯Ό λΆμ¬λ£κ³ μ§λ¬ΈνμΈμ")
|
380 |
|
381 |
+
# Chatbotμ type="messages"λ‘ μ€μ (κΆμ₯)
|
382 |
chatbot = gr.Chatbot(
|
383 |
label="λν",
|
384 |
+
height=400,
|
385 |
+
type="messages"
|
386 |
)
|
387 |
|
388 |
msg = gr.Textbox(
|
|
|
390 |
placeholder="λ©μμ§λ₯Ό μ
λ ₯νμΈμ..."
|
391 |
)
|
392 |
|
|
|
393 |
max_tokens = gr.Slider(
|
394 |
minimum=1, maximum=8000,
|
395 |
value=4000, label="Max Tokens",
|
|
|
416 |
]
|
417 |
gr.Examples(examples, inputs=msg)
|
418 |
|
419 |
+
# λν μν(μ±ν
κΈ°λ‘)λ ChatMessage κ°μ²΄λ‘λ§ κ΄λ¦¬
|
420 |
conversation_state = gr.State([])
|
421 |
|
422 |
+
# 1) μ μ λ©μμ§ μ
λ ₯ -> user_submit_message
|
423 |
+
# 2) respond_wrapper -> Gemini μ€νΈλ¦¬λ° -> λν μ
λ°μ΄νΈ -> (user,assistant) λ³ννμ¬ chatbot νμ
|
|
|
424 |
msg.submit(
|
425 |
user_submit_message,
|
426 |
inputs=[msg, conversation_state],
|
|
|
429 |
).then(
|
430 |
respond_wrapper,
|
431 |
inputs=[msg, conversation_state, max_tokens, temperature, top_p],
|
432 |
+
outputs=[msg, chatbot],
|
433 |
)
|
434 |
|
435 |
with gr.TabItem("Recommended Best"):
|
|
|
437 |
"Discover recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard)."
|
438 |
)
|
439 |
|
440 |
+
# λΆμ ν λ‘μ§
|
441 |
space_id_state = gr.State()
|
442 |
tree_structure_state = gr.State()
|
443 |
app_py_content_lines = gr.State()
|