Spaces:
Sleeping
Sleeping
File size: 10,653 Bytes
adcb9bd 217c046 adcb9bd 7495ae7 adcb9bd 67f3d72 adcb9bd 67f3d72 b2b2425 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 6a11a38 adcb9bd 6a11a38 adcb9bd 67f3d72 adcb9bd e8b5e4c ae2483e e8b5e4c ae2483e e8b5e4c adcb9bd ae2483e adcb9bd ae2483e e8b5e4c ae2483e e8b5e4c ae2483e e8b5e4c ae2483e e8b5e4c ae2483e e8b5e4c ae2483e 217c046 67f3d72 728a4ac 217c046 728a4ac 217c046 728a4ac 217c046 67f3d72 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 217c046 adcb9bd 6a11a38 217c046 6a11a38 217c046 6a11a38 217c046 6a11a38 67f3d72 217c046 b759464 adcb9bd 217c046 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
"""
HuggingFace ZeroGPU Space - OpenAI-compatible inference provider for opencode.
This Gradio app provides:
- OpenAI-compatible API via Gradio's native API system
- Pass-through model selection (any HF model ID)
- ZeroGPU H200 inference with HF Serverless fallback
- HF Token authentication
- SSE streaming support
"""
# Import spaces FIRST - required for ZeroGPU GPU detection
import spaces
import logging
import time
from typing import Optional
import gradio as gr
import httpx
from huggingface_hub import HfApi
from config import get_config, get_quota_tracker
from models import (
apply_chat_template,
generate_text,
generate_text_stream,
get_current_model,
)
from openai_compat import (
ChatCompletionRequest,
InferenceParams,
create_chat_response,
create_error_response,
estimate_tokens,
)
logger = logging.getLogger(__name__)
config = get_config()
quota_tracker = get_quota_tracker()
# HuggingFace API for token validation
hf_api = HfApi()
ZEROGPU_AVAILABLE = True
# --- Authentication ---
def validate_hf_token(token: str) -> bool:
"""Validate a HuggingFace token by checking with the API."""
if not token or not token.startswith("hf_"):
return False
try:
hf_api.whoami(token=token)
return True
except Exception:
return False
# --- ZeroGPU Inference Functions ---
# These MUST be decorated with @spaces.GPU for ZeroGPU detection
@spaces.GPU(duration=120)
def zerogpu_generate(
model_id: str,
prompt: str,
max_new_tokens: int,
temperature: float,
top_p: float,
) -> str:
"""Generate text using ZeroGPU (H200 GPU)."""
start_time = time.time()
result = generate_text(
model_id=model_id,
prompt=prompt,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
stop_sequences=None,
)
# Track quota usage
duration = time.time() - start_time
quota_tracker.add_usage(duration)
return result
# --- HF Serverless Fallback ---
def serverless_generate_sync(
model_id: str,
prompt: str,
max_new_tokens: int,
temperature: float,
top_p: float,
token: str,
) -> str:
"""Generate text using HuggingFace Serverless Inference API (sync version)."""
url = f"https://api-inference.huggingface.co/models/{model_id}"
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": max_new_tokens,
"temperature": temperature,
"top_p": top_p,
"return_full_text": False,
},
}
with httpx.Client() as client:
response = client.post(
url,
json=payload,
headers={"Authorization": f"Bearer {token}"},
timeout=120.0,
)
if response.status_code != 200:
raise Exception(f"HF Serverless error: {response.text}")
result = response.json()
# Handle different response formats
if isinstance(result, list) and len(result) > 0:
if "generated_text" in result[0]:
return result[0]["generated_text"]
raise Exception(f"Unexpected response format from HF Serverless: {result}")
# --- Gradio Chat Function (GPU decorated for ZeroGPU) ---
@spaces.GPU(duration=120)
def gradio_chat(
message: str,
history: list[list[str]],
model_id: str,
temperature: float,
max_tokens: int,
):
"""Gradio chat interface handler - GPU decorated for ZeroGPU."""
# Validate model_id
if not model_id:
return "Please select a model first."
# Build messages from history
messages = []
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
# Apply chat template
try:
prompt = apply_chat_template(model_id, messages)
except Exception as e:
return f"Error loading model: {str(e)}"
# Generate response (non-streaming for simplicity with ZeroGPU)
try:
response = generate_text(
model_id=model_id,
prompt=prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=0.95,
stop_sequences=None,
)
return response
except Exception as e:
return f"Error generating response: {str(e)}"
# --- API Functions for Gradio's gr.api() ---
def api_health() -> dict:
"""Health check endpoint."""
return {
"status": "healthy",
"zerogpu_available": ZEROGPU_AVAILABLE,
"quota_remaining_minutes": quota_tracker.remaining_minutes(),
"fallback_enabled": config.fallback_enabled,
}
def api_chat_completions(
token: str,
model: str,
messages: list[dict],
temperature: float = 0.7,
max_tokens: int = 512,
top_p: float = 0.95,
) -> dict:
"""
OpenAI-compatible chat completions.
Args:
token: HuggingFace API token (hf_xxx)
model: HuggingFace model ID (e.g., "meta-llama/Llama-3.1-8B-Instruct")
messages: List of message dicts with "role" and "content"
temperature: Sampling temperature (0.0-2.0)
max_tokens: Maximum tokens to generate
top_p: Nucleus sampling probability
Returns:
OpenAI-compatible response dict
"""
# Validate authentication
if not token or not validate_hf_token(token):
return create_error_response(
message="Invalid or missing HuggingFace token",
error_type="authentication_error",
code="invalid_api_key",
).model_dump()
# Apply chat template
try:
prompt = apply_chat_template(model, messages)
except Exception as e:
logger.error(f"Failed to apply chat template: {e}")
return create_error_response(
message=f"Failed to load model or apply chat template: {str(e)}",
error_type="invalid_request_error",
param="model",
).model_dump()
prompt_tokens = estimate_tokens(prompt)
# Determine inference method
use_zerogpu = ZEROGPU_AVAILABLE and not quota_tracker.quota_exhausted
if not use_zerogpu and not config.fallback_enabled:
return create_error_response(
message="ZeroGPU quota exhausted and fallback is disabled",
error_type="server_error",
code="quota_exhausted",
).model_dump()
try:
# Non-streaming response
if use_zerogpu:
response_text = zerogpu_generate(
model_id=model,
prompt=prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
else:
logger.info("Using HF Serverless fallback")
response_text = serverless_generate_sync(
model_id=model,
prompt=prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
token=token,
)
completion_tokens = estimate_tokens(response_text)
return create_chat_response(
model=model,
content=response_text,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
).model_dump()
except Exception as e:
logger.exception(f"Inference error: {e}")
return create_error_response(
message=f"Inference failed: {str(e)}",
error_type="server_error",
).model_dump()
# --- Build Gradio Interface ---
with gr.Blocks(title="ZeroGPU OpenCode Provider") as demo:
gr.Markdown(
"""
# ZeroGPU OpenCode Provider
OpenAI-compatible inference endpoint for [opencode](https://github.com/sst/opencode).
**API Endpoints:**
- `/api/health` - Health check
- `/api/chat_completions` - Chat completions (OpenAI-compatible response format)
## Usage with opencode
Configure in `~/.config/opencode/opencode.json`:
```json
{
"providers": {
"zerogpu": {
"npm": "@ai-sdk/openai-compatible",
"options": {
"baseURL": "https://serenichron-opencode-zerogpu.hf.space/api",
"headers": {
"Authorization": "Bearer hf_YOUR_TOKEN"
}
},
"models": {
"llama-8b": {
"name": "meta-llama/Llama-3.1-8B-Instruct"
}
}
}
}
}
```
---
"""
)
with gr.Row():
with gr.Column(scale=1):
model_dropdown = gr.Dropdown(
label="Model",
choices=[
"meta-llama/Llama-3.1-8B-Instruct",
"mistralai/Mistral-7B-Instruct-v0.3",
"Qwen/Qwen2.5-7B-Instruct",
"Qwen/Qwen2.5-14B-Instruct",
],
value="meta-llama/Llama-3.1-8B-Instruct",
allow_custom_value=True,
)
temperature_slider = gr.Slider(
label="Temperature",
minimum=0.0,
maximum=2.0,
value=0.7,
step=0.1,
)
max_tokens_slider = gr.Slider(
label="Max Tokens",
minimum=64,
maximum=4096,
value=512,
step=64,
)
gr.Markdown(
f"""
### Status
- **ZeroGPU:** {'Available' if ZEROGPU_AVAILABLE else 'Not Available'}
- **Fallback:** {'Enabled' if config.fallback_enabled else 'Disabled'}
"""
)
with gr.Column(scale=3):
chatbot = gr.ChatInterface(
fn=gradio_chat,
additional_inputs=[model_dropdown, temperature_slider, max_tokens_slider],
title="",
)
# Register API endpoints using Gradio's API system
# These will be available at /api/<name>
gr.api(api_health, api_name="health")
gr.api(api_chat_completions, api_name="chat_completions")
# --- Launch the application ---
# On HuggingFace Spaces, the runtime handles the launch automatically
# We just expose the demo object
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)
|