Commit
·
8c56dcc
1
Parent(s):
0bec3bf
update
Browse files
app.py
CHANGED
|
@@ -10,6 +10,99 @@ from openai import OpenAI
|
|
| 10 |
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "ERNIE-4.5-VL-28B-A3B-Thinking")
|
| 11 |
BASE_URL = os.getenv("BASE_URL","")
|
| 12 |
api_key = os.getenv("ERNIE_API_KEY","")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
_client = OpenAI(
|
| 14 |
base_url=BASE_URL,
|
| 15 |
api_key=api_key,
|
|
@@ -117,8 +210,29 @@ def stream_response(message: Dict[str, Any], history: List[Dict[str, Any]], mode
|
|
| 117 |
|
| 118 |
|
| 119 |
def build_demo() -> gr.Blocks:
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
textbox = gr.MultimodalTextbox(
|
| 123 |
show_label=False,
|
| 124 |
placeholder="Enter text, or upload one or more images...",
|
|
@@ -137,20 +251,31 @@ def build_demo() -> gr.Blocks:
|
|
| 137 |
},
|
| 138 |
]
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
with gr.
|
| 149 |
-
gr.
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
|
|
|
|
| 10 |
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "ERNIE-4.5-VL-28B-A3B-Thinking")
|
| 11 |
BASE_URL = os.getenv("BASE_URL","")
|
| 12 |
api_key = os.getenv("ERNIE_API_KEY","")
|
| 13 |
+
|
| 14 |
+
CUSTOM_CSS = """
|
| 15 |
+
body {
|
| 16 |
+
background: radial-gradient(circle at top, #fdfbff 0%, #e7ecf7 45%, #dfe6f5 100%);
|
| 17 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
|
| 18 |
+
color: #0f172a;
|
| 19 |
+
}
|
| 20 |
+
.gradio-container {
|
| 21 |
+
max-width: 1200px !important;
|
| 22 |
+
margin: 0 auto;
|
| 23 |
+
}
|
| 24 |
+
#ernie-hero {
|
| 25 |
+
padding: 12px 0 4px;
|
| 26 |
+
}
|
| 27 |
+
#ernie-hero h1 {
|
| 28 |
+
font-size: 1.85rem;
|
| 29 |
+
margin-bottom: 0;
|
| 30 |
+
font-weight: 500;
|
| 31 |
+
}
|
| 32 |
+
#examples-panel {
|
| 33 |
+
margin-top: 20px;
|
| 34 |
+
padding: 18px 22px;
|
| 35 |
+
border-radius: 18px;
|
| 36 |
+
border: 1px solid rgba(15, 23, 42, 0.12);
|
| 37 |
+
background: rgba(255, 255, 255, 0.92);
|
| 38 |
+
box-shadow: 0 15px 35px rgba(15, 23, 42, 0.08);
|
| 39 |
+
gap: 18px;
|
| 40 |
+
}
|
| 41 |
+
#examples-panel h4 {
|
| 42 |
+
margin: 0 0 8px;
|
| 43 |
+
font-size: 1.1rem;
|
| 44 |
+
font-weight: 500;
|
| 45 |
+
}
|
| 46 |
+
#examples-panel p {
|
| 47 |
+
margin: 0;
|
| 48 |
+
color: rgba(15, 23, 42, 0.7);
|
| 49 |
+
font-size: 0.95rem;
|
| 50 |
+
}
|
| 51 |
+
#examples-grid table {
|
| 52 |
+
width: 100%;
|
| 53 |
+
}
|
| 54 |
+
#examples-grid table tbody {
|
| 55 |
+
display: grid;
|
| 56 |
+
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
| 57 |
+
gap: 12px;
|
| 58 |
+
}
|
| 59 |
+
#examples-grid table tr {
|
| 60 |
+
display: block;
|
| 61 |
+
background: #f7f9ff;
|
| 62 |
+
border-radius: 14px;
|
| 63 |
+
border: 1px solid rgba(15, 23, 42, 0.08);
|
| 64 |
+
padding: 14px;
|
| 65 |
+
box-shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
|
| 66 |
+
}
|
| 67 |
+
#examples-grid table td {
|
| 68 |
+
display: block;
|
| 69 |
+
padding: 0;
|
| 70 |
+
}
|
| 71 |
+
#chat-wrapper {
|
| 72 |
+
margin-top: 32px;
|
| 73 |
+
border-radius: 24px;
|
| 74 |
+
padding: 18px;
|
| 75 |
+
background: rgba(255, 255, 255, 0.95);
|
| 76 |
+
border: 1px solid rgba(15, 23, 42, 0.1);
|
| 77 |
+
box-shadow: 0 25px 60px rgba(15, 23, 42, 0.12);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
@media (prefers-color-scheme: dark) {
|
| 81 |
+
body {
|
| 82 |
+
background: radial-gradient(circle at top, #1f264b 0%, #0f172a 45%, #040713 100%);
|
| 83 |
+
color: #ecf2ff;
|
| 84 |
+
}
|
| 85 |
+
#examples-panel {
|
| 86 |
+
border: 1px solid rgba(255, 255, 255, 0.05);
|
| 87 |
+
background: rgba(8, 13, 30, 0.85);
|
| 88 |
+
box-shadow: 0 15px 45px rgba(3, 7, 18, 0.55);
|
| 89 |
+
}
|
| 90 |
+
#examples-panel p {
|
| 91 |
+
color: rgba(236, 242, 255, 0.75);
|
| 92 |
+
}
|
| 93 |
+
#examples-grid table tr {
|
| 94 |
+
background: rgba(15, 23, 42, 0.7);
|
| 95 |
+
border: 1px solid rgba(255, 255, 255, 0.04);
|
| 96 |
+
box-shadow: 0 10px 30px rgba(4, 6, 15, 0.45);
|
| 97 |
+
}
|
| 98 |
+
#chat-wrapper {
|
| 99 |
+
background: rgba(2, 6, 23, 0.78);
|
| 100 |
+
border: 1px solid rgba(99, 102, 241, 0.25);
|
| 101 |
+
box-shadow: 0 25px 70px rgba(2, 6, 23, 0.7);
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
_client = OpenAI(
|
| 107 |
base_url=BASE_URL,
|
| 108 |
api_key=api_key,
|
|
|
|
| 210 |
|
| 211 |
|
| 212 |
def build_demo() -> gr.Blocks:
|
| 213 |
+
theme = gr.themes.Soft(primary_hue="violet", secondary_hue="cyan", neutral_hue="slate")
|
| 214 |
+
|
| 215 |
+
with gr.Blocks(
|
| 216 |
+
title="ERNIE-4.5-VL-28B-A3B-Thinking",
|
| 217 |
+
theme=theme,
|
| 218 |
+
css=CUSTOM_CSS,
|
| 219 |
+
) as demo:
|
| 220 |
+
with gr.Column(elem_id="ernie-hero"):
|
| 221 |
+
gr.Markdown(
|
| 222 |
+
"""
|
| 223 |
+
<h1>Chat with ERNIE-4.5-VL-28B-A3B-Thinking</h1>
|
| 224 |
+
""",
|
| 225 |
+
elem_id="hero-text",
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
chatbot = gr.Chatbot(
|
| 229 |
+
type="messages",
|
| 230 |
+
allow_tags=["think"],
|
| 231 |
+
height=560,
|
| 232 |
+
render_markdown=True,
|
| 233 |
+
bubble_full_width=False,
|
| 234 |
+
show_copy_button=True,
|
| 235 |
+
)
|
| 236 |
textbox = gr.MultimodalTextbox(
|
| 237 |
show_label=False,
|
| 238 |
placeholder="Enter text, or upload one or more images...",
|
|
|
|
| 251 |
},
|
| 252 |
]
|
| 253 |
|
| 254 |
+
with gr.Row(elem_id="examples-panel"):
|
| 255 |
+
with gr.Column(scale=1):
|
| 256 |
+
gr.Markdown(
|
| 257 |
+
"""
|
| 258 |
+
<h4>Quick Examples</h4>
|
| 259 |
+
<p>选择一个示例,即可快速体验 ERNIE 的视觉 + 推理能力。</p>
|
| 260 |
+
""",
|
| 261 |
+
elem_id="examples-copy",
|
| 262 |
+
)
|
| 263 |
+
with gr.Column(scale=2):
|
| 264 |
+
gr.Examples(
|
| 265 |
+
examples=examples,
|
| 266 |
+
inputs=textbox,
|
| 267 |
+
label=None,
|
| 268 |
+
examples_per_page=4,
|
| 269 |
+
elem_id="examples-grid",
|
| 270 |
+
)
|
| 271 |
|
| 272 |
+
with gr.Column(elem_id="chat-wrapper"):
|
| 273 |
+
chat_interface = gr.ChatInterface(
|
| 274 |
+
fn=stream_response,
|
| 275 |
+
type="messages",
|
| 276 |
+
multimodal=True,
|
| 277 |
+
chatbot=chatbot,
|
| 278 |
+
textbox=textbox,
|
| 279 |
)
|
| 280 |
|
| 281 |
|