Spaces:
Runtime error
Runtime error
Commit ยท
b8415be
1
Parent(s): 6a54b49
Final deployment fixes: endpoints, dependencies, cache
Browse files- .gitignore +8 -13
- README.md +4 -9
- app.py +19 -19
- requirements.txt +5 -5
.gitignore
CHANGED
|
@@ -1,16 +1,11 @@
|
|
| 1 |
-
# System
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
*.
|
| 5 |
-
*.log
|
| 6 |
|
| 7 |
-
# Hugging Face
|
| 8 |
.cache/
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
.
|
| 12 |
-
.vscode/
|
| 13 |
-
|
| 14 |
-
# Python
|
| 15 |
-
__pycache__/
|
| 16 |
-
*.pyc
|
|
|
|
| 1 |
+
# System
|
| 2 |
+
.DS_Store
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
|
|
|
| 5 |
|
| 6 |
+
# Hugging Face
|
| 7 |
.cache/
|
| 8 |
+
/cache/
|
| 9 |
|
| 10 |
+
# Logs
|
| 11 |
+
*.log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,18 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title: GPT-2
|
| 3 |
emoji: ๐
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
sdk_version: 20.10.23
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# GPT-2
|
| 13 |
-
|
| 14 |
-
A FastAPI service for text generation using GPT-2 model.
|
| 15 |
-
|
| 16 |
-
## Endpoints
|
| 17 |
-
- POST `/generate` - Generate text
|
| 18 |
-
- GET `/health` - Service health check
|
|
|
|
| 1 |
---
|
| 2 |
+
title: GPT-2 Text Generation API
|
| 3 |
emoji: ๐
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
sdk_version: 20.10.23
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# GPT-2 FastAPI Deployment
|
| 13 |
+
Text generation API using Hugging Face Transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
import logging
|
| 2 |
-
import time
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 5 |
import torch
|
| 6 |
from pathlib import Path
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -16,45 +14,47 @@ app = FastAPI()
|
|
| 16 |
cache_dir = Path("/cache")
|
| 17 |
cache_dir.mkdir(exist_ok=True, parents=True)
|
| 18 |
|
| 19 |
-
# Load model
|
| 20 |
try:
|
| 21 |
-
logger.info("Loading
|
| 22 |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2', cache_dir=cache_dir)
|
| 23 |
model = GPT2LMHeadModel.from_pretrained('gpt2', cache_dir=cache_dir)
|
| 24 |
logger.info("Model loaded successfully!")
|
| 25 |
except Exception as e:
|
| 26 |
logger.error(f"Model loading failed: {e}")
|
| 27 |
-
raise RuntimeError("Model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
@app.post("/generate")
|
| 30 |
async def generate(request: Request):
|
| 31 |
try:
|
| 32 |
data = await request.json()
|
| 33 |
user_input = data.get("message", "").strip()
|
| 34 |
-
|
| 35 |
if not user_input:
|
| 36 |
-
raise HTTPException(
|
| 37 |
|
| 38 |
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
| 39 |
-
|
| 40 |
outputs = model.generate(
|
| 41 |
inputs,
|
| 42 |
max_length=100,
|
| 43 |
num_return_sequences=1,
|
| 44 |
pad_token_id=tokenizer.eos_token_id,
|
| 45 |
-
temperature=0.7
|
| 46 |
-
do_sample=True
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
response
|
| 50 |
-
|
| 51 |
-
|
| 52 |
except torch.cuda.OutOfMemoryError:
|
| 53 |
-
raise HTTPException(429, "
|
| 54 |
except Exception as e:
|
| 55 |
-
logger.error(f"
|
| 56 |
-
raise HTTPException(500, "
|
| 57 |
|
| 58 |
@app.get("/health")
|
| 59 |
-
async def
|
| 60 |
-
return {"status": "healthy"
|
|
|
|
| 1 |
import logging
|
|
|
|
| 2 |
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
import torch
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 14 |
cache_dir = Path("/cache")
|
| 15 |
cache_dir.mkdir(exist_ok=True, parents=True)
|
| 16 |
|
| 17 |
+
# Load model with explicit cache
|
| 18 |
try:
|
| 19 |
+
logger.info("Loading model...")
|
| 20 |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2', cache_dir=cache_dir)
|
| 21 |
model = GPT2LMHeadModel.from_pretrained('gpt2', cache_dir=cache_dir)
|
| 22 |
logger.info("Model loaded successfully!")
|
| 23 |
except Exception as e:
|
| 24 |
logger.error(f"Model loading failed: {e}")
|
| 25 |
+
raise RuntimeError("Model initialization failed")
|
| 26 |
+
|
| 27 |
+
@app.get("/")
|
| 28 |
+
async def root():
|
| 29 |
+
return {"status": "ready", "model": "gpt2"}
|
| 30 |
|
| 31 |
@app.post("/generate")
|
| 32 |
async def generate(request: Request):
|
| 33 |
try:
|
| 34 |
data = await request.json()
|
| 35 |
user_input = data.get("message", "").strip()
|
| 36 |
+
|
| 37 |
if not user_input:
|
| 38 |
+
raise HTTPException(400, "Empty input")
|
| 39 |
|
| 40 |
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
| 41 |
+
|
| 42 |
outputs = model.generate(
|
| 43 |
inputs,
|
| 44 |
max_length=100,
|
| 45 |
num_return_sequences=1,
|
| 46 |
pad_token_id=tokenizer.eos_token_id,
|
| 47 |
+
temperature=0.7
|
|
|
|
| 48 |
)
|
| 49 |
+
|
| 50 |
+
return {"response": tokenizer.decode(outputs[0], "status": "success"}
|
| 51 |
+
|
|
|
|
| 52 |
except torch.cuda.OutOfMemoryError:
|
| 53 |
+
raise HTTPException(429, "Prompt too long")
|
| 54 |
except Exception as e:
|
| 55 |
+
logger.error(f"Error: {str(e)}")
|
| 56 |
+
raise HTTPException(500, "Generation failed")
|
| 57 |
|
| 58 |
@app.get("/health")
|
| 59 |
+
async def health_check():
|
| 60 |
+
return {"status": "healthy"}
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
fastapi>=0.
|
| 2 |
-
uvicorn>=0.
|
| 3 |
-
transformers>=4.
|
| 4 |
-
torch>=
|
| 5 |
python-multipart
|
| 6 |
-
|
|
|
|
| 1 |
+
fastapi>=0.85.0
|
| 2 |
+
uvicorn>=0.19.0
|
| 3 |
+
transformers>=4.28.1
|
| 4 |
+
torch>=2.0.0
|
| 5 |
python-multipart
|
| 6 |
+
huggingface_hub[cli]
|