Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
from gradio import ChatMessage
|
@@ -191,6 +190,18 @@ def convert_chat_history(messages: List[ChatMessage]) -> List[Tuple[str, str]]:
|
|
191 |
i += 1
|
192 |
return conv
|
193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
def stream_gemini_response(user_message: str, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
195 |
if not user_message.strip():
|
196 |
messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed."))
|
@@ -271,7 +282,7 @@ def stream_gemini_response(user_message: str, messages: List[ChatMessage]) -> It
|
|
271 |
|
272 |
def respond(message: str, history: List[ChatMessage]) -> Iterator[List[Tuple[str, str]]]:
|
273 |
"""
|
274 |
-
|
275 |
"""
|
276 |
for updated_messages in stream_gemini_response(message, history):
|
277 |
yield convert_chat_history(updated_messages)
|
@@ -280,6 +291,13 @@ def user_message(msg: str, history: List[ChatMessage]) -> Tuple[str, List[ChatMe
|
|
280 |
history.append(ChatMessage(role="user", content=msg))
|
281 |
return "", history
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
# --------------------------------------------------
|
284 |
# Gradio UI 구성
|
285 |
# --------------------------------------------------
|
@@ -364,7 +382,7 @@ def create_ui():
|
|
364 |
"""
|
365 |
|
366 |
with gr.Blocks(theme="default", css=css) as demo:
|
367 |
-
gr.Markdown("# MOUSE Space
|
368 |
|
369 |
with gr.Tabs() as tabs:
|
370 |
with gr.TabItem("분석"):
|
@@ -403,7 +421,8 @@ def create_ui():
|
|
403 |
elem_classes="full-height code-box"
|
404 |
)
|
405 |
|
406 |
-
with gr.TabItem("AI
|
|
|
407 |
# 채팅 박스 높이를 400px로 지정하여 화면 높이에 맞게 줄임.
|
408 |
chatbot = gr.Chatbot(
|
409 |
label="대화",
|
@@ -424,15 +443,11 @@ def create_ui():
|
|
424 |
["사용 방법과 차별점, 특징, 강점을 중심으로 4000 토큰 이상 유튜브 영상 스크립트 형태로 작성하라"],
|
425 |
["본 서비스를 SEO 최적화하여 블로그 포스트로 4000 토큰 이상 작성하라"],
|
426 |
["특허 출원에 활용할 혁신적인 창의 발명 내용을 중심으로 4000 토큰 이상 작성하라."],
|
|
|
427 |
["계속 이어서 답변하라"],
|
428 |
]
|
429 |
gr.Examples(examples, inputs=msg)
|
430 |
|
431 |
-
def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
|
432 |
-
# 반환되는 제너레이터의 각 단계마다 채팅 기록을 튜플 목록으로 변환합니다.
|
433 |
-
for updated in stream_gemini_response(message, chat_history):
|
434 |
-
yield "", convert_chat_history(updated)
|
435 |
-
|
436 |
msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
|
437 |
|
438 |
with gr.TabItem("Recommended Best"):
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from gradio import ChatMessage
|
|
|
190 |
i += 1
|
191 |
return conv
|
192 |
|
193 |
+
def convert_to_chatmessage(history: List[Tuple[str, str]]) -> List[ChatMessage]:
|
194 |
+
"""
|
195 |
+
튜플 목록을 ChatMessage 객체 목록으로 변환합니다.
|
196 |
+
"""
|
197 |
+
new_history = []
|
198 |
+
for tup in history:
|
199 |
+
if tup[0]:
|
200 |
+
new_history.append(ChatMessage(role="user", content=tup[0]))
|
201 |
+
if tup[1]:
|
202 |
+
new_history.append(ChatMessage(role="assistant", content=tup[1]))
|
203 |
+
return new_history
|
204 |
+
|
205 |
def stream_gemini_response(user_message: str, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
|
206 |
if not user_message.strip():
|
207 |
messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed."))
|
|
|
282 |
|
283 |
def respond(message: str, history: List[ChatMessage]) -> Iterator[List[Tuple[str, str]]]:
|
284 |
"""
|
285 |
+
stream_gemini_response()를 호출한 후, 출력 결과를 튜플 목록으로 변환하여 반환합니다.
|
286 |
"""
|
287 |
for updated_messages in stream_gemini_response(message, history):
|
288 |
yield convert_chat_history(updated_messages)
|
|
|
291 |
history.append(ChatMessage(role="user", content=msg))
|
292 |
return "", history
|
293 |
|
294 |
+
def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
|
295 |
+
# chat_history가 튜플 목록이라면 ChatMessage 객체로 변환
|
296 |
+
if chat_history and isinstance(chat_history[0], tuple):
|
297 |
+
chat_history = convert_to_chatmessage(chat_history)
|
298 |
+
for updated in stream_gemini_response(message, chat_history):
|
299 |
+
yield "", convert_chat_history(updated)
|
300 |
+
|
301 |
# --------------------------------------------------
|
302 |
# Gradio UI 구성
|
303 |
# --------------------------------------------------
|
|
|
382 |
"""
|
383 |
|
384 |
with gr.Blocks(theme="default", css=css) as demo:
|
385 |
+
gr.Markdown("# MOUSE: Space Research Thinking", elem_classes="header-markdown")
|
386 |
|
387 |
with gr.Tabs() as tabs:
|
388 |
with gr.TabItem("분석"):
|
|
|
421 |
elem_classes="full-height code-box"
|
422 |
)
|
423 |
|
424 |
+
with gr.TabItem("AI 코드챗"):
|
425 |
+
gr.Markdown("## : 예제를 입력/선택하고, 이어서 복사한 app.py 소스코드를 붙여 넣으세요", elem_classes="header-markdown")
|
426 |
# 채팅 박스 높이를 400px로 지정하여 화면 높이에 맞게 줄임.
|
427 |
chatbot = gr.Chatbot(
|
428 |
label="대화",
|
|
|
443 |
["사용 방법과 차별점, 특징, 강점을 중심으로 4000 토큰 이상 유튜브 영상 스크립트 형태로 작성하라"],
|
444 |
["본 서비스를 SEO 최적화하여 블로그 포스트로 4000 토큰 이상 작성하라"],
|
445 |
["특허 출원에 활용할 혁신적인 창의 발명 내용을 중심으로 4000 토큰 이상 작성하라."],
|
446 |
+
["혁신적이고 논리적인 전문 논문의 형식으로 4000 토큰 이상 작성하라."],
|
447 |
["계속 이어서 답변하라"],
|
448 |
]
|
449 |
gr.Examples(examples, inputs=msg)
|
450 |
|
|
|
|
|
|
|
|
|
|
|
451 |
msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
|
452 |
|
453 |
with gr.TabItem("Recommended Best"):
|