Download index.html from ahepc/ai: direct link, hf CLI and curl.
- Browser
- Download file 35.3 kB
-
https://huggingface.co/spaces/ahepc/ai/resolve/main/index.html
- Command line
-
hf download hf://spaces/ahepc/ai/index.html
-
curl -L -o index.html https://huggingface.co/spaces/ahepc/ai/resolve/main/index.html
35.3 kB
| <html lang="zh-CN"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Transformer架构交互式教学</title> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <script src="https://d3js.org/d3.v7.min.js"></script> | |
| <script src="https://cdn.plot.ly/plotly-2.24.1.min.js"></script> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> | |
| <script> | |
| tailwind.config = { | |
| theme: { | |
| extend: { | |
| colors: { | |
| primary: '#4F46E5', | |
| secondary: '#818CF8', | |
| accent: '#F472B6', | |
| dark: '#1E293B', | |
| light: '#F1F5F9' | |
| } | |
| } | |
| } | |
| } | |
| </script> | |
| <style> | |
| .attention-node { | |
| transition: all 0.3s ease; | |
| cursor: pointer; | |
| } | |
| .attention-node:hover { | |
| stroke-width: 3px; | |
| stroke: #F472B6; | |
| } | |
| .attention-line { | |
| stroke-opacity: 0.3; | |
| transition: stroke-opacity 0.3s ease; | |
| } | |
| .attention-line:hover { | |
| stroke-opacity: 0.8; | |
| } | |
| .token-highlight { | |
| background-color: rgba(79, 70, 229, 0.2); | |
| transition: all 0.3s ease; | |
| } | |
| .fade-in { | |
| animation: fadeIn 0.5s ease-in; | |
| } | |
| @keyframes fadeIn { | |
| from { opacity: 0; transform: translateY(10px); } | |
| to { opacity: 1; transform: translateY(0); } | |
| } | |
| .architecture-diagram { | |
| background-image: linear-gradient(to right, #F1F5F9 1px, transparent 1px), | |
| linear-gradient(to bottom, #F1F5F9 1px, transparent 1px); | |
| background-size: 30px 30px; | |
| } | |
| </style> | |
| </head> | |
| <body class="bg-light text-dark font-sans"> | |
| <!-- 导航栏 --> | |
| <nav class="bg-primary text-white shadow-lg"> | |
| <div class="container mx-auto px-4 py-3 flex justify-between items-center"> | |
| <div class="flex items-center space-x-2"> | |
| <i class="fas fa-brain text-2xl"></i> | |
| <h1 class="text-xl font-bold">Transformer架构交互式教学</h1> | |
| </div> | |
| <div class="flex space-x-4"> | |
| <a href="#architecture" class="hover:text-secondary transition">架构</a> | |
| <a href="#attention" class="hover:text-secondary transition">注意力机制</a> | |
| <a href="#generation" class="hover:text-secondary transition">文本生成</a> | |
| <a href="#deployment" class="hover:text-secondary transition">部署指南</a> | |
| </div> | |
| </div> | |
| </nav> | |
| <!-- 介绍部分 --> | |
| <section class="py-16 bg-gradient-to-br from-primary to-secondary text-white"> | |
| <div class="container mx-auto px-4 text-center"> | |
| <h2 class="text-4xl font-bold mb-6">探索Transformer架构的奥秘</h2> | |
| <p class="text-xl max-w-3xl mx-auto mb-8"> | |
| 通过交互式可视化,直观理解大语言模型的核心原理 - Transformer架构和注意力机制 | |
| </p> | |
| <div class="flex justify-center space-x-4"> | |
| <a href="#architecture" class="bg-white text-primary px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">开始探索</a> | |
| <a href="#generation" class="bg-accent text-white px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">体验文本生成</a> | |
| </div> | |
| </div> | |
| </section> | |
| <!-- Transformer架构可视化 --> | |
| <section id="architecture" class="py-16 bg-white"> | |
| <div class="container mx-auto px-4"> | |
| <h2 class="text-3xl font-bold text-center mb-4">Transformer架构解析</h2> | |
| <p class="text-center text-gray-600 max-w-3xl mx-auto mb-12"> | |
| Transformer是当今大语言模型的核心架构,由编码器和解码器组成,通过自注意力机制处理序列数据 | |
| </p> | |
| <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 items-center"> | |
| <div> | |
| <div class="architecture-diagram bg-white rounded-xl shadow-lg p-4 h-96" id="transformer-viz"></div> | |
| </div> | |
| <div> | |
| <div class="bg-gray-50 rounded-xl p-6 shadow"> | |
| <h3 class="text-xl font-semibold mb-4">核心组件</h3> | |
| <ul class="space-y-3"> | |
| <li class="flex items-start"> | |
| <i class="fas fa-cube text-secondary mt-1 mr-3"></i> | |
| <div> | |
| <strong>输入嵌入层</strong> - 将单词转换为向量表示 | |
| </div> | |
| </li> | |
| <li class="flex items-start"> | |
| <i class="fas fa-project-diagram text-secondary mt-1 mr-3"></i> | |
| <div> | |
| <strong>位置编码</strong> - 为输入添加位置信息 | |
| </div> | |
| </li> | |
| <li class="flex items-start"> | |
| <i class="fas fa-bullseye text-accent mt-1 mr-3"></i> | |
| <div> | |
| <strong>多头注意力</strong> - 模型的核心机制,计算单词间相关性 | |
| </div> | |
| </li> | |
| <li class="flex items-start"> | |
| <i class="fas fa-layer-group text-secondary mt-1 mr-3"></i> | |
| <div> | |
| <strong>前馈神经网络</strong> - 对注意力结果进行非线性变换 | |
| </div> | |
| </li> | |
| <li class="flex items-start"> | |
| <i class="fas fa-code-branch text-secondary mt-1 mr-3"></i> | |
| <div> | |
| <strong>残差连接和层归一化</strong> - 稳定训练过程 | |
| </div> | |
| </li> | |
| </ul> | |
| <div class="mt-8"> | |
| <h4 class="font-semibold mb-2">探索架构</h4> | |
| <div class="flex flex-wrap gap-2"> | |
| <button onclick="highlightComponent('encoder')" class="bg-secondary text-white px-3 py-1 rounded hover:bg-opacity-90 transition">编码器</button> | |
| <button onclick="highlightComponent('decoder')" class="bg-accent text-white px-3 py-1 rounded hover:bg-opacity-90 transition">解码器</button> | |
| <button onclick="highlightComponent('attention')" class="bg-primary text-white px-3 py-1 rounded hover:bg-opacity-90 transition">注意力层</button> | |
| <button onclick="resetHighlight()" class="bg-gray-300 text-gray-700 px-3 py-1 rounded hover:bg-gray-400 transition">重置</button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <!-- 注意力机制可视化 --> | |
| <section id="attention" class="py-16 bg-gray-50"> | |
| <div class="container mx-auto px-4"> | |
| <h2 class="text-3xl font-bold text-center mb-4">注意力机制详解</h2> | |
| <p class="text-center text-gray-600 max-w-3xl mx-auto mb-12"> | |
| 注意力机制让模型能够"关注"输入序列中不同部分之间的关系,是Transformer的核心创新 | |
| </p> | |
| <div class="grid grid-cols-1 lg:grid-cols-2 gap-8"> | |
| <div class="bg-white rounded-xl shadow-lg p-4"> | |
| <h3 class="text-xl font-semibold mb-4">注意力权重可视化</h3> | |
| <div id="attention-heatmap" class="h-80"></div> | |
| <div class="mt-4"> | |
| <label class="block text-sm font-medium mb-2">输入文本</label> | |
| <input type="text" id="attention-text" value="人工智能正在改变世界" class="w-full p-2 border rounded"> | |
| <button onclick="updateAttentionViz()" class="mt-3 bg-primary text-white px-4 py-2 rounded hover:bg-opacity-90 transition">计算注意力</button> | |
| </div> | |
| </div> | |
| <div> | |
| <div class="bg-white rounded-xl shadow-lg p-6 mb-6"> | |
| <h3 class="text-xl font-semibold mb-4">注意力机制如何工作</h3> | |
| <p class="mb-4">注意力机制通过三个步骤计算单词之间的相关性:</p> | |
| <ol class="list-decimal pl-6 space-y-2"> | |
| <li><strong>查询(Query)</strong> - 当前处理的单词</li> | |
| <li><strong>键(Key)</strong> - 序列中的所有单词</li> | |
| <li><strong>值(Value)</strong> - 每个键对应的内容</li> | |
| </ol> | |
| <div class="mt-4 p-4 bg-blue-50 rounded-lg"> | |
| <p class="font-mono text-sm">注意力分数 = Softmax(Query • Key<sup>T</sup> / √d<sub>k</sub>) • Value</p> | |
| </div> | |
| </div> | |
| <div class="bg-white rounded-xl shadow-lg p-6"> | |
| <h3 class="text-xl font-semibold mb-4">多头注意力</h3> | |
| <p>Transformer使用多个"注意力头",每个头学习不同的表示方式,使模型能够捕捉不同方面的关系。</p> | |
| <div class="mt-4 flex justify-center"> | |
| <div class="relative"> | |
| <div class="w-12 h-12 bg-secondary rounded-full flex items-center justify-center text-white">头1</div> | |
| <div class="w-12 h-12 bg-primary rounded-full flex items-center justify-center text-white absolute top-0 left-14">头2</div> | |
| <div class="w-12 h-12 bg-accent rounded-full flex items-center justify-center text-white absolute top-0 left-28">头3</div> | |
| <div class="w-12 h-12 bg-purple-500 rounded-full flex items-center justify-center text-white absolute top-0 left-42">头4</div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <!-- 文本生成体验 --> | |
| <section id="generation" class="py-16 bg-white"> | |
| <div class="container mx-auto px-4"> | |
| <h2 class="text-3xl font-bold text-center mb-4">文本生成体验</h2> | |
| <p class="text-center text-gray-600 max-w-3xl mx-auto mb-12"> | |
| 体验Transformer模型如何生成文本,并可视化生成过程中的注意力机制 | |
| </p> | |
| <div class="max-w-4xl mx-auto bg-gray-50 rounded-xl shadow-lg p-6"> | |
| <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6"> | |
| <div> | |
| <label class="block text-sm font-medium mb-2">选择模型</label> | |
| <select id="model-select" class="w-full p-2 border rounded"> | |
| <option value="gpt-mini">硅基流动-GPT-Mini</option> | |
| <option value="gpt-medium">硅基流动-GPT-Medium</option> | |
| </select> | |
| </div> | |
| <div> | |
| <label class="block text-sm font-medium mb-2">生成长度</label> | |
| <input type="range" id="length-slider" min="20" max="100" value="50" class="w-full"> | |
| <div class="text-center text-sm"><span id="length-value">50</span> tokens</div> | |
| </div> | |
| <div> | |
| <label class="block text-sm font-medium mb-2">温度</label> | |
| <input type="range" id="temp-slider" min="0.1" max="1.0" step="0.1" value="0.7" class="w-full"> | |
| <div class="text-center text-sm"><span id="temp-value">0.7</span></div> | |
| </div> | |
| </div> | |
| <div class="mb-6"> | |
| <label class="block text-sm font-medium mb-2">输入提示</label> | |
| <textarea id="prompt-input" class="w-full p-3 border rounded h-32">人工智能是</textarea> | |
| </div> | |
| <div class="flex justify-center mb-6"> | |
| <button id="generate-btn" class="bg-primary text-white px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition flex items-center"> | |
| <i class="fas fa-bolt mr-2"></i>生成文本 | |
| </button> | |
| </div> | |
| <div class="bg-white rounded-lg shadow p-4 mb-6"> | |
| <h3 class="font-semibold mb-2">生成结果</h3> | |
| <div id="generated-text" class="min-h-32 p-3 bg-gray-100 rounded"> | |
| <p class="text-gray-500">点击"生成文本"按钮查看结果...</p> | |
| </div> | |
| </div> | |
| <div class="bg-white rounded-lg shadow p-4"> | |
| <h3 class="font-semibold mb-2">生成过程可视化</h3> | |
| <div id="generation-viz" class="h-64"></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <!-- 部署指南 --> | |
| <section id="deployment" class="py-16 bg-gray-50"> | |
| <div class="container mx-auto px-4"> | |
| <h2 class="text-3xl font-bold text-center mb-4">Windows 11部署指南</h2> | |
| <p class="text-center text-gray-600 max-w-3xl mx-auto mb-12"> | |
| 面向小白的详细部署教程,轻松在Windows 11上运行此交互式教学网站 | |
| </p> | |
| <div class="max-w-4xl mx-auto bg-white rounded-xl shadow-lg overflow-hidden"> | |
| <div class="grid grid-cols-1 md:grid-cols-2"> | |
| <div class="p-6"> | |
| <h3 class="text-xl font-semibold mb-4">部署步骤</h3> | |
| <ol class="list-decimal pl-6 space-y-4"> | |
| <li> | |
| <strong>下载网站文件</strong> | |
| <p class="text-sm text-gray-600 mt-1">将本网页保存为HTML文件(右键点击 → 另存为)</p> | |
| </li> | |
| <li> | |
| <strong>安装本地服务器(可选)</strong> | |
| <p class="text-sm text-gray-600 mt-1">安装Node.js和http-server模块:<code class="bg-gray-100 p-1 rounded">npm install -g http-server</code></p> | |
| </li> | |
| <li> | |
| <strong>运行网站</strong> | |
| <p class="text-sm text-gray-600 mt-1">双击HTML文件直接打开,或在命令行中运行:<code class="bg-gray-100 p-1 rounded">http-server</code></p> | |
| </li> | |
| <li> | |
| <strong>访问网站</strong> | |
| <p class="text-sm text-gray-600 mt-1">在浏览器中访问 <code class="bg-gray-100 p-1 rounded">http://localhost:8080</code></p> | |
| </li> | |
| </ol> | |
| </div> | |
| <div class="bg-gray-800 text-white p-6"> | |
| <h3 class="text-xl font-semibold mb-4">常见问题</h3> | |
| <div class="space-y-4"> | |
| <div> | |
| <h4 class="font-medium">Q: 为什么需要本地服务器?</h4> | |
| <p class="text-gray-300 text-sm mt-1">A: 直接打开HTML文件时,某些API调用可能被浏览器安全策略阻止。使用本地服务器可以避免此问题。</p> | |
| </div> | |
| <div> | |
| <h4 class="font-medium">Q: 如何安装Node.js?</h4> | |
| <p class="text-gray-300 text-sm mt-1">A: 访问 <a href="https://nodejs.org" class="text-secondary underline">nodejs.org</a> 下载安装包,按提示安装即可。</p> | |
| </div> | |
| <div> | |
| <h4 class="font-medium">Q: 网站无法加载可视化?</h4> | |
| <p class="text-gray-300 text-sm mt-1">A: 确保浏览器支持JavaScript,并检查网络连接是否正常。</p> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="mt-12 max-w-4xl mx-auto"> | |
| <h3 class="text-xl font-semibold mb-4">视频教程</h3> | |
| <div class="bg-gray-200 border-2 border-dashed rounded-xl w-full h-64 flex items-center justify-center"> | |
| <div class="text-center"> | |
| <i class="fas fa-video text-4xl text-gray-400 mb-3"></i> | |
| <p class="text-gray-500">Windows 11部署视频教程</p> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <!-- 页脚 --> | |
| <footer class="bg-dark text-white py-8"> | |
| <div class="container mx-auto px-4 text-center"> | |
| <p class="mb-4">Transformer架构交互式教学网站 | 帮助初学者理解大语言模型原理</p> | |
| <div class="flex justify-center space-x-4"> | |
| <a href="#" class="hover:text-secondary transition"><i class="fab fa-github"></i></a> | |
| <a href="#" class="hover:text-secondary transition"><i class="fab fa-twitter"></i></a> | |
| <a href="#" class="hover:text-secondary transition"><i class="fab fa-linkedin"></i></a> | |
| </div> | |
| <p class="mt-4 text-gray-400 text-sm">© 2023 AI Education Project. 本网站仅用于教学目的。</p> | |
| </div> | |
| </footer> | |
| <script> | |
| // 初始化Transformer架构可视化 | |
| function initTransformerViz() { | |
| const svg = d3.select("#transformer-viz") | |
| .append("svg") | |
| .attr("width", "100%") | |
| .attr("height", "100%") | |
| .attr("viewBox", "0 0 800 400"); | |
| // 绘制编码器 | |
| const encoderGroup = svg.append("g").attr("transform", "translate(100, 50)"); | |
| // 编码器标签 | |
| encoderGroup.append("text") | |
| .text("编码器") | |
| .attr("x", 0) | |
| .attr("y", -10) | |
| .attr("font-weight", "bold") | |
| .attr("fill", "#4F46E5"); | |
| // 绘制编码器层 | |
| for (let i = 0; i < 3; i++) { | |
| const layerGroup = encoderGroup.append("g").attr("transform", `translate(0, ${i * 100})`); | |
| // 层矩形 | |
| layerGroup.append("rect") | |
| .attr("id", `encoder-layer-${i}`) | |
| .attr("x", 0) | |
| .attr("y", 0) | |
| .attr("width", 200) | |
| .attr("height", 80) | |
| .attr("rx", 10) | |
| .attr("fill", i === 1 ? "#E0E7FF" : "#F1F5F9") | |
| .attr("stroke", "#4F46E5") | |
| .attr("stroke-width", 2); | |
| // 层标签 | |
| layerGroup.append("text") | |
| .text(`编码器层 ${i+1}`) | |
| .attr("x", 100) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#4F46E5"); | |
| // 内部组件 | |
| layerGroup.append("rect") | |
| .attr("x", 20) | |
| .attr("y", 20) | |
| .attr("width", 60) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", i === 1 ? "#C7D2FE" : "#DBEAFE") | |
| .attr("stroke", "#818CF8") | |
| .attr("id", `attention-${i}`); | |
| layerGroup.append("text") | |
| .text("注意力") | |
| .attr("x", 50) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#4F46E5") | |
| .attr("font-size", "12px"); | |
| layerGroup.append("rect") | |
| .attr("x", 120) | |
| .attr("y", 20) | |
| .attr("width", 60) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", i === 1 ? "#C7D2FE" : "#DBEAFE") | |
| .attr("stroke", "#818CF8"); | |
| layerGroup.append("text") | |
| .text("前馈") | |
| .attr("x", 150) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#4F46E5") | |
| .attr("font-size", "12px"); | |
| } | |
| // 绘制解码器 | |
| const decoderGroup = svg.append("g").attr("transform", "translate(500, 50)"); | |
| // 解码器标签 | |
| decoderGroup.append("text") | |
| .text("解码器") | |
| .attr("x", 0) | |
| .attr("y", -10) | |
| .attr("font-weight", "bold") | |
| .attr("fill", "#F472B6"); | |
| // 绘制解码器层 | |
| for (let i = 0; i < 3; i++) { | |
| const layerGroup = decoderGroup.append("g").attr("transform", `translate(0, ${i * 100})`); | |
| // 层矩形 | |
| layerGroup.append("rect") | |
| .attr("id", `decoder-layer-${i}`) | |
| .attr("x", 0) | |
| .attr("y", 0) | |
| .attr("width", 200) | |
| .attr("height", 80) | |
| .attr("rx", 10) | |
| .attr("fill", i === 1 ? "#FCE7F3" : "#FDF2F8") | |
| .attr("stroke", "#F472B6") | |
| .attr("stroke-width", 2); | |
| // 层标签 | |
| layerGroup.append("text") | |
| .text(`解码器层 ${i+1}`) | |
| .attr("x", 100) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#F472B6"); | |
| // 内部组件 | |
| layerGroup.append("rect") | |
| .attr("x", 20) | |
| .attr("y", 20) | |
| .attr("width", 50) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", i === 1 ? "#FBCFE8" : "#FCE7F3") | |
| .attr("stroke", "#F472B6"); | |
| layerGroup.append("text") | |
| .text("自注意") | |
| .attr("x", 45) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#F472B6") | |
| .attr("font-size", "12px"); | |
| layerGroup.append("rect") | |
| .attr("x", 85) | |
| .attr("y", 20) | |
| .attr("width", 50) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", i === 1 ? "#FBCFE8" : "#FCE7F3") | |
| .attr("stroke", "#F472B6"); | |
| layerGroup.append("text") | |
| .text("编码注意") | |
| .attr("x", 110) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#F472B6") | |
| .attr("font-size", "10px"); | |
| layerGroup.append("rect") | |
| .attr("x", 150) | |
| .attr("y", 20) | |
| .attr("width", 40) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", i === 1 ? "#FBCFE8" : "#FCE7F3") | |
| .attr("stroke", "#F472B6"); | |
| layerGroup.append("text") | |
| .text("前馈") | |
| .attr("x", 170) | |
| .attr("y", 40) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#F472B6") | |
| .attr("font-size", "12px"); | |
| } | |
| // 绘制连接线 | |
| svg.append("line") | |
| .attr("x1", 300) | |
| .attr("y1", 200) | |
| .attr("x2", 500) | |
| .attr("y2", 200) | |
| .attr("stroke", "#94A3B8") | |
| .attr("stroke-width", 2) | |
| .attr("marker-end", "url(#arrow)"); | |
| // 绘制输入输出 | |
| svg.append("rect") | |
| .attr("x", 20) | |
| .attr("y", 180) | |
| .attr("width", 60) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", "#D1FAE5") | |
| .attr("stroke", "#10B981"); | |
| svg.append("text") | |
| .text("输入") | |
| .attr("x", 50) | |
| .attr("y", 200) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#10B981"); | |
| svg.append("rect") | |
| .attr("x", 720) | |
| .attr("y", 180) | |
| .attr("width", 60) | |
| .attr("height", 40) | |
| .attr("rx", 5) | |
| .attr("fill", "#D1FAE5") | |
| .attr("stroke", "#10B981"); | |
| svg.append("text") | |
| .text("输出") | |
| .attr("x", 750) | |
| .attr("y", 200) | |
| .attr("text-anchor", "middle") | |
| .attr("dominant-baseline", "middle") | |
| .attr("fill", "#10B981"); | |
| // 添加箭头标记 | |
| svg.append("defs").append("marker") | |
| .attr("id", "arrow") | |
| .attr("viewBox", "0 -5 10 10") | |
| .attr("refX", 5) | |
| .attr("refY", 0) | |
| .attr("markerWidth", 6) | |
| .attr("markerHeight", 6) | |
| .attr("orient", "auto") | |
| .append("path") | |
| .attr("d", "M0,-5L10,0L0,5") | |
| .attr("fill", "#94A3B8"); | |
| } | |
| // 初始化注意力热力图 | |
| function initAttentionViz() { | |
| const text = "人工智能正在改变世界"; | |
| const tokens = text.split(''); | |
| const data = [{ | |
| z: generateAttentionMatrix(tokens.length), | |
| x: tokens, | |
| y: tokens, | |
| type: 'heatmap', | |
| colorscale: 'Blues', | |
| hoverongaps: false | |
| }]; | |
| const layout = { | |
| title: '注意力权重分布', | |
| xaxis: { title: '键 (Key)' }, | |
| yaxis: { title: '查询 (Query)', autorange: 'reversed' }, | |
| height: 300, | |
| margin: { l: 60, r: 20, t: 40, b: 60 } | |
| }; | |
| Plotly.newPlot('attention-heatmap', data, layout); | |
| } | |
| // 生成模拟注意力矩阵 | |
| function generateAttentionMatrix(size) { | |
| const matrix = []; | |
| for (let i = 0; i < size; i++) { | |
| const row = []; | |
| for (let j = 0; j < size; j++) { | |
| // 模拟对角线附近有较高权重 | |
| const dist = Math.abs(i - j); | |
| let value = Math.exp(-dist * 0.5); | |
| // 添加一些随机性 | |
| value += (Math.random() - 0.5) * 0.2; | |
| value = Math.max(0, Math.min(1, value)); | |
| row.push(value); | |
| } | |
| matrix.push(row); | |
| } | |
| return matrix; | |
| } | |
| // 更新注意力可视化 | |
| function updateAttentionViz() { | |
| const text = document.getElementById('attention-text').value || "人工智能正在改变世界"; | |
| const tokens = text.split(''); | |
| Plotly.update('attention-heatmap', { | |
| z: [generateAttentionMatrix(tokens.length)], | |
| x: [tokens], | |
| y: [tokens] | |
| }); | |
| } | |
| // 高亮架构组件 | |
| function highlightComponent(component) { | |
| d3.selectAll("rect").attr("stroke-width", 1); | |
| if (component === 'encoder') { | |
| d3.selectAll("#encoder-layer-1") | |
| .attr("stroke-width", 3) | |
| .attr("stroke", "#4F46E5"); | |
| } else if (component === 'decoder') { | |
| d3.selectAll("#decoder-layer-1") | |
| .attr("stroke-width", 3) | |
| .attr("stroke", "#F472B6"); | |
| } else if (component === 'attention') { | |
| d3.selectAll("[id^='attention-']") | |
| .attr("stroke-width", 3) | |
| .attr("stroke", "#818CF8"); | |
| } | |
| } | |
| // 重置高亮 | |
| function resetHighlight() { | |
| d3.selectAll("rect").attr("stroke-width", 1); | |
| } | |
| // 文本生成函数 | |
| async function generateText() { | |
| const prompt = document.getElementById('prompt-input').value; | |
| const model = document.getElementById('model-select').value; | |
| const length = document.getElementById('length-slider').value; | |
| const temperature = document.getElementById('temp-slider').value; | |
| if (!prompt.trim()) { | |
| alert("请输入提示文本!"); | |
| return; | |
| } | |
| document.getElementById('generate-btn').innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>生成中...'; | |
| try { | |
| // 模拟API调用延迟 | |
| await new Promise(resolve => setTimeout(resolve, 1500)); | |
| // 模拟生成结果 | |
| const generatedText = prompt + "一项革命性技术,它通过模拟人类智能过程,使计算机能够执行复杂的任务。从语音助手到自动驾驶,AI正在渗透到我们生活的方方面面。"; | |
| // 显示生成结果 | |
| document.getElementById('generated-text').innerHTML = | |
| `<p class="fade-in">${generatedText}</p>`; | |
| // 更新生成过程可视化 | |
| updateGenerationViz(prompt, generatedText); | |
| } catch (error) { | |
| console.error("生成错误:", error); | |
| document.getElementById('generated-text').innerHTML = | |
| `<p class="text-red-500">生成失败: ${error.message}</p>`; | |
| } finally { | |
| document.getElementById('generate-btn').innerHTML = '<i class="fas fa-bolt mr-2"></i>生成文本'; | |
| } | |
| } | |
| // 更新生成过程可视化 | |
| function updateGenerationViz(prompt, fullText) { | |
| const promptTokens = prompt.split(''); | |
| const allTokens = fullText.split(''); | |
| const data = [{ | |
| z: generateAttentionMatrix(allTokens.length), | |
| x: allTokens, | |
| y: allTokens, | |
| type: 'heatmap', | |
| colorscale: 'Blues', | |
| hoverongaps: false | |
| }]; | |
| const layout = { | |
| title: '生成过程注意力权重', | |
| xaxis: { title: '位置' }, | |
| yaxis: { title: '生成步骤', autorange: 'reversed' }, | |
| height: 250, | |
| margin: { l: 50, r: 20, t: 40, b: 50 } | |
| }; | |
| Plotly.newPlot('generation-viz', data, layout); | |
| } | |
| // 初始化页面 | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // 初始化可视化 | |
| initTransformerViz(); | |
| initAttentionViz(); | |
| // 设置滑块事件 | |
| document.getElementById('length-slider').addEventListener('input', function() { | |
| document.getElementById('length-value').textContent = this.value; | |
| }); | |
| document.getElementById('temp-slider').addEventListener('input', function() { | |
| document.getElementById('temp-value').textContent = this.value; | |
| }); | |
| // 设置生成按钮事件 | |
| document.getElementById('generate-btn').addEventListener('click', generateText); | |
| // 平滑滚动 | |
| document.querySelectorAll('a[href^="#"]').forEach(anchor => { | |
| anchor.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| document.querySelector(this.getAttribute('href')).scrollIntoView({ | |
| behavior: 'smooth' | |
| }); | |
| }); | |
| }); | |
| }); | |
| </script> | |
| <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=ahepc/ai" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body> | |
| </html> |