Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,7 @@ from http import HTTPStatus
|
|
4 |
from typing import Dict, List, Optional, Tuple
|
5 |
import base64
|
6 |
import anthropic
|
7 |
-
from functools import partial
|
8 |
-
import json
|
9 |
|
10 |
import gradio as gr
|
11 |
import modelscope_studio.components.base as ms
|
@@ -13,11 +12,6 @@ import modelscope_studio.components.legacy as legacy
|
|
13 |
import modelscope_studio.components.antd as antd
|
14 |
from config import DEMO_LIST, SystemPrompt
|
15 |
|
16 |
-
# μΊμ μ μ₯μ
|
17 |
-
DEMO_CACHE = {}
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
# νμΌ μλ¨μ import λ¬Έ μλμ μΆκ°
|
22 |
def get_image_base64(image_path):
|
23 |
with open(image_path, "rb") as image_file:
|
@@ -68,28 +62,9 @@ def send_to_sandbox(code):
|
|
68 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
69 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
70 |
|
71 |
-
|
72 |
-
# μΊμλ λ°λͺ¨ μ€λͺ
μ κ°μ Έμ€λ ν¨μ
|
73 |
-
def get_cached_demo_description(index: int) -> str:
|
74 |
-
if index not in DEMO_CACHE:
|
75 |
-
DEMO_CACHE[index] = DEMO_LIST[index]['description']
|
76 |
-
# μΊμ νμΌμ μ μ₯
|
77 |
-
with open('demo_cache.json', 'w', encoding='utf-8') as f:
|
78 |
-
json.dump(DEMO_CACHE, f, ensure_ascii=False, indent=2)
|
79 |
-
return DEMO_CACHE[index]
|
80 |
-
|
81 |
-
# μμν λ μΊμ λ‘λ
|
82 |
-
try:
|
83 |
-
with open('demo_cache.json', 'r', encoding='utf-8') as f:
|
84 |
-
DEMO_CACHE = json.load(f)
|
85 |
-
except FileNotFoundError:
|
86 |
-
# μ²μ μ€νμ μΊμ μ΄κΈ°ν
|
87 |
-
for i in range(len(DEMO_LIST)):
|
88 |
-
get_cached_demo_description(i)
|
89 |
-
|
90 |
def demo_card_click(e: gr.EventData):
|
91 |
index = e._data['component']['index']
|
92 |
-
return
|
93 |
|
94 |
with gr.Blocks(css_paths="app.css") as demo:
|
95 |
history = gr.State([])
|
@@ -102,12 +77,10 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
102 |
with antd.Row(gutter=[32, 12]) as layout:
|
103 |
with antd.Col(span=24, md=8):
|
104 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
105 |
-
# μμ λ header λΆλΆ
|
106 |
header = gr.HTML(f"""
|
107 |
<div class="left_header">
|
108 |
<img src="data:image/gif;base64,{get_image_base64('mouse.gif')}" width="360px" />
|
109 |
<h1 style="font-size: 20px;">AI μ½λ© μ½νμΌλΏ: MOUSE(WEB)</h2>
|
110 |
-
|
111 |
</div>
|
112 |
""")
|
113 |
input = antd.InputTextarea(
|
@@ -115,8 +88,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
115 |
allow_clear=True,
|
116 |
placeholder="Please enter what kind of application you want"
|
117 |
)
|
118 |
-
|
119 |
-
|
120 |
|
121 |
btn = antd.Button("send", type="primary", size="large")
|
122 |
clear_btn = antd.Button("clear history", type="default", size="large")
|
@@ -126,7 +97,7 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
126 |
with ms.Each(DEMO_LIST):
|
127 |
with antd.Card(hoverable=True, as_item="card") as demoCard:
|
128 |
antd.CardMeta()
|
129 |
-
demoCard.click(demo_card_click, outputs=[input])
|
130 |
|
131 |
antd.Divider("setting")
|
132 |
|
|
|
4 |
from typing import Dict, List, Optional, Tuple
|
5 |
import base64
|
6 |
import anthropic
|
7 |
+
from functools import partial
|
|
|
8 |
|
9 |
import gradio as gr
|
10 |
import modelscope_studio.components.base as ms
|
|
|
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):
|
17 |
with open(image_path, "rb") as image_file:
|
|
|
62 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
63 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
def demo_card_click(e: gr.EventData):
|
66 |
index = e._data['component']['index']
|
67 |
+
return DEMO_LIST[index]['description']
|
68 |
|
69 |
with gr.Blocks(css_paths="app.css") as demo:
|
70 |
history = gr.State([])
|
|
|
77 |
with antd.Row(gutter=[32, 12]) as layout:
|
78 |
with antd.Col(span=24, md=8):
|
79 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
|
|
80 |
header = gr.HTML(f"""
|
81 |
<div class="left_header">
|
82 |
<img src="data:image/gif;base64,{get_image_base64('mouse.gif')}" width="360px" />
|
83 |
<h1 style="font-size: 20px;">AI μ½λ© μ½νμΌλΏ: MOUSE(WEB)</h2>
|
|
|
84 |
</div>
|
85 |
""")
|
86 |
input = antd.InputTextarea(
|
|
|
88 |
allow_clear=True,
|
89 |
placeholder="Please enter what kind of application you want"
|
90 |
)
|
|
|
|
|
91 |
|
92 |
btn = antd.Button("send", type="primary", size="large")
|
93 |
clear_btn = antd.Button("clear history", type="default", size="large")
|
|
|
97 |
with ms.Each(DEMO_LIST):
|
98 |
with antd.Card(hoverable=True, as_item="card") as demoCard:
|
99 |
antd.CardMeta()
|
100 |
+
demoCard.click(demo_card_click, outputs=[input], api_name="demo_click", cache=True)
|
101 |
|
102 |
antd.Divider("setting")
|
103 |
|