seawolf2357 commited on
Commit
dafa74a
ยท
verified ยท
1 Parent(s): 28c9e0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -80
app.py CHANGED
@@ -11,6 +11,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
 
15
  # ํŒŒ์ผ ์ƒ๋‹จ์˜ import ๋ฌธ ์•„๋ž˜์— ์ถ”๊ฐ€
16
  def get_image_base64(image_path):
@@ -73,6 +74,7 @@ def demo_card_click(e: gr.EventData):
73
  return DEMO_CACHE[index]
74
 
75
 
 
76
  with gr.Blocks(css_paths="app.css") as demo:
77
  history = gr.State([])
78
  setting = gr.State({
@@ -80,69 +82,7 @@ with gr.Blocks(css_paths="app.css") as demo:
80
  })
81
 
82
  def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
83
- if query is None:
84
- query = ''
85
- if _history is None:
86
- _history = []
87
-
88
- messages = history_to_messages(_history, _setting['system'])
89
- system_message = messages[0]['content']
90
-
91
- claude_messages = [
92
- {"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
93
- for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
94
- ]
95
-
96
- try:
97
- # ์ค‘๊ฐ„ ์ƒํƒœ ํ‘œ์‹œ
98
- yield [
99
- "Generating code...", # code_output
100
- _history, # history
101
- None, # sandbox
102
- gr.update(active_key="loading"), # state_tab
103
- gr.update(open=True) # code_drawer
104
- ]
105
-
106
- # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต ์‚ฌ์šฉ
107
- with client.messages.stream(
108
- model="claude-3-5-sonnet-20241022",
109
- max_tokens=7800,
110
- system=system_message,
111
- messages=claude_messages
112
- ) as stream:
113
- collected_content = ""
114
- for chunk in stream:
115
- if chunk.type == "content_block_delta":
116
- delta = chunk.delta.text
117
- collected_content += delta
118
-
119
- yield [
120
- collected_content, # code_output
121
- _history, # history
122
- None, # sandbox (์•„์ง ์™„์„ฑ๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ None)
123
- gr.update(active_key="loading"), # state_tab
124
- gr.update(open=True) # code_drawer
125
- ]
126
-
127
- # ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
128
- _history = messages_to_history([
129
- {'role': Role.SYSTEM, 'content': system_message}
130
- ] + claude_messages + [{
131
- 'role': Role.ASSISTANT,
132
- 'content': collected_content
133
- }])
134
-
135
- yield [
136
- collected_content, # code_output
137
- _history, # history
138
- send_to_sandbox(remove_code_block(collected_content)), # sandbox
139
- gr.update(active_key="render"), # state_tab
140
- gr.update(open=True) # code_drawer
141
- ]
142
-
143
- except Exception as e:
144
- raise ValueError(f'Error calling Claude API: {str(e)}')
145
-
146
 
147
  with ms.Application() as app:
148
  with antd.ConfigProvider():
@@ -157,10 +97,11 @@ with gr.Blocks(css_paths="app.css") as demo:
157
  <h1 style="font-size: 20px;">AI ์ฝ”๋”ฉ ์ฝ”ํŒŒ์ผ๋Ÿฟ: MOUSE(WEB)</h2>
158
  </div>
159
  """)
 
160
  input = antd.InputTextarea(
161
  size="large",
162
  allow_clear=True,
163
- placeholder="Please enter what kind of application you want"
164
  )
165
 
166
  btn = antd.Button("send", type="primary", size="large")
@@ -190,22 +131,13 @@ with gr.Blocks(css_paths="app.css") as demo:
190
  with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
191
  with antd.Tabs.Item(key="empty"):
192
  empty = antd.Empty(description="empty input", elem_classes="right_content")
193
- with antd.Tabs.Item(key="loading"):
194
- loading = antd.Spin(True, tip="coding...", size="large", elem_classes="right_content")
195
- with antd.Tabs.Item(key="render"):
196
- sandbox = gr.HTML(elem_classes="html_content")
197
-
198
- # Examples ์„น์…˜์„ ๋ณ„๋„์˜ Row๋กœ ์ถ”๊ฐ€ (์ „์ฒด ํ™”๋ฉด ํ•˜๋‹จ)
199
- with antd.Row(gutter=[0, 24], elem_classes="examples-section"):
200
- with antd.Col(span=24):
201
- antd.Divider("examples")
202
- with antd.Row(gutter=[16, 16]):
203
- with ms.Each(DEMO_LIST):
204
- with antd.Col(span=8): # ํ•œ ์ค„์— 3๊ฐœ์”ฉ (24/3 = 8)
205
- with antd.Card(hoverable=True, as_item="card") as demoCard:
206
- antd.CardMeta()
207
- demoCard.click(demo_card_click, outputs=[input])
208
-
209
  # ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
210
  settingPromptBtn.click(lambda: gr.update(
211
  open=True), inputs=[], outputs=[system_prompt_modal])
 
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):
 
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
  })
83
 
84
  def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
85
+ [... generation_code ํ•จ์ˆ˜ ๋‚ด์šฉ ๋™์ผ ...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  with ms.Application() as app:
88
  with antd.ConfigProvider():
 
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,
104
+ placeholder=random.choice(DEMO_LIST)['description']
105
  )
106
 
107
  btn = antd.Button("send", type="primary", size="large")
 
131
  with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
132
  with antd.Tabs.Item(key="empty"):
133
  empty = antd.Empty(description="empty input", elem_classes="right_content")
134
+ with antd.Tabs.Item(key="loading"):
135
+ loading = antd.Spin(True, tip="coding...", size="large", elem_classes="right_content")
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])