Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -125,7 +125,6 @@ def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
|
|
125 |
formatted.append({"role": role, "parts": [m.content or ""]})
|
126 |
return formatted
|
127 |
|
128 |
-
|
129 |
import google.generativeai as genai
|
130 |
|
131 |
def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
|
@@ -188,22 +187,14 @@ def explain_usage(app_content: str):
|
|
188 |
except Exception as e:
|
189 |
return f"์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
190 |
|
191 |
-
|
192 |
def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
193 |
"""
|
194 |
Gemini์ ์คํธ๋ฆฌ๋ฐ ์์ฒญ.
|
195 |
-
user_message๊ฐ
|
196 |
"""
|
197 |
-
# ๋น ๋ฉ์์ง ์ฒ๋ฆฌ
|
198 |
if not user_message.strip():
|
199 |
-
|
200 |
-
|
201 |
-
role="assistant",
|
202 |
-
content="(Note: You sent an empty message. No LLM call was made.)"
|
203 |
-
)
|
204 |
-
)
|
205 |
-
yield conversation_state
|
206 |
-
return
|
207 |
|
208 |
print(f"\n=== New Request ===\nUser message: {user_message}")
|
209 |
chat_history = format_chat_history(conversation_state)
|
@@ -273,11 +264,9 @@ def stream_gemini_response(user_message: str, conversation_state: List[ChatMessa
|
|
273 |
)
|
274 |
yield conversation_state
|
275 |
|
276 |
-
|
277 |
def convert_for_messages_format(messages: List[ChatMessage]) -> List[Dict[str, str]]:
|
278 |
"""
|
279 |
-
|
280 |
-
ChatMessage ๋ฆฌ์คํธ -> [{"role": "...", "content": "..."}] ๋ชฉ๋ก์ผ๋ก ๋ณํ
|
281 |
"""
|
282 |
output = []
|
283 |
for msg in messages:
|
@@ -286,18 +275,13 @@ def convert_for_messages_format(messages: List[ChatMessage]) -> List[Dict[str, s
|
|
286 |
|
287 |
def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
|
288 |
conversation_state.append(ChatMessage(role="user", content=msg))
|
289 |
-
# ์
๋ ฅ์ฐฝ
|
290 |
return "", conversation_state
|
291 |
|
292 |
def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
|
293 |
-
"""
|
294 |
-
Gemini ์คํธ๋ฆฌ๋ฐ -> conversation_state ๊ฐฑ์ -> messages format์ ๋ง์ถฐ ๋ณํ ํ ๋ฐํ
|
295 |
-
"""
|
296 |
for updated_messages in stream_gemini_response(message, conversation_state):
|
297 |
-
# ChatMessage -> [{"role":"user"/"assistant", "content": "..."}] ๋ก ๋ณํ
|
298 |
yield "", convert_for_messages_format(updated_messages)
|
299 |
|
300 |
-
|
301 |
def create_ui():
|
302 |
try:
|
303 |
css = """
|
@@ -337,11 +321,10 @@ def create_ui():
|
|
337 |
with gr.TabItem("AI ์ฝ๋์ฑ"):
|
338 |
gr.Markdown("## ์์ ๋ฅผ ์
๋ ฅ ๋๋ ์์ค ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ๊ณ ์ง๋ฌธํ์ธ์")
|
339 |
|
340 |
-
# Chatbot: type="messages"
|
341 |
chatbot = gr.Chatbot(
|
342 |
label="๋ํ",
|
343 |
height=400,
|
344 |
-
type="messages"
|
345 |
)
|
346 |
|
347 |
msg = gr.Textbox(
|
@@ -375,7 +358,6 @@ def create_ui():
|
|
375 |
]
|
376 |
gr.Examples(examples, inputs=msg)
|
377 |
|
378 |
-
# ๋ํ ์ํ
|
379 |
conversation_state = gr.State([])
|
380 |
|
381 |
msg.submit(
|
@@ -429,7 +411,6 @@ def create_ui():
|
|
429 |
print(traceback.format_exc())
|
430 |
raise
|
431 |
|
432 |
-
|
433 |
if __name__ == "__main__":
|
434 |
try:
|
435 |
print("Starting HuggingFace Space Analyzer...")
|
|
|
125 |
formatted.append({"role": role, "parts": [m.content or ""]})
|
126 |
return formatted
|
127 |
|
|
|
128 |
import google.generativeai as genai
|
129 |
|
130 |
def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
|
|
|
187 |
except Exception as e:
|
188 |
return f"์ฌ์ฉ๋ฒ ์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
189 |
|
|
|
190 |
def stream_gemini_response(user_message: str, conversation_state: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
191 |
"""
|
192 |
Gemini์ ์คํธ๋ฆฌ๋ฐ ์์ฒญ.
|
193 |
+
user_message๊ฐ ๋น์ด ์์ผ๋ฉด, ์๋ฌด๊ฒ๋ ํ์ง ์๊ณ return.
|
194 |
"""
|
|
|
195 |
if not user_message.strip():
|
196 |
+
# ์ฌ์ฉ์ ๋ฉ์์ง๊ฐ ๋น ๊ฒฝ์ฐ: ์๋ฌด ๋ฉ์์ง๋ ์ถ๊ฐ ์ ํจ, LLM ํธ์ถ๋ ์ ํจ.
|
197 |
+
return # yield๋ ์์ด ๊ทธ๋ฅ ์ข
๋ฃ
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
print(f"\n=== New Request ===\nUser message: {user_message}")
|
200 |
chat_history = format_chat_history(conversation_state)
|
|
|
264 |
)
|
265 |
yield conversation_state
|
266 |
|
|
|
267 |
def convert_for_messages_format(messages: List[ChatMessage]) -> List[Dict[str, str]]:
|
268 |
"""
|
269 |
+
ChatMessage ๋ฆฌ์คํธ -> [{"role":"...", "content":"..."}] ๋ชฉ๋ก
|
|
|
270 |
"""
|
271 |
output = []
|
272 |
for msg in messages:
|
|
|
275 |
|
276 |
def user_submit_message(msg: str, conversation_state: List[ChatMessage]):
|
277 |
conversation_state.append(ChatMessage(role="user", content=msg))
|
278 |
+
# ์
๋ ฅ์ฐฝ ๋น์
|
279 |
return "", conversation_state
|
280 |
|
281 |
def respond_wrapper(message: str, conversation_state: List[ChatMessage], max_tokens, temperature, top_p):
|
|
|
|
|
|
|
282 |
for updated_messages in stream_gemini_response(message, conversation_state):
|
|
|
283 |
yield "", convert_for_messages_format(updated_messages)
|
284 |
|
|
|
285 |
def create_ui():
|
286 |
try:
|
287 |
css = """
|
|
|
321 |
with gr.TabItem("AI ์ฝ๋์ฑ"):
|
322 |
gr.Markdown("## ์์ ๋ฅผ ์
๋ ฅ ๋๋ ์์ค ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ๊ณ ์ง๋ฌธํ์ธ์")
|
323 |
|
|
|
324 |
chatbot = gr.Chatbot(
|
325 |
label="๋ํ",
|
326 |
height=400,
|
327 |
+
type="messages"
|
328 |
)
|
329 |
|
330 |
msg = gr.Textbox(
|
|
|
358 |
]
|
359 |
gr.Examples(examples, inputs=msg)
|
360 |
|
|
|
361 |
conversation_state = gr.State([])
|
362 |
|
363 |
msg.submit(
|
|
|
411 |
print(traceback.format_exc())
|
412 |
raise
|
413 |
|
|
|
414 |
if __name__ == "__main__":
|
415 |
try:
|
416 |
print("Starting HuggingFace Space Analyzer...")
|