Steps

  1. Create a Hugging Face account and log in: Visit huggingface.co to sign up; no payment method required.
  2. Create a new Space: Click "New Space", choose an SDK (Gradio or Docker), and select hardware "CPU basic" (free) or "GPU T4 small" (free but queued, usually with a daily quota limit).
  3. Write the inference app: In the Space's app.py, load a lightweight open-source model (e.g., google/flan-t5-small, a quantized version of microsoft/phi-2) and use the transformers pipeline to implement a text generation endpoint. Example code:

```python

from transformers import pipeline

generator = pipeline('text-generation', model='microsoft/phi-2', device=-1) # CPU

def generate(prompt):

return generator(prompt, max_length=100)[0]['generated_text']

```

For GPU hardware, set device=0 to enable CUDA.

  1. Expose an API endpoint: Gradio apps provide a default /api/predict endpoint, callable via https://<username>-<space-name>.hf.space/api/predict. The request body is JSON with a data field.
  2. Use Datasets for input/output storage: For batch tasks, save prompts as a Hugging Face Dataset (private or public), stream-read them in the Space via datasets.load_dataset, and write results back to a new Dataset. Free accounts get a certain amount of private dataset storage (typically a few GB).
  3. Schedule triggers and manage queues: Use GitHub Actions or local cron to send requests to the Space API periodically, avoiding manual intervention. Note that free Spaces sleep when idle; the first request may be delayed.

Precautions

  • Free CPU instances have limited performance, suitable for small models (<1B parameters) or short text generation; GPU instances require queuing and have limited daily usage hours (typically a few hours), not suitable for high concurrency.
  • Waking a sleeping Space takes tens of seconds; consider sending a warm-up request beforehand.
  • Private dataset storage for free accounts is limited; clean up regularly.
  • Comply with Hugging Face content policies; do not use for generating prohibited content.
  • This approach is not suitable for low-latency or large-context-window scenarios.

Use Cases

  • Individual developers doing prototype validation or small-scale text generation.
  • Educational purposes, such as student project demos.
  • Batch tasks with low real-time requirements (e.g., overnight batch summarization).
  • As a backup inference channel when the main API quota is exhausted.