seawolf2357 commited on
Commit
f1158e0
ยท
verified ยท
1 Parent(s): 47719ad

Update app-backup1.py

Browse files
Files changed (1) hide show
  1. 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 = gr.HTML("""
74
- <div class="left_header">
75
- <img src="//img.alicdn.com/imgextra/i2/O1CN01KDhOma1DUo8oa7OIU_!!6000000000220-1-tps-240-240.gif" width="200px" />
76
- <h1>Claude-3-Sonnet Coder</h2>
77
- </div>
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
- response = client.messages.create(
 
161
  model="claude-3-5-sonnet-20241022",
162
  max_tokens=7800,
163
  system=system_message,
164
  messages=claude_messages
165
- )
166
-
167
- content = response.content[0].text
168
- _history = messages_to_history([
169
- {'role': Role.SYSTEM, 'content': system_message}
170
- ] + claude_messages + [{
171
- 'role': Role.ASSISTANT,
172
- 'content': content
173
- }])
174
-
175
- # ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
176
- yield [
177
- content, # code_output
178
- _history, # history
179
- send_to_sandbox(remove_code_block(content)), # sandbox
180
- gr.update(active_key="render"), # state_tab
181
- gr.update(open=True) # code_drawer
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)}')