Update app.py
Browse files
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
|
5 |
import base64
|
6 |
import dashscope
|
7 |
import gradio as gr
|
@@ -10,100 +10,18 @@ from dashscope.api_entities.dashscope_response import Role
|
|
10 |
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 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
text-align: center;
|
19 |
-
padding: 2rem;
|
20 |
-
background: linear-gradient(135deg, #6366f1, #8b5cf6);
|
21 |
-
border-radius: 15px;
|
22 |
-
margin-bottom: 1.5rem;
|
23 |
-
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
24 |
-
}
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
margin-bottom: 1rem;
|
29 |
-
transition: transform 0.3s ease;
|
30 |
-
}
|
31 |
-
|
32 |
-
.left_header img:hover {
|
33 |
-
transform: scale(1.05);
|
34 |
-
}
|
35 |
-
|
36 |
-
.left_header h1 {
|
37 |
-
color: white;
|
38 |
-
font-size: 2rem;
|
39 |
-
font-weight: 600;
|
40 |
-
margin: 0;
|
41 |
-
}
|
42 |
-
|
43 |
-
.right_panel {
|
44 |
-
background: white;
|
45 |
-
border-radius: 15px;
|
46 |
-
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
47 |
-
height: 100%;
|
48 |
-
}
|
49 |
-
|
50 |
-
.render_header {
|
51 |
-
background: #f3f4f6;
|
52 |
-
padding: 12px;
|
53 |
-
border-radius: 15px 15px 0 0;
|
54 |
-
display: flex;
|
55 |
-
gap: 8px;
|
56 |
-
}
|
57 |
-
|
58 |
-
.header_btn {
|
59 |
-
width: 12px;
|
60 |
-
height: 12px;
|
61 |
-
border-radius: 50%;
|
62 |
-
}
|
63 |
-
|
64 |
-
.header_btn:nth-child(1) { background: #ef4444; }
|
65 |
-
.header_btn:nth-child(2) { background: #f59e0b; }
|
66 |
-
.header_btn:nth-child(3) { background: #10b981; }
|
67 |
-
|
68 |
-
.html_content {
|
69 |
-
height: calc(100vh - 180px);
|
70 |
-
overflow: auto;
|
71 |
-
padding: 20px;
|
72 |
-
}
|
73 |
-
|
74 |
-
.right_content {
|
75 |
-
padding: 2rem;
|
76 |
-
display: flex;
|
77 |
-
align-items: center;
|
78 |
-
justify-content: center;
|
79 |
-
height: calc(100vh - 180px);
|
80 |
-
}
|
81 |
-
|
82 |
-
/* Mobile Responsive Design */
|
83 |
-
@media (max-width: 768px) {
|
84 |
-
.left_header {
|
85 |
-
padding: 1.5rem;
|
86 |
-
}
|
87 |
-
|
88 |
-
.left_header img {
|
89 |
-
max-width: 150px;
|
90 |
-
}
|
91 |
-
|
92 |
-
.left_header h1 {
|
93 |
-
font-size: 1.5rem;
|
94 |
-
}
|
95 |
-
|
96 |
-
.html_content {
|
97 |
-
height: calc(100vh - 140px);
|
98 |
-
}
|
99 |
-
}
|
100 |
-
"""
|
101 |
|
102 |
# Initialize DashScope
|
103 |
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
104 |
dashscope.api_key = YOUR_API_TOKEN
|
105 |
|
106 |
-
# Helper functions
|
107 |
def history_to_messages(history: History, system: str) -> Messages:
|
108 |
messages = [{'role': Role.SYSTEM, 'content': system}]
|
109 |
for h in history:
|
@@ -118,35 +36,35 @@ def messages_to_history(messages: Messages) -> History:
|
|
118 |
history.append([q['content'], r['content']])
|
119 |
return history
|
120 |
|
121 |
-
def remove_code_block(text):
|
122 |
pattern = r'```html\n(.+?)\n```'
|
123 |
match = re.search(pattern, text, re.DOTALL)
|
124 |
if match:
|
125 |
return match.group(1).strip()
|
126 |
return text.strip()
|
127 |
|
128 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
|
130 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
131 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
132 |
|
133 |
-
def demo_card_click(e: gr.EventData):
|
134 |
index = e._data['component']['index']
|
135 |
return DEMO_LIST[index]['description']
|
136 |
|
137 |
# Create Gradio interface
|
138 |
-
with gr.Blocks(css=
|
139 |
history = gr.State([])
|
140 |
setting = gr.State({"system": SystemPrompt})
|
141 |
|
142 |
with ms.Application() as app:
|
143 |
-
with antd.ConfigProvider(
|
144 |
-
"token": {
|
145 |
-
"colorPrimary": "#6366f1",
|
146 |
-
"borderRadius": 8,
|
147 |
-
"fontSize": 16
|
148 |
-
}
|
149 |
-
}):
|
150 |
with antd.Row(gutter=[32, 12]) as layout:
|
151 |
# Left Column
|
152 |
with antd.Col(span=24, md=8):
|
@@ -161,8 +79,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
161 |
input = antd.InputTextarea(
|
162 |
size="large",
|
163 |
allow_clear=True,
|
164 |
-
placeholder="What kind of application would you like to build?"
|
165 |
-
style={"minHeight": "120px"}
|
166 |
)
|
167 |
|
168 |
with antd.Flex(gap="small", justify="space-between"):
|
@@ -177,10 +94,8 @@ with gr.Blocks(css=custom_css) as demo:
|
|
177 |
antd.CardMeta()
|
178 |
demoCard.click(demo_card_click, outputs=[input])
|
179 |
|
180 |
-
antd.Divider("Settings")
|
181 |
-
|
182 |
with antd.Flex(gap="small", wrap=True):
|
183 |
-
settingPromptBtn = antd.Button("⚙️
|
184 |
codeBtn = antd.Button("🧑💻 View Code", type="default")
|
185 |
historyBtn = antd.Button("📜 History", type="default")
|
186 |
|
@@ -227,7 +142,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
227 |
|
228 |
# Event handlers
|
229 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
230 |
-
if query
|
231 |
return
|
232 |
|
233 |
if _history is None:
|
@@ -279,7 +194,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
279 |
outputs=[code_output, history, sandbox, state_tab, code_drawer]
|
280 |
)
|
281 |
|
282 |
-
clear_btn.click(
|
283 |
|
284 |
settingPromptBtn.click(
|
285 |
lambda: gr.update(open=True),
|
|
|
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
|
|
|
10 |
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 |
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 |
|
61 |
# Create Gradio interface
|
62 |
+
with gr.Blocks(css="app.css") as demo:
|
63 |
history = gr.State([])
|
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):
|
|
|
79 |
input = antd.InputTextarea(
|
80 |
size="large",
|
81 |
allow_clear=True,
|
82 |
+
placeholder="What kind of application would you like to build?"
|
|
|
83 |
)
|
84 |
|
85 |
with antd.Flex(gap="small", justify="space-between"):
|
|
|
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 |
|
|
|
142 |
|
143 |
# Event handlers
|
144 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
145 |
+
if not query or query.strip() == '':
|
146 |
return
|
147 |
|
148 |
if _history is None:
|
|
|
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),
|