gradio

A set of coding rules for using Gradio, a Python tool for building interfaces around machine-learning applications. The rules cover its client library, API calls, streaming results, errors, jobs, deployment, and security considerations.

In plain words
What is it for?
Use it when writing Python code that connects to Gradio applications, including blocking or background requests, file inputs, streamed output, job status checks, cancellation, retries, and timeouts.
Why use it?
It helps an agent follow the project's chosen Gradio usage patterns instead of mixing incompatible calling styles or handling long-running jobs incorrectly.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/wangtingyeye/llm-agent-study/gradio
Clone the repo
git clone --depth 1 https://github.com/WangTingYeYe/llm-agent-study

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 507 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00507
Opus 5 $0.00000 $0.00253
Sonnet 5 $0.00000 $0.00101
Haiku 4.5 $0.00000 $0.00051

Measured 2d ago against content hash 1d95c326b398, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

gradio scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/frameworks/gradio.mdc · 61 lines

What it actually says


description: 该规则解释了 Gradio 机器学习模型部署和客户端调用的约定和最佳实践。 globs: **/*.py alwaysApply: false

Gradio 规则

客户端使用规范

  • 使用 gradio_client 库而非完整的 gradio 包以获得更轻量级的体验
  • 安装命令:pip install gradio_client
  • 使用 Client 类连接到 Gradio 应用:client = Client("app_url")

API 调用规范

  • 使用 client.predict() 进行阻塞式 API 调用,直接获取结果
  • 使用 client.submit() 进行非阻塞式调用,返回 Job 对象用于后台处理
  • 文件参数使用 file() 函数包装:file("path/to/file")
  • API 调用时使用与原应用相同的关键字参数名称

流式处理规范

  • 对于流式输出,使用 submit() 方法并迭代结果:
for result in client.submit("prompt"):
    print(result)

错误处理规范

  • 确保上游应用设置 show_error=True 或使用 gr.Error 异常
  • 客户端会抛出与上游应用相同的异常
  • 使用适当的异常处理机制捕获和处理错误

作业管理规范

  • 使用 job.status() 检查作业状态和队列位置
  • 使用 job.done() 检查作业是否完成
  • 支持作业取消和队列状态监控
  • 在长时间运行的任务中定期检查作业状态

部署和集成规范

  • 客户端可在任何 Python 环境中运行(包括 Flask、Django 等)
  • 支持 Hugging Face Spaces 以及自定义服务器
  • 在生产环境中使用时考虑认证和安全性
  • 使用合适的超时设置避免长时间阻塞

最佳实践

  • 对于生产应用,使用非阻塞的 submit() 方法
  • 实现适当的重试机制处理网络问题
  • 在多线程环境中注意客户端的线程安全性
  • 缓存客户端实例避免重复连接开销
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 61 lines · 0 tokens per session scan A 1d95c326b398

Subscribe to this mod's changes

gradio is a cursor rule published in the GitHub repository WangTingYeYe/llm-agent-study (4 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 507 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.