教程|Hugging Face Spaces 免费部署 AI Demo:3 分钟上线

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)

本教程演示如何在 Hugging Face Spaces 上免费部署 AI Demo。

Spaces 是什么?

Hugging Face 提供的免费 AI 应用托管

  • CPU 基础:免费
  • GPU T4:免费 2 小时/天
  • GPU A10G:$0.6/小时
  • 支持 Gradio / Streamlit / Docker

第 1 步:写 app.py

import gradio as gr
from transformers import pipeline

# 加载模型(首次慢)
generator = pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")

def respond(message, history):
    messages = []
    for h in history[-5:]:  # 只保留最近 5 轮
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    output = generator(messages, max_new_tokens=512, do_sample=True, temperature=0.7)
    return output[0]["generated_text"][-1]["content"]

demo = gr.ChatInterface(respond, title="Qwen 2.5 聊天 Demo")
demo.launch()

第 2 步:写 README.md

---
title: Qwen 2.5 Chat
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---

第 3 步:推到 HF Space

# 1. 在 huggingface.co/new-space 创建 Space
# 2. 克隆到本地
git clone https://huggingface.co/spaces/yourname/qwen-chat
cd qwen-chat

# 3. 复制文件
cp ../app.py .
cp ../README.md .

# 4. 推送
git add . && git commit -m "init"
git push
# 5-10 分钟后 Space 自动 build 完成

第 4 步:升级到 GPU

在 README.md 的 frontmatter 加:

---
sdk: gradio
app_file: app.py
# 关键
hardware: t4-small  # 免费 T4
# 或
hardware: a10g-small  # 付费
---

第 5 步:用 secrets

Settings → Secrets 加环境变量:

import os
api_key = os.environ["OPENAI_API_KEY"]  # 自动注入

适合谁

  • 产品快速验证(Demo 给客户看)
  • 学术项目展示
  • 开源项目主页
  • 个人作品集

限制

  • 免费 GPU 时间有限(2h/天)
  • 模型启动慢(每次重启要 1-3 分钟)
  • 不能用于生产(无 SLA)
Leave a Comment