Update app-backup1.py
Browse files- app-backup1.py +48 -25
app-backup1.py
CHANGED
@@ -11,6 +11,12 @@ import modelscope_studio.components.legacy as legacy
|
|
11 |
import modelscope_studio.components.antd as antd
|
12 |
from config import DEMO_LIST, SystemPrompt
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
YOUR_API_TOKEN = os.getenv('ANTHROPIC_API_KEY')
|
15 |
client = anthropic.Anthropic(api_key=YOUR_API_TOKEN)
|
16 |
|
@@ -59,6 +65,8 @@ def demo_card_click(e: gr.EventData):
|
|
59 |
index = e._data['component']['index']
|
60 |
return DEMO_LIST[index]['description']
|
61 |
|
|
|
|
|
62 |
with gr.Blocks(css_paths="app.css") as demo:
|
63 |
history = gr.State([])
|
64 |
setting = gr.State({
|
@@ -70,12 +78,14 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
70 |
with antd.Row(gutter=[32, 12]) as layout:
|
71 |
with antd.Col(span=24, md=8):
|
72 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
73 |
-
header
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
79 |
input = antd.InputTextarea(
|
80 |
size="large", allow_clear=True, placeholder="Please enter what kind of application you want")
|
81 |
btn = antd.Button("send", type="primary", size="large")
|
@@ -157,29 +167,42 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
157 |
gr.update(open=True) # code_drawer
|
158 |
]
|
159 |
|
160 |
-
|
|
|
161 |
model="claude-3-5-sonnet-20241022",
|
162 |
max_tokens=7800,
|
163 |
system=system_message,
|
164 |
messages=claude_messages
|
165 |
-
)
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
except Exception as e:
|
185 |
raise ValueError(f'Error calling Claude API: {str(e)}')
|
|
|
11 |
import modelscope_studio.components.antd as antd
|
12 |
from config import DEMO_LIST, SystemPrompt
|
13 |
|
14 |
+
# ํ์ผ ์๋จ์ import ๋ฌธ ์๋์ ์ถ๊ฐ
|
15 |
+
def get_image_base64(image_path):
|
16 |
+
with open(image_path, "rb") as image_file:
|
17 |
+
encoded_string = base64.b64encode(image_file.read()).decode()
|
18 |
+
return encoded_string
|
19 |
+
|
20 |
YOUR_API_TOKEN = os.getenv('ANTHROPIC_API_KEY')
|
21 |
client = anthropic.Anthropic(api_key=YOUR_API_TOKEN)
|
22 |
|
|
|
65 |
index = e._data['component']['index']
|
66 |
return DEMO_LIST[index]['description']
|
67 |
|
68 |
+
|
69 |
+
|
70 |
with gr.Blocks(css_paths="app.css") as demo:
|
71 |
history = gr.State([])
|
72 |
setting = gr.State({
|
|
|
78 |
with antd.Row(gutter=[32, 12]) as layout:
|
79 |
with antd.Col(span=24, md=8):
|
80 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
81 |
+
# ์์ ๋ header ๋ถ๋ถ
|
82 |
+
header = gr.HTML(f"""
|
83 |
+
<div class="left_header">
|
84 |
+
<img src="data:image/gif;base64,{get_image_base64('mouse.gif')}" width="200px" />
|
85 |
+
<h1>MOUSE Coder: WEB</h2>
|
86 |
+
</div>
|
87 |
+
""")
|
88 |
+
|
89 |
input = antd.InputTextarea(
|
90 |
size="large", allow_clear=True, placeholder="Please enter what kind of application you want")
|
91 |
btn = antd.Button("send", type="primary", size="large")
|
|
|
167 |
gr.update(open=True) # code_drawer
|
168 |
]
|
169 |
|
170 |
+
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์ฌ์ฉ
|
171 |
+
with client.messages.stream(
|
172 |
model="claude-3-5-sonnet-20241022",
|
173 |
max_tokens=7800,
|
174 |
system=system_message,
|
175 |
messages=claude_messages
|
176 |
+
) as stream:
|
177 |
+
collected_content = ""
|
178 |
+
for chunk in stream:
|
179 |
+
if chunk.type == "content_block_delta":
|
180 |
+
delta = chunk.delta.text
|
181 |
+
collected_content += delta
|
182 |
+
|
183 |
+
yield [
|
184 |
+
collected_content, # code_output
|
185 |
+
_history, # history
|
186 |
+
None, # sandbox (์์ง ์์ฑ๋์ง ์์์ผ๋ฏ๋ก None)
|
187 |
+
gr.update(active_key="loading"), # state_tab
|
188 |
+
gr.update(open=True) # code_drawer
|
189 |
+
]
|
190 |
+
|
191 |
+
# ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
|
192 |
+
_history = messages_to_history([
|
193 |
+
{'role': Role.SYSTEM, 'content': system_message}
|
194 |
+
] + claude_messages + [{
|
195 |
+
'role': Role.ASSISTANT,
|
196 |
+
'content': collected_content
|
197 |
+
}])
|
198 |
+
|
199 |
+
yield [
|
200 |
+
collected_content, # code_output
|
201 |
+
_history, # history
|
202 |
+
send_to_sandbox(remove_code_block(collected_content)), # sandbox
|
203 |
+
gr.update(active_key="render"), # state_tab
|
204 |
+
gr.update(open=True) # code_drawer
|
205 |
+
]
|
206 |
|
207 |
except Exception as e:
|
208 |
raise ValueError(f'Error calling Claude API: {str(e)}')
|