Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
benya7 committed Sep 18, 2024
1 parent bca88a0 commit 024ec06
Show file tree
Hide file tree
Showing 8 changed files with 516 additions and 70 deletions.
12 changes: 6 additions & 6 deletions runner/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def load_pipeline(pipeline: str, model_id: str) -> any:
from app.pipelines.upscale import UpscalePipeline

return UpscalePipeline(model_id)
case "sentiment-analysis":
from app.pipelines.sentiment_analysis import SentimentAnalysisPipeline
case "text-sentiment-analysis":
from app.pipelines.text_sentiment_analysis import TextSentimentAnalysisPipeline

return SentimentAnalysisPipeline(model_id)
return TextSentimentAnalysisPipeline(model_id)
case "segment-anything-2":
from app.pipelines.segment_anything_2 import SegmentAnything2Pipeline

Expand Down Expand Up @@ -88,10 +88,10 @@ def load_route(pipeline: str) -> any:
from app.routes import upscale

return upscale.router
case "sentiment-analysis":
from app.routes import sentiment_analysis
case "text-sentiment-analysis":
from app.routes import text_sentiment_analysis

return sentiment_analysis.router
return text_sentiment_analysis.router
case "segment-anything-2":
from app.routes import segment_anything_2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
logger = logging.getLogger(__name__)


class SentimentAnalysisPipeline(Pipeline):
class TextSentimentAnalysisPipeline(Pipeline):
def __init__(self, model_id: str):
self.model_id = model_id
kwargs = {}
Expand All @@ -31,4 +31,4 @@ def __call__(self, text: str, **kwargs):
return self.ldm(text, **kwargs)

def __str__(self) -> str:
return f"SentimentAnalysisPipeline model_id={self.model_id}"
return f"TextSentimentAnalysisPipeline model_id={self.model_id}"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from app.dependencies import get_pipeline
from app.pipelines.base import Pipeline
from app.routes.util import HTTPError, SentimentAnalysisResponse, http_error
from app.routes.util import HTTPError, TextSentimentAnalysisResponse, http_error
from fastapi import APIRouter, Depends, status
from fastapi.responses import JSONResponse
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
Expand All @@ -15,6 +15,15 @@
logger = logging.getLogger(__name__)

RESPONSES = {
status.HTTP_200_OK: {
"content": {
"application/json": {
"schema": {
"x-speakeasy-name-override": "data",
}
}
},
},
status.HTTP_400_BAD_REQUEST: {"model": HTTPError},
status.HTTP_401_UNAUTHORIZED: {"model": HTTPError},
status.HTTP_500_INTERNAL_SERVER_ERROR: {"model": HTTPError},
Expand All @@ -41,18 +50,22 @@ def handle_pipeline_error(e: Exception) -> JSONResponse:


@router.post(
"/sentiment-analysis",
response_model=SentimentAnalysisResponse,
"/text-sentiment-analysis",
response_model=TextSentimentAnalysisResponse,
responses=RESPONSES,
description="Analyze the sentiment of the provided text."
description="Analyze the sentiment of a given text inputs.",
operation_id="analyzeSentiment",
summary="Text Sentiment Analysis",
tags=["analysis"],
openapi_extra={"x-speakeasy-name-override": "textSentimentAnalysis"},
)
@router.post(
"/sentiment-analysis/",
response_model=SentimentAnalysisResponse,
"/text-sentiment-analysis/",
response_model=TextSentimentAnalysisResponse,
responses=RESPONSES,
include_in_schema=False,
)
async def sentiment_analysis(
async def text_sentiment_analysis(
model_id: Annotated[
str,
Field(
Expand All @@ -62,7 +75,7 @@ async def sentiment_analysis(
],
text_input: Annotated[
str,
Field(description="Text to classify. Separate multiple sentences by commas.")
Field(description="Text to analyze. For multiple sentences, separate them with commas.")
],
pipeline: Pipeline = Depends(get_pipeline),
token: HTTPAuthorizationCredentials = Depends(HTTPBearer(auto_error=False)),
Expand Down
2 changes: 1 addition & 1 deletion runner/app/routes/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class LabelScore(BaseModel):
score: float


class SentimentAnalysisResponse(BaseModel):
class TextSentimentAnalysisResponse(BaseModel):
results: list[LabelScore]


Expand Down
86 changes: 86 additions & 0 deletions runner/gateway.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,64 @@ paths:
security:
- HTTPBearer: []
x-speakeasy-name-override: segmentAnything2
/text-sentiment-analysis:
post:
tags:
- analysis
summary: Text Sentiment Analysis
description: Analyze the sentiment of a given text inputs.
operationId: analyzeSentiment
security:
- HTTPBearer: []
parameters:
- name: model_id
in: query
required: true
schema:
type: string
description: Hugging Face model ID used for text classsification.
title: Model Id
- name: text_input
in: query
required: true
schema:
type: string
description: Text to analyze. For multiple sentences, separate them with
commas.
title: Text Input
responses:
'200':
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/TextSentimentAnalysisResponse'
x-speakeasy-name-override: data
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
description: Bad Request
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
description: Unauthorized
'500':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
description: Internal Server Error
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-speakeasy-name-override: textSentimentAnalysis
components:
schemas:
APIError:
Expand Down Expand Up @@ -586,6 +644,23 @@ components:
- images
title: ImageResponse
description: Response model for image generation.
LabelScore:
properties:
label:
type: string
enum:
- negative
- neutral
- positive
title: Label
score:
type: number
title: Score
type: object
required:
- label
- score
title: LabelScore
MasksResponse:
properties:
masks:
Expand Down Expand Up @@ -646,6 +721,17 @@ components:
- chunks
title: TextResponse
description: Response model for text generation.
TextSentimentAnalysisResponse:
properties:
results:
items:
$ref: '#/components/schemas/LabelScore'
type: array
title: Results
type: object
required:
- results
title: TextSentimentAnalysisResponse
TextToImageParams:
properties:
model_id:
Expand Down
4 changes: 3 additions & 1 deletion runner/gen_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
image_to_image,
image_to_video,
segment_anything_2,
text_sentiment_analysis,
text_to_image,
upscale,
)
Expand Down Expand Up @@ -123,7 +124,8 @@ def write_openapi(fname: str, entrypoint: str = "runner", version: str = "0.0.0"
app.include_router(upscale.router)
app.include_router(audio_to_text.router)
app.include_router(segment_anything_2.router)

app.include_router(text_sentiment_analysis.router)

logger.info(f"Generating OpenAPI schema for '{entrypoint}' entrypoint...")
openapi = get_openapi(
title="Livepeer AI Runner",
Expand Down
86 changes: 86 additions & 0 deletions runner/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,64 @@ paths:
security:
- HTTPBearer: []
x-speakeasy-name-override: segmentAnything2
/text-sentiment-analysis:
post:
tags:
- analysis
summary: Text Sentiment Analysis
description: Analyze the sentiment of a given text inputs.
operationId: analyzeSentiment
security:
- HTTPBearer: []
parameters:
- name: model_id
in: query
required: true
schema:
type: string
description: Hugging Face model ID used for text classsification.
title: Model Id
- name: text_input
in: query
required: true
schema:
type: string
description: Text to analyze. For multiple sentences, separate them with
commas.
title: Text Input
responses:
'200':
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/TextSentimentAnalysisResponse'
x-speakeasy-name-override: data
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
description: Bad Request
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
description: Unauthorized
'500':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
description: Internal Server Error
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
x-speakeasy-name-override: textSentimentAnalysis
components:
schemas:
APIError:
Expand Down Expand Up @@ -600,6 +658,23 @@ components:
- images
title: ImageResponse
description: Response model for image generation.
LabelScore:
properties:
label:
type: string
enum:
- negative
- neutral
- positive
title: Label
score:
type: number
title: Score
type: object
required:
- label
- score
title: LabelScore
MasksResponse:
properties:
masks:
Expand Down Expand Up @@ -660,6 +735,17 @@ components:
- chunks
title: TextResponse
description: Response model for text generation.
TextSentimentAnalysisResponse:
properties:
results:
items:
$ref: '#/components/schemas/LabelScore'
type: array
title: Results
type: object
required:
- results
title: TextSentimentAnalysisResponse
TextToImageParams:
properties:
model_id:
Expand Down
Loading

0 comments on commit 024ec06

Please sign in to comment.