[Python] Cannot send a request through Fastapi in Python (Failed to connect to localhost port...

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I devised a spam detecter for my example but I cannot send any request through Postman

    Here is the requirement.txt file

    fastapi
    uvicorn[standard]
    transformers
    torch


    Here is my python file shown below

    from fastapi import FastAPI, HTTPException
    from pydantic import BaseModel, Field
    from transformers import pipeline

    class ClassifyRequest(BaseModel):
    text: str = Field(..., example="")
    lang: str = Field(
    "en",
    pattern=r"^(en|tr|de|fr|es|it|pt|ru|ar|zh|ja|ko|hi|bn|ur|fa|th|vi|id|ms|nl|sv|no|da|fi|pl|cs|sk|hu|ro|bg|hr|sr|sl|et|lv|lt|el|he|uk|be|ky|uz|km|my|tg|az|hy|ga|cy|is|mk|bs|sq|mn|ne|pa|gl|la)$",
    description="ISO language code",
    example="tr"
    )

    class ClassifyResponse(BaseModel):
    label: str
    score: float

    app = FastAPI(title="Spam & Abuse Detector")

    classifier = pipeline(
    "zero-shot-classification",
    model="joeddav/xlm-roberta-large-xnli"
    )

    CANDIDATE_LABELS = ["spam", "adult_content", "drugs", "non_spam"]

    @app.post("/classify", response_model=ClassifyResponse)
    def classify(req: ClassifyRequest):
    res = classifier(
    sequences=req.text,
    candidate_labels=CANDIDATE_LABELS
    )
    best_idx = res["scores"].index(max(res["scores"]))
    label = res["labels"][best_idx]
    score = res["scores"][best_idx]
    return ClassifyResponse(label=label, score=score)


    Here is Dockerfile

    FROM python:3.10-slim

    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt

    COPY app ./app

    EXPOSE 8000
    CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--log-level", "info"]


    When I run these commands shown below

    docker build -t spam-detector .
    docker run -p 8000:8000 spam-detector


    I got this console output

    INFO: Will watch for changes in these directories: ['/app']
    INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
    INFO: Started reloader process [1] using WatchFiles


    When I send a request through Postman

    curl -X POST http://127.0.0.1:8000/classify \
    -H "Content-Type: application/json" \
    -d '{"text":"bla bla","lang":"en"}'


    I get "Failed to connect to localhost port 8000 after 0 ms: Couldn't connect to server"

    How can I fix the issue?

    Continue reading...

Compartilhe esta Página