import os
import grpc
import core.sentiment as sentiment
import protobuf.sentiment_analysis_pb2 as sentiment_pb2

class SentimentServer():
  def __init__(self): 
    model_name = os.getenv("SENTIMENT_MODEL", "scherrmann/GermanFinBert_SC_Sentiment")
    self.analyzer = sentiment.SentimentAnalyzer(model_name=model_name)

  async def AnalyzeSentiment(
    self,
    request: sentiment_pb2.AnalyzeSentimentRequest,
    context: grpc.aio.ServicerContext,
  ) -> sentiment_pb2.AnalyzeSentimentResponse:
    response = self.analyzer.analyze(request.text)

    return sentiment_pb2.AnalyzeSentimentResponse(
      positiveScore=response["positive"],
      neutralScore=response["neutral"],
      negativeScore=response["negative"],
    )