Coder Social home page Coder Social logo

tddi-model-service's Introduction

🤖 Model Service

TDDI Model Service

Geliştirilmiş olan BERT Base, Türkçe doğal dil işleme ile hakaret tespiti modelinin çeşitli uygulamalardaki kullanımını kolaylaştırmak amacıyla geliştirilmiş bir mikroservistir. Deployment işlemleri AWS EC2 üzerinden sağlanmaktadır.

Swagger dökümanına erişmek için tıklayınız

Örnek İstek Fonksiyonu

import requests
import pandas as pd
import datetime

def fetch_predictions(df: pd.DataFrame) -> pd.DataFrame:
    """
    Sends a request to the TDDI-Model-Service prediction endpoint with a given DataFrame and retrieves the predictions for each text in the DataFrame.

    Parameters
    ----------
    df : pd.DataFrame
        A DataFrame containing a 'text' column that includes the texts to be predicted.

    Returns
    -------
    pd.DataFrame
        A DataFrame that includes the original 'text' column, a new 'clean_text' column that includes the cleaned version of the texts, 
        and two new columns that include the predicted target class and whether the text is offensive or not.
        
    Examples
    --------
    >>> import pandas as pd
    >>> pd.DataFrame({'text': ['Bu bir örnek metindir.','Bu da bir örnek metin!']})
    >>> result = get_predictions(df)
    >>> print(result.head())
    
                         text   target      is_offensive
    0  bu bir örnek metindir    OTHER             0
    1  bu da bir örnek metin    OTHER             0
    """
    print('Bağlantı kuruluyor..')
    start_date = datetime.datetime.now()
    api_url = "http://44.210.240.127/docs"
    response = requests.post(api_url, json={"texts": list(df.text)})
    end_date = datetime.datetime.now()
    print(f'sonuc döndü bu dönüş: {end_date-start_date} zaman sürdü.')

    predictions = response.json()['result']['model']
    
    for i, prediction in enumerate(predictions):
        df.at[i, 'target'] = prediction['prediction']
        df.at[i, 'is_offensive'] = int(prediction['is_offensive'])
    
    df['is_offensive'] = df['is_offensive'].astype(int)

    return df

Ortam Oluşturma

Lütfen Python sürümünüzü '3.10' olarak ayarlayın.

Python versiyonunuzdan emin olmak için:

python3 --version

Geliştirme Ortamını Ayarlamak

  • Virtual environment oluşturunuz.
    $ python -m venv <venv-name>
  • Virtual environmentınızı aktive ediniz.
    $ source <venv-name>/bin/activate
  • Kütüphaneleri Yükleyiniz.
    $ pip install -r requirements.txt

Çalıştırma

Uygulamanın çalışması için gerekli adımlar tamamlanmıştır.

    $ python3 main.py

App 5000 portunda çalışmaktadır.

http://localhost:5000/

tddi-model-service's People

Contributors

aerdincdal avatar seymasa avatar tarikkaankoc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tddi-model-service's Issues

📌 Bug: ImportError: cannot import name 'BERTModelMicroService' from partially initialized module 'wsgi' (most likely due to a circular import)

Error msj:


from wsgi import BERTModelMicroService
ImportError: cannot import name 'BERTModelMicroService' from partially initialized module 'wsgi' (most likely due to a circular import) (/Users/koc/tddi-model-service/wsgi.py)

Endpoint


from wsgi import BERTModelMicroService

bert_serivce = BERTModelMicroService()

@model_router.get("/example_dump_model")
async def get_label_score(texts: List[str]):
    preprocess_url = "https://cryptic-oasis-68424.herokuapp.com/bulk_preprocess?turkish_char=true"
   
    for text in texts:
        preprocess_response = requests.post(
            preprocess_url, json={"text": text})
        processed_text = preprocess_response.json()["result"]

        results = bert_serivce.predict(processed_text)

    return {"success": True,
            "payload": results}

wsgi:


from fastapi import FastAPI
from api.controllers.model_controller import model_router
from fastapi.middleware.cors import CORSMiddleware
from transformers import BertTokenizer, TFBertForSequenceClassification
from transformers import TextClassificationPipeline

BERT_MODEL_PATH = "api/static/model/bigscience_t0_model"
BERT_TOKENIZER_PATH = "api/static/model/bigscience_t0_tokenizer"


class BERTModelMicroService:
    def __init__(self):
        """
        Initializes a new instance of the BERTModelMicroService class.
        """
        self.app = FastAPI(
            title="BERT Model Micro Service",
            version="0.1.0",
            description="This API analyzes Turkish text using BERT, a natural language processing technology. "
                        "It helps Telco and OTT brands to monitor and analyze Turkish text data to identify patterns in customer feedback "
                        "or detect inappropriate language, and improve their customer experience and reputation management."
        )
        self.make_middleware()
        self.bert_model = TFBertForSequenceClassification.from_pretrained(BERT_MODEL_PATH, from_pt=True) 
        self.bert_tokenizer = BertTokenizer.from_pretrained(BERT_TOKENIZER_PATH, do_lower_case=True)
        self.pipeline = TextClassificationPipeline(model=self.bert_model, tokenizer=self.bert_tokenizer)

    def predict(self, processed_text):
        results = [f"{processed_text[index]} - {i['label']}" for index, i in enumerate(self.pipeline(processed_text))]
        return results

    def make_middleware(self):
        """
        Adds middleware to the application to enable cross-origin resource sharing.
        """
        self.app.add_middleware(
            CORSMiddleware,
            allow_origins=["*"],
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"]
        )

    def init_routes(self):
        """
        Initialize routes for the application.

        Parameters
        ----------
        app : FastAPI
            The FastAPI instance to attach the routes to.

        Returns
        -------
        None
        """
        @self.app.get("/healthcheck")
        async def healthcheck():
            return {"success": True}

        self.app.include_router(model_router, prefix="/api/v1")

BERTModelMicroService = BERTModelMicroService()
app = BERTModelMicroService.app

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.