sidbhasin commited on
Commit
849ae25
Β·
verified Β·
1 Parent(s): e739250

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -16
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import re
3
  from http import HTTPStatus
4
- from typing import Dict, List, Optional, Tuple, TypedDict
5
  import base64
6
  import dashscope
7
  import gradio as gr
@@ -11,17 +11,37 @@ import modelscope_studio.components.base as ms
11
  import modelscope_studio.components.legacy as legacy
12
  import modelscope_studio.components.antd as antd
13
 
14
- # Type definitions
15
- History = List[Tuple[str, str]]
16
- Messages = List[Dict[str, str]]
17
 
18
- class SystemConfig(TypedDict):
19
- system: str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Initialize DashScope
22
  YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
23
  dashscope.api_key = YOUR_API_TOKEN
24
 
 
 
 
 
25
  def history_to_messages(history: History, system: str) -> Messages:
26
  messages = [{'role': Role.SYSTEM, 'content': system}]
27
  for h in history:
@@ -36,25 +56,25 @@ def messages_to_history(messages: Messages) -> History:
36
  history.append([q['content'], r['content']])
37
  return history
38
 
39
- def remove_code_block(text: str) -> str:
40
  pattern = r'```html\n(.+?)\n```'
41
  match = re.search(pattern, text, re.DOTALL)
42
  if match:
43
  return match.group(1).strip()
44
  return text.strip()
45
 
46
- def history_render(history: History) -> tuple[gr.update, History]:
47
  return gr.update(open=True), history
48
 
49
- def clear_history() -> List:
50
  return []
51
 
52
- def send_to_sandbox(code: str) -> str:
53
  encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
54
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
55
  return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
56
 
57
- def demo_card_click(e: gr.EventData) -> str:
58
  index = e._data['component']['index']
59
  return DEMO_LIST[index]['description']
60
 
@@ -64,7 +84,13 @@ with gr.Blocks(css="app.css") as demo:
64
  setting = gr.State({"system": SystemPrompt})
65
 
66
  with ms.Application() as app:
67
- with antd.ConfigProvider():
 
 
 
 
 
 
68
  with antd.Row(gutter=[32, 12]) as layout:
69
  # Left Column
70
  with antd.Col(span=24, md=8):
@@ -94,8 +120,10 @@ with gr.Blocks(css="app.css") as demo:
94
  antd.CardMeta()
95
  demoCard.click(demo_card_click, outputs=[input])
96
 
 
 
97
  with antd.Flex(gap="small", wrap=True):
98
- settingPromptBtn = antd.Button("βš™οΈ Settings", type="default")
99
  codeBtn = antd.Button("πŸ§‘β€πŸ’» View Code", type="default")
100
  historyBtn = antd.Button("πŸ“œ History", type="default")
101
 
@@ -194,8 +222,6 @@ with gr.Blocks(css="app.css") as demo:
194
  outputs=[code_output, history, sandbox, state_tab, code_drawer]
195
  )
196
 
197
- clear_btn.click(clear_history, outputs=[history])
198
-
199
  settingPromptBtn.click(
200
  lambda: gr.update(open=True),
201
  outputs=[system_prompt_modal]
@@ -206,11 +232,14 @@ with gr.Blocks(css="app.css") as demo:
206
  inputs=[systemPromptInput],
207
  outputs=[setting, system_prompt_modal]
208
  )
 
 
209
 
210
  # Launch the app
211
  if __name__ == "__main__":
212
  demo.queue(concurrency_count=20).launch(
213
  server_name="0.0.0.0",
214
  server_port=7860,
215
- share=True
 
216
  )
 
1
  import os
2
  import re
3
  from http import HTTPStatus
4
+ from typing import Dict, List, Optional, Tuple
5
  import base64
6
  import dashscope
7
  import gradio as gr
 
11
  import modelscope_studio.components.legacy as legacy
12
  import modelscope_studio.components.antd as antd
13
 
14
+ # Define system prompt
15
+ SystemPrompt = """You are an AI assistant specialized in creating web applications and tools.
16
+ Generate complete, runnable code based on user requirements with modern best practices."""
17
 
18
+ # Define demo list
19
+ DEMO_LIST = [
20
+ {
21
+ "title": "Web Application",
22
+ "description": "Create a responsive web application with modern UI",
23
+ "icon": "🌐"
24
+ },
25
+ {
26
+ "title": "Data Visualization",
27
+ "description": "Build an interactive data visualization dashboard",
28
+ "icon": "πŸ“Š"
29
+ },
30
+ {
31
+ "title": "Automation Tool",
32
+ "description": "Generate a Python automation tool",
33
+ "icon": "πŸ› οΈ"
34
+ }
35
+ ]
36
 
37
  # Initialize DashScope
38
  YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
39
  dashscope.api_key = YOUR_API_TOKEN
40
 
41
+ # Type definitions
42
+ History = List[Tuple[str, str]]
43
+ Messages = List[Dict[str, str]]
44
+
45
  def history_to_messages(history: History, system: str) -> Messages:
46
  messages = [{'role': Role.SYSTEM, 'content': system}]
47
  for h in history:
 
56
  history.append([q['content'], r['content']])
57
  return history
58
 
59
+ def remove_code_block(text):
60
  pattern = r'```html\n(.+?)\n```'
61
  match = re.search(pattern, text, re.DOTALL)
62
  if match:
63
  return match.group(1).strip()
64
  return text.strip()
65
 
66
+ def history_render(history: History):
67
  return gr.update(open=True), history
68
 
69
+ def clear_history():
70
  return []
71
 
72
+ def send_to_sandbox(code):
73
  encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
74
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
75
  return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
76
 
77
+ def demo_card_click(e: gr.EventData):
78
  index = e._data['component']['index']
79
  return DEMO_LIST[index]['description']
80
 
 
84
  setting = gr.State({"system": SystemPrompt})
85
 
86
  with ms.Application() as app:
87
+ with antd.ConfigProvider(theme={
88
+ "token": {
89
+ "colorPrimary": "#6366f1",
90
+ "borderRadius": 8,
91
+ "fontSize": 16
92
+ }
93
+ }):
94
  with antd.Row(gutter=[32, 12]) as layout:
95
  # Left Column
96
  with antd.Col(span=24, md=8):
 
120
  antd.CardMeta()
121
  demoCard.click(demo_card_click, outputs=[input])
122
 
123
+ antd.Divider("Settings")
124
+
125
  with antd.Flex(gap="small", wrap=True):
126
+ settingPromptBtn = antd.Button("βš™οΈ System Prompt", type="default")
127
  codeBtn = antd.Button("πŸ§‘β€πŸ’» View Code", type="default")
128
  historyBtn = antd.Button("πŸ“œ History", type="default")
129
 
 
222
  outputs=[code_output, history, sandbox, state_tab, code_drawer]
223
  )
224
 
 
 
225
  settingPromptBtn.click(
226
  lambda: gr.update(open=True),
227
  outputs=[system_prompt_modal]
 
232
  inputs=[systemPromptInput],
233
  outputs=[setting, system_prompt_modal]
234
  )
235
+
236
+ clear_btn.click(clear_history, outputs=[history])
237
 
238
  # Launch the app
239
  if __name__ == "__main__":
240
  demo.queue(concurrency_count=20).launch(
241
  server_name="0.0.0.0",
242
  server_port=7860,
243
+ share=True,
244
+ ssr_mode=False
245
  )