Spaces:
Runtime error
Runtime error
File size: 1,981 Bytes
cce0194 |
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 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import torch
app = FastAPI()
# Model configuration
MODEL_NAME = "nlptown/bert-base-multilingual-uncased-sentiment"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer, device=DEVICE)
class TextInput(BaseModel):
text: str
@app.post("/analyze-sentiment")
async def analyze_sentiment(input_data: TextInput):
try:
result = classifier(input_data.text)
return {
"sentiment": result[0]['label'],
"score": float(result[0]['score'])
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Przykład dla większego modelu (np. GPT-2)
MODEL_NAME_LARGE = "gpt2-large"
tokenizer_large = AutoTokenizer.from_pretrained(MODEL_NAME_LARGE)
model_large = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME_LARGE)
class GenerationInput(BaseModel):
prompt: str
max_length: int = 100
@app.post("/generate-text")
async def generate_text(input_data: GenerationInput):
try:
inputs = tokenizer_large(input_data.prompt, return_tensors="pt")
outputs = model_large.generate(
inputs["input_ids"],
max_length=input_data.max_length,
num_return_sequences=1,
no_repeat_ngram_size=2
)
generated_text = tokenizer_large.decode(outputs[0], skip_special_tokens=True)
return {"generated_text": generated_text}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Dodanie podstawowego health checka
@app.get("/health")
async def health_check():
return {"status": "healthy"} |