Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
from http import HTTPStatus
|
| 4 |
from typing import Dict, List, Optional, Tuple
|
| 5 |
import base64
|
|
@@ -11,9 +12,7 @@ import modelscope_studio.components.base as ms
|
|
| 11 |
import modelscope_studio.components.legacy as legacy
|
| 12 |
import modelscope_studio.components.antd as antd
|
| 13 |
from config import DEMO_LIST, SystemPrompt
|
| 14 |
-
import random # 추가
|
| 15 |
|
| 16 |
-
# 파일 상단의 import 문 아래에 추가
|
| 17 |
def get_image_base64(image_path):
|
| 18 |
with open(image_path, "rb") as image_file:
|
| 19 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
|
@@ -63,18 +62,6 @@ def send_to_sandbox(code):
|
|
| 63 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
| 64 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
DEMO_CACHE = {}
|
| 69 |
-
|
| 70 |
-
def demo_card_click(e: gr.EventData):
|
| 71 |
-
index = e._data['component']['index']
|
| 72 |
-
if index not in DEMO_CACHE:
|
| 73 |
-
DEMO_CACHE[index] = DEMO_LIST[index]['description']
|
| 74 |
-
return DEMO_CACHE[index]
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
with gr.Blocks(css_paths="app.css") as demo:
|
| 79 |
history = gr.State([])
|
| 80 |
setting = gr.State({
|
|
@@ -82,13 +69,69 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 82 |
})
|
| 83 |
|
| 84 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
with ms.Application() as app:
|
| 88 |
with antd.ConfigProvider():
|
| 89 |
-
# 메인 컨텐츠를 위한 Row
|
| 90 |
with antd.Row(gutter=[32, 12]) as layout:
|
| 91 |
-
# 좌측 패널
|
| 92 |
with antd.Col(span=24, md=8):
|
| 93 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
| 94 |
header = gr.HTML(f"""
|
|
@@ -97,7 +140,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 97 |
<h1 style="font-size: 20px;">AI 코딩 코파일럿: MOUSE(WEB)</h2>
|
| 98 |
</div>
|
| 99 |
""")
|
| 100 |
-
# 랜덤 예제를 placeholder로 사용
|
| 101 |
input = antd.InputTextarea(
|
| 102 |
size="large",
|
| 103 |
allow_clear=True,
|
|
@@ -107,7 +149,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 107 |
btn = antd.Button("send", type="primary", size="large")
|
| 108 |
clear_btn = antd.Button("clear history", type="default", size="large")
|
| 109 |
|
| 110 |
-
# Modal과 Drawer 컴포넌트들
|
| 111 |
with antd.Modal(open=False, title="set system Prompt", width="800px") as system_prompt_modal:
|
| 112 |
systemPromptInput = antd.InputTextarea(
|
| 113 |
SystemPrompt, auto_size=True)
|
|
@@ -118,7 +159,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 118 |
with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
|
| 119 |
history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
|
| 120 |
|
| 121 |
-
# 우측 패널
|
| 122 |
with antd.Col(span=24, md=16):
|
| 123 |
with ms.Div(elem_classes="right_panel"):
|
| 124 |
with antd.Flex(gap="small", elem_classes="setting-buttons"):
|
|
@@ -136,9 +176,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 136 |
with antd.Tabs.Item(key="render"):
|
| 137 |
sandbox = gr.HTML(elem_classes="html_content")
|
| 138 |
|
| 139 |
-
# Examples 섹션 제거
|
| 140 |
-
|
| 141 |
-
# 버튼 이벤트 핸들러
|
| 142 |
settingPromptBtn.click(lambda: gr.update(
|
| 143 |
open=True), inputs=[], outputs=[system_prompt_modal])
|
| 144 |
system_prompt_modal.ok(lambda input: ({"system": input}, gr.update(
|
|
@@ -163,6 +200,5 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 163 |
|
| 164 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
| 165 |
|
| 166 |
-
|
| 167 |
if __name__ == "__main__":
|
| 168 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import random
|
| 4 |
from http import HTTPStatus
|
| 5 |
from typing import Dict, List, Optional, Tuple
|
| 6 |
import base64
|
|
|
|
| 12 |
import modelscope_studio.components.legacy as legacy
|
| 13 |
import modelscope_studio.components.antd as antd
|
| 14 |
from config import DEMO_LIST, SystemPrompt
|
|
|
|
| 15 |
|
|
|
|
| 16 |
def get_image_base64(image_path):
|
| 17 |
with open(image_path, "rb") as image_file:
|
| 18 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
|
|
|
| 62 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
| 63 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Blocks(css_paths="app.css") as demo:
|
| 66 |
history = gr.State([])
|
| 67 |
setting = gr.State({
|
|
|
|
| 69 |
})
|
| 70 |
|
| 71 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 72 |
+
if query is None:
|
| 73 |
+
query = ''
|
| 74 |
+
if _history is None:
|
| 75 |
+
_history = []
|
| 76 |
+
|
| 77 |
+
messages = history_to_messages(_history, _setting['system'])
|
| 78 |
+
system_message = messages[0]['content']
|
| 79 |
+
|
| 80 |
+
claude_messages = [
|
| 81 |
+
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
| 82 |
+
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
yield [
|
| 87 |
+
"Generating code...",
|
| 88 |
+
_history,
|
| 89 |
+
None,
|
| 90 |
+
gr.update(active_key="loading"),
|
| 91 |
+
gr.update(open=True)
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
with client.messages.stream(
|
| 95 |
+
model="claude-3-5-sonnet-20241022",
|
| 96 |
+
max_tokens=7800,
|
| 97 |
+
system=system_message,
|
| 98 |
+
messages=claude_messages
|
| 99 |
+
) as stream:
|
| 100 |
+
collected_content = ""
|
| 101 |
+
for chunk in stream:
|
| 102 |
+
if chunk.type == "content_block_delta":
|
| 103 |
+
delta = chunk.delta.text
|
| 104 |
+
collected_content += delta
|
| 105 |
+
|
| 106 |
+
yield [
|
| 107 |
+
collected_content,
|
| 108 |
+
_history,
|
| 109 |
+
None,
|
| 110 |
+
gr.update(active_key="loading"),
|
| 111 |
+
gr.update(open=True)
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
_history = messages_to_history([
|
| 115 |
+
{'role': Role.SYSTEM, 'content': system_message}
|
| 116 |
+
] + claude_messages + [{
|
| 117 |
+
'role': Role.ASSISTANT,
|
| 118 |
+
'content': collected_content
|
| 119 |
+
}])
|
| 120 |
+
|
| 121 |
+
yield [
|
| 122 |
+
collected_content,
|
| 123 |
+
_history,
|
| 124 |
+
send_to_sandbox(remove_code_block(collected_content)),
|
| 125 |
+
gr.update(active_key="render"),
|
| 126 |
+
gr.update(open=True)
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
raise ValueError(f'Error calling Claude API: {str(e)}')
|
| 131 |
|
| 132 |
with ms.Application() as app:
|
| 133 |
with antd.ConfigProvider():
|
|
|
|
| 134 |
with antd.Row(gutter=[32, 12]) as layout:
|
|
|
|
| 135 |
with antd.Col(span=24, md=8):
|
| 136 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
| 137 |
header = gr.HTML(f"""
|
|
|
|
| 140 |
<h1 style="font-size: 20px;">AI 코딩 코파일럿: MOUSE(WEB)</h2>
|
| 141 |
</div>
|
| 142 |
""")
|
|
|
|
| 143 |
input = antd.InputTextarea(
|
| 144 |
size="large",
|
| 145 |
allow_clear=True,
|
|
|
|
| 149 |
btn = antd.Button("send", type="primary", size="large")
|
| 150 |
clear_btn = antd.Button("clear history", type="default", size="large")
|
| 151 |
|
|
|
|
| 152 |
with antd.Modal(open=False, title="set system Prompt", width="800px") as system_prompt_modal:
|
| 153 |
systemPromptInput = antd.InputTextarea(
|
| 154 |
SystemPrompt, auto_size=True)
|
|
|
|
| 159 |
with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
|
| 160 |
history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
|
| 161 |
|
|
|
|
| 162 |
with antd.Col(span=24, md=16):
|
| 163 |
with ms.Div(elem_classes="right_panel"):
|
| 164 |
with antd.Flex(gap="small", elem_classes="setting-buttons"):
|
|
|
|
| 176 |
with antd.Tabs.Item(key="render"):
|
| 177 |
sandbox = gr.HTML(elem_classes="html_content")
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
settingPromptBtn.click(lambda: gr.update(
|
| 180 |
open=True), inputs=[], outputs=[system_prompt_modal])
|
| 181 |
system_prompt_modal.ok(lambda input: ({"system": input}, gr.update(
|
|
|
|
| 200 |
|
| 201 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
| 202 |
|
|
|
|
| 203 |
if __name__ == "__main__":
|
| 204 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|