mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
656 lines
29 KiB
HTML
656 lines
29 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>ml-huggingface-spaces</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="alternate" type="text/markdown" href="/ml-huggingface-spaces.md">
|
|
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/ml-huggingface-spaces">
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/ml-huggingface-spaces.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>ml-huggingface-spaces</h1><p class="lede">Deploying Gradio and Streamlit apps on HuggingFace Spaces</p><h1>HuggingFace Spaces</h1>
|
|
<p><strong>Scope</strong>: Gradio apps, Streamlit apps, Space configuration, hardware selection, secrets management <strong>Lines</strong>: ~285 <strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<hr>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Activate this skill when:</p>
|
|
<ul><li>Deploying interactive ML demos and applications</li><li>Sharing models with non-technical users</li><li>Creating public or private app interfaces</li><li>Hosting Gradio or Streamlit applications</li><li>Configuring GPU-accelerated inference</li><li>Managing API keys and secrets securely</li><li>Embedding apps in websites or documentation</li><li>Setting up custom domains for apps</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>Space Types</h3>
|
|
<p><strong>Gradio Spaces</strong>: Python library for ML interfaces</p>
|
|
<ul><li>Quick setup with minimal code</li><li>Pre-built components (sliders, text boxes, images)</li><li>Automatic input/output handling</li><li>Built-in examples and flagging</li><li>Real-time inference</li></ul>
|
|
<p><strong>Streamlit Spaces</strong>: Python framework for data apps</p>
|
|
<ul><li>More flexible layouts</li><li>Custom widgets and components</li><li>Session state management</li><li>Data visualization support</li><li>Multi-page apps</li></ul>
|
|
<p><strong>Static Spaces</strong>: HTML/CSS/JS only</p>
|
|
<ul><li>No server-side computation</li><li>Client-side inference with ONNX.js or TensorFlow.js</li><li>Fast loading, no compute costs</li></ul>
|
|
<h3>Space Configuration</h3>
|
|
<p><strong>README.md Metadata</strong>: Configure Space settings</p>
|
|
<pre><code class="lang-yaml">---
|
|
title: My Demo App
|
|
emoji: 🚀
|
|
colorFrom: blue
|
|
colorTo: purple
|
|
sdk: gradio # or streamlit, static
|
|
sdk_version: 4.0.0
|
|
app_file: app.py
|
|
pinned: false
|
|
license: mit
|
|
---</code></pre>
|
|
<p><strong>Hardware Options</strong>:</p>
|
|
<ul><li>CPU Basic (free): 2 vCPU, 16GB RAM</li><li>CPU Upgrade ($0.03/hr): 8 vCPU, 32GB RAM</li><li>T4 GPU ($0.60/hr): 16GB VRAM</li><li>A10G GPU ($3.15/hr): 24GB VRAM</li><li>A100 GPU ($4.13/hr): 40GB VRAM</li></ul>
|
|
<h3>Secrets Management</h3>
|
|
<p><strong>Environment Variables</strong>: Store API keys securely</p>
|
|
<ul><li>Accessible in app code via <code>os.environ</code></li><li>Not visible in public repos</li><li>Configure in Space settings UI</li><li>Support for HuggingFace tokens, OpenAI keys, etc.</li></ul>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Basic Gradio App</h3>
|
|
<pre><code class="lang-python">import gradio as gr
|
|
from transformers import pipeline
|
|
|
|
# Load model
|
|
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
# Define function
|
|
def classify_text(text):
|
|
result = classifier(text)[0]
|
|
return f"{result['label']}: {result['score']:.2%}"
|
|
|
|
# Create interface
|
|
demo = gr.Interface(
|
|
fn=classify_text,
|
|
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
|
outputs=gr.Textbox(label="Sentiment"),
|
|
title="Sentiment Analysis",
|
|
description="Analyze sentiment of text using DistilBERT",
|
|
examples=[
|
|
["I love this product!"],
|
|
["This is terrible."],
|
|
["It's okay, I guess."]
|
|
]
|
|
)
|
|
|
|
# Launch app
|
|
if __name__ == "__main__":
|
|
demo.launch()</code></pre>
|
|
<h3>Advanced Gradio with Multiple Inputs</h3>
|
|
<pre><code class="lang-python">import gradio as gr
|
|
from transformers import pipeline
|
|
|
|
generator = pipeline("text-generation", model="gpt2")
|
|
|
|
def generate_text(prompt, max_length, temperature, top_p):
|
|
result = generator(
|
|
prompt,
|
|
max_length=max_length,
|
|
temperature=temperature,
|
|
top_p=top_p,
|
|
num_return_sequences=1
|
|
)
|
|
return result[0]["generated_text"]
|
|
|
|
demo = gr.Interface(
|
|
fn=generate_text,
|
|
inputs=[
|
|
gr.Textbox(lines=3, label="Prompt"),
|
|
gr.Slider(10, 200, value=50, step=10, label="Max Length"),
|
|
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
|
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top P")
|
|
],
|
|
outputs=gr.Textbox(lines=5, label="Generated Text"),
|
|
title="GPT-2 Text Generator",
|
|
examples=[
|
|
["Once upon a time", 100, 0.8, 0.95],
|
|
["The future of AI", 150, 0.7, 0.9]
|
|
]
|
|
)
|
|
|
|
demo.launch()</code></pre>
|
|
<h3>Gradio with Image Input/Output</h3>
|
|
<pre><code class="lang-python">import gradio as gr
|
|
from PIL import Image
|
|
from transformers import pipeline
|
|
|
|
# Image classification
|
|
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
|
|
|
def classify_image(image):
|
|
results = classifier(image)
|
|
# Format results as dictionary for label component
|
|
return {result["label"]: result["score"] for result in results[:5]}
|
|
|
|
demo = gr.Interface(
|
|
fn=classify_image,
|
|
inputs=gr.Image(type="pil", label="Upload Image"),
|
|
outputs=gr.Label(num_top_classes=5, label="Predictions"),
|
|
title="Image Classification",
|
|
description="Classify images using Vision Transformer",
|
|
examples=["cat.jpg", "dog.jpg"]
|
|
)
|
|
|
|
demo.launch()</code></pre>
|
|
<h3>Basic Streamlit App</h3>
|
|
<pre><code class="lang-python">import streamlit as st
|
|
from transformers import pipeline
|
|
|
|
st.title("Sentiment Analysis App")
|
|
st.write("Analyze the sentiment of your text")
|
|
|
|
# Load model (cached)
|
|
@st.cache_resource
|
|
def load_model():
|
|
return pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
classifier = load_model()
|
|
|
|
# Input
|
|
text = st.text_area("Enter text:", height=100)
|
|
|
|
# Predict button
|
|
if st.button("Analyze"):
|
|
if text:
|
|
with st.spinner("Analyzing..."):
|
|
result = classifier(text)[0]
|
|
|
|
# Display result
|
|
st.success("Analysis Complete!")
|
|
col1, col2 = st.columns(2)
|
|
col1.metric("Label", result["label"])
|
|
col2.metric("Confidence", f"{result['score']:.2%}")
|
|
else:
|
|
st.warning("Please enter some text")
|
|
|
|
# Examples
|
|
st.subheader("Examples")
|
|
if st.button("Try Example: Positive"):
|
|
st.write("I love this product!")
|
|
if st.button("Try Example: Negative"):
|
|
st.write("This is terrible.")</code></pre>
|
|
<h3>Streamlit with Session State</h3>
|
|
<pre><code class="lang-python">import streamlit as st
|
|
from transformers import pipeline
|
|
|
|
st.title("Chat with GPT-2")
|
|
|
|
# Initialize session state
|
|
if "messages" not in st.session_state:
|
|
st.session_state.messages = []
|
|
|
|
# Load model
|
|
@st.cache_resource
|
|
def load_model():
|
|
return pipeline("text-generation", model="gpt2")
|
|
|
|
generator = load_model()
|
|
|
|
# Display chat history
|
|
for message in st.session_state.messages:
|
|
with st.chat_message(message["role"]):
|
|
st.write(message["content"])
|
|
|
|
# Chat input
|
|
if prompt := st.chat_input("Your message"):
|
|
# Add user message
|
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
with st.chat_message("user"):
|
|
st.write(prompt)
|
|
|
|
# Generate response
|
|
with st.chat_message("assistant"):
|
|
with st.spinner("Thinking..."):
|
|
response = generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
|
st.write(response)
|
|
|
|
# Add assistant message
|
|
st.session_state.messages.append({"role": "assistant", "content": response})</code></pre>
|
|
<h3>Space Configuration with README</h3>
|
|
<pre><code class="lang-markdown">---
|
|
title: Sentiment Analysis Demo
|
|
emoji: 😊
|
|
colorFrom: blue
|
|
colorTo: green
|
|
sdk: gradio
|
|
sdk_version: 4.0.0
|
|
app_file: app.py
|
|
pinned: false
|
|
license: mit
|
|
tags:
|
|
- sentiment-analysis
|
|
- nlp
|
|
- transformers
|
|
models:
|
|
- distilbert-base-uncased-finetuned-sst-2-english
|
|
---
|
|
|
|
# Sentiment Analysis Demo
|
|
|
|
This Space demonstrates sentiment analysis using DistilBERT.
|
|
|
|
## Features
|
|
- Real-time sentiment classification
|
|
- Confidence scores
|
|
- Example inputs
|
|
|
|
## Usage
|
|
Enter any text and click "Submit" to analyze sentiment.
|
|
|
|
## Model
|
|
Uses `distilbert-base-uncased-finetuned-sst-2-english` from HuggingFace.</code></pre>
|
|
<h3>Using Secrets</h3>
|
|
<pre><code class="lang-python">import os
|
|
import gradio as gr
|
|
from openai import OpenAI
|
|
|
|
# Access secret (set in Space settings)
|
|
api_key = os.environ.get("OPENAI_API_KEY")
|
|
|
|
if not api_key:
|
|
raise ValueError("OPENAI_API_KEY not found in secrets")
|
|
|
|
client = OpenAI(api_key=api_key)
|
|
|
|
def generate_with_gpt(prompt):
|
|
response = client.chat.completions.create(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": prompt}]
|
|
)
|
|
return response.choices[0].message.content
|
|
|
|
demo = gr.Interface(
|
|
fn=generate_with_gpt,
|
|
inputs=gr.Textbox(lines=3, label="Prompt"),
|
|
outputs=gr.Textbox(lines=5, label="Response"),
|
|
title="GPT-3.5 Generator"
|
|
)
|
|
|
|
demo.launch()</code></pre>
|
|
<h3>GPU-Accelerated Inference</h3>
|
|
<pre><code class="lang-python">import gradio as gr
|
|
import torch
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
# Check GPU availability
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
print(f"Using device: {device}")
|
|
|
|
# Load model to GPU
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
"meta-llama/Llama-2-7b-chat-hf",
|
|
torch_dtype=torch.float16,
|
|
device_map="auto"
|
|
)
|
|
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
|
|
|
|
def generate(prompt, max_length=100):
|
|
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
|
|
|
with torch.no_grad():
|
|
outputs = model.generate(
|
|
**inputs,
|
|
max_length=max_length,
|
|
temperature=0.7,
|
|
top_p=0.95,
|
|
do_sample=True
|
|
)
|
|
|
|
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
demo = gr.Interface(
|
|
fn=generate,
|
|
inputs=[
|
|
gr.Textbox(lines=3, label="Prompt"),
|
|
gr.Slider(50, 500, value=100, label="Max Length")
|
|
],
|
|
outputs=gr.Textbox(lines=10, label="Generated Text"),
|
|
title="Llama 2 Chat (GPU-Accelerated)"
|
|
)
|
|
|
|
demo.launch()</code></pre>
|
|
<h3>Custom Domain and Embedding</h3>
|
|
<pre><code class="lang-python">import gradio as gr
|
|
|
|
demo = gr.Interface(
|
|
fn=lambda x: x,
|
|
inputs=gr.Textbox(),
|
|
outputs=gr.Textbox(),
|
|
title="My App"
|
|
)
|
|
|
|
# Launch with custom settings
|
|
demo.launch(
|
|
share=False, # Don't create temporary share link
|
|
show_api=True, # Show API documentation
|
|
show_error=True # Show errors in UI
|
|
)</code></pre>
|
|
<p><strong>Embed in HTML</strong>:</p>
|
|
<pre><code class="lang-html"><!-- Embed Space in website -->
|
|
<iframe
|
|
src="https://username-space-name.hf.space"
|
|
frameborder="0"
|
|
width="850"
|
|
height="450"
|
|
></iframe>
|
|
|
|
<!-- Embed with Gradio JS -->
|
|
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/3.50.0/gradio.js"></script>
|
|
|
|
<gradio-app src="https://username-space-name.hf.space"></gradio-app></code></pre>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Gradio Components</h3>
|
|
<pre><code>Component | Type | Use Case
|
|
--------------------|-------------|---------------------------
|
|
Textbox | Input | Text input/output
|
|
Slider | Input | Numeric range selection
|
|
Image | Input | Image upload
|
|
Audio | Input | Audio upload
|
|
Video | Input | Video upload
|
|
File | Input | File upload
|
|
Label | Output | Classification results
|
|
JSON | Output | Structured data
|
|
HTML | Output | Custom HTML
|
|
Plot | Output | Matplotlib/Plotly plots</code></pre>
|
|
<h3>Streamlit Components</h3>
|
|
<pre><code>Component | Function | Use Case
|
|
--------------------|-----------------------|---------------------------
|
|
text_input() | st.text_input() | Single line text
|
|
text_area() | st.text_area() | Multi-line text
|
|
slider() | st.slider() | Numeric slider
|
|
selectbox() | st.selectbox() | Dropdown selection
|
|
file_uploader() | st.file_uploader() | File upload
|
|
image() | st.image() | Display image
|
|
dataframe() | st.dataframe() | Display table
|
|
chart() | st.line_chart() | Line chart
|
|
metric() | st.metric() | Key metric display</code></pre>
|
|
<h3>Hardware Tiers</h3>
|
|
<pre><code>Tier | vCPU | RAM | GPU VRAM | Cost/Hour | Use Case
|
|
--------------|------|-------|----------|-----------|---------------------------
|
|
CPU Basic | 2 | 16GB | - | Free | Light inference
|
|
CPU Upgrade | 8 | 32GB | - | $0.03 | CPU-intensive tasks
|
|
T4 Small | 4 | 15GB | 16GB | $0.60 | Small models (<7B)
|
|
A10G Large | 12 | 46GB | 24GB | $3.15 | Medium models (7-13B)
|
|
A100 Large | 12 | 142GB | 40GB | $4.13 | Large models (13B+)</code></pre>
|
|
<h3>Key Guidelines</h3>
|
|
<pre><code>✅ DO: Cache model loading with @st.cache_resource (Streamlit)
|
|
✅ DO: Provide example inputs for easier testing
|
|
✅ DO: Use secrets for API keys and tokens
|
|
✅ DO: Test locally before deploying
|
|
✅ DO: Set appropriate hardware tier for model size
|
|
✅ DO: Add loading states and error handling
|
|
|
|
❌ DON'T: Hardcode API keys in code
|
|
❌ DON'T: Load models on every function call
|
|
❌ DON'T: Use GPU for CPU-compatible models
|
|
❌ DON'T: Forget to add title and description</code></pre>
|
|
<hr>
|
|
<h2>Anti-Patterns</h2>
|
|
<h3>Critical Violations</h3>
|
|
<pre><code class="lang-python"># ❌ NEVER: Hardcode secrets
|
|
api_key = "sk-xxxxxxxxxxxx" # SECURITY RISK!
|
|
|
|
# ✅ CORRECT: Use environment variables
|
|
api_key = os.environ.get("OPENAI_API_KEY")</code></pre>
|
|
<p>❌ <strong>Hardcoded secrets</strong>: Security breach, exposed in public repos ✅ <strong>Correct approach</strong>: Use Space secrets in settings</p>
|
|
<h3>Common Mistakes</h3>
|
|
<pre><code class="lang-python"># ❌ Don't: Load model on every call (Streamlit)
|
|
def predict(text):
|
|
model = pipeline("sentiment-analysis") # Reloads every time!
|
|
return model(text)
|
|
|
|
# ✅ Correct: Cache model loading
|
|
@st.cache_resource
|
|
def load_model():
|
|
return pipeline("sentiment-analysis")
|
|
|
|
model = load_model()
|
|
|
|
def predict(text):
|
|
return model(text)</code></pre>
|
|
<p>❌ <strong>No model caching</strong>: Slow, wastes resources ✅ <strong>Better</strong>: Cache with <code>@st.cache_resource</code> or load globally</p>
|
|
<pre><code class="lang-python"># ❌ Don't: Use GPU for small models
|
|
# Running CPU-compatible model on A100 ($4/hr)
|
|
model = pipeline("sentiment-analysis", model="distilbert-base-uncased")
|
|
|
|
# ✅ Correct: Use CPU for small models
|
|
# Save costs, same performance
|
|
model = pipeline("sentiment-analysis", model="distilbert-base-uncased", device="cpu")</code></pre>
|
|
<p>❌ <strong>Wrong hardware tier</strong>: Wasted cost ✅ <strong>Better</strong>: Match hardware to model requirements</p>
|
|
<pre><code class="lang-python"># ❌ Don't: No error handling
|
|
def predict(text):
|
|
return model(text) # Crashes on invalid input
|
|
|
|
# ✅ Correct: Handle errors gracefully
|
|
def predict(text):
|
|
try:
|
|
if not text or len(text.strip()) == 0:
|
|
return "Please enter some text"
|
|
return model(text)
|
|
except Exception as e:
|
|
return f"Error: {str(e)}"</code></pre>
|
|
<p>❌ <strong>No error handling</strong>: Poor user experience ✅ <strong>Better</strong>: Validate inputs and catch exceptions</p>
|
|
<pre><code class="lang-python"># ❌ Don't: Missing examples
|
|
demo = gr.Interface(fn=predict, inputs=..., outputs=...)
|
|
|
|
# ✅ Correct: Provide examples
|
|
demo = gr.Interface(
|
|
fn=predict,
|
|
inputs=...,
|
|
outputs=...,
|
|
examples=[
|
|
["Example input 1"],
|
|
["Example input 2"]
|
|
]
|
|
)</code></pre>
|
|
<p>❌ <strong>No examples</strong>: Users don't know what to try ✅ <strong>Better</strong>: Always provide 2-5 example inputs</p>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>ml/huggingface/huggingface-hub.md</code> - Managing Space repositories</li><li><code>ml/huggingface/huggingface-transformers.md</code> - Loading models for Spaces</li><li><code>frontend/elegant-design.md</code> - Improving UI/UX of custom interfaces</li><li><code>modal/modal-web-endpoints.md</code> - Alternative deployment platform</li><li><code>infrastructure/cloudflare-workers.md</code> - Edge deployment alternative</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/ml-huggingface-spaces.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |