Skip to content

Commit

Permalink
280: Rename route, add models supported langages endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Smixie committed Dec 23, 2024
1 parent 2bd0e34 commit 37b97fb
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
Binary file added nlp/docs/__pycache__/conf.cpython-310.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions nlp/src/base_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,29 @@ def json(self):
"metadata": self.metadata,
"results": self.results
}


class ModelSupportedLanguages(BaseModel):
"""Response model for used language"""
sentiment: List[str]
language: List[str]
sarcasm: List[str]
keywords: List[str]
spam: List[str]
political: List[str]
hateSpeech: List[str]
clickbait: List[str]
llmDetection: List[str]

def json(self):
return {
"sentiment": self.sentiment,
"language": self.language,
"sarcasm": self.sarcasm,
"keywords": self.keywords,
"spam": self.spam,
"political": self.political,
"hateSpeech": self.hateSpeech,
"clickbait": self.clickbait,
"llmDetection": self.llmDetection
}
38 changes: 32 additions & 6 deletions nlp/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.params import Depends

from docs.conf import language
from src.base_models import *
from src.logger_config import logger
from src.models_loading import *
Expand All @@ -31,7 +33,7 @@ async def lifespan(application: FastAPI):
raise

models = [
"language", "sentiment", "ai_detector", "sarcasm", "spam",
"language", "sentiment", "llm_detection", "sarcasm", "spam",
"political", "hate_speech", "clickbait", "keywords"
]

Expand Down Expand Up @@ -388,7 +390,7 @@ def process_fn(text):
).json()


@app.post("/ai-detector", response_model=TextResponse,
@app.post("/llm-detection", response_model=TextResponse,
dependencies=[Depends(auth.get_api_key)])
async def get_ai_bots(request: TextRequest):
"""
Expand All @@ -408,11 +410,11 @@ def process_fn(text):
prediction = gl.ai_detector_pipeline_english(text)
confidence = prediction[0]["score"]
if confidence > 0.5:
label="AI"
label = "AI"
confidence = confidence_output(confidence)
else:
label="Human"
confidence = confidence_output(1-confidence)
label = "Human"
confidence = confidence_output(1 - confidence)

result = AnalysisResult(
labels=[label],
Expand All @@ -423,9 +425,33 @@ def process_fn(text):
results, generation_time = measure_execution_time(
lambda: process_inputs(process_fn, request.text))


return TextResponse(
kind="aiDetector",
metadata=Metadata(generated_in=generation_time),
results=results
).json()


@app.post("/info/langs", response_model=ModelSupportedLanguages,
dependencies=[Depends(auth.get_api_key)])
async def get_languages():
"""
Get languages used in the application.
Returns:
ModelSupportedLanguages: Languages used in the application.
"""
with open("version_models.json", "r") as file:
data = json.load(file)

return ModelSupportedLanguages(
sentiment=data.get("sentiment", []),
language=data.get("language", []),
sarcasm=data.get("sarcasm", []),
keywords=data.get("keywords", []),
spam=data.get("spam", []),
political=data.get("political", []),
hateSpeech=data.get("hate_speech", []),
clickbait=data.get("clickbait", []),
llmDetection=data.get("llm_detection", [])
).json()
4 changes: 2 additions & 2 deletions nlp/src/models_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def load_model(model_name: str):
elif model_name == "clickbait":
g.clickbait_pipeline = pipeline("text-classification", model=model,
tokenizer=tokenizer)
elif model_name == "ai_detector":
elif model_name == "llm_detection":
g.ai_detector_pipeline_english = pipeline("text-classification",
model=model, tokenizer=tokenizer)
model=model, tokenizer=tokenizer)
7 changes: 5 additions & 2 deletions nlp/version_models.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"language": [
"english"
"all"
],
"sentiment": [
"english",
Expand All @@ -22,7 +22,10 @@
"clickbait": [
"english"
],
"ai_detector": [
"llm_detection": [
"english"
],
"keywords": [
"all"
]
}

0 comments on commit 37b97fb

Please sign in to comment.