File size: 1,268 Bytes
667fe9d
 
85ac990
 
 
667fe9d
 
85ac990
667fe9d
85ac990
 
667fe9d
85ac990
667fe9d
 
85ac990
 
 
667fe9d
 
85ac990
 
 
 
 
 
 
 
667fe9d
 
85ac990
 
 
 
667fe9d
85ac990
 
 
 
 
667fe9d
 
85ac990
 
 
 
 
 
667fe9d
 
85ac990
 
 
 
667fe9d
 
85ac990
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import annotations

import os
from functools import lru_cache
from typing import TYPE_CHECKING

import gradio as gr
import joblib

if TYPE_CHECKING:
    from sklearn.pipeline import Pipeline

__all__ = ["launch_gui"]


POSITIVE_LABEL = "Positive 😊"
NEUTRAL_LABEL = "Neutral 😐"
NEGATIVE_LABEL = "Negative 😀"


@lru_cache(maxsize=1)
def load_model() -> Pipeline:
    """Load the trained model and cache it."""
    model_path = os.environ.get("MODEL_PATH", None)
    if model_path is None:
        msg = "MODEL_PATH environment variable not set"
        raise ValueError(msg)
    return joblib.load(model_path)


def sentiment_analysis(text: str) -> str:
    """Perform sentiment analysis on the provided text."""
    model = load_model()
    prediction = model.predict([text])[0]

    if prediction == 0:
        return NEGATIVE_LABEL
    if prediction == 1:
        return POSITIVE_LABEL
    return NEUTRAL_LABEL


demo = gr.Interface(
    fn=sentiment_analysis,
    inputs="text",
    outputs="label",
    title="Sentiment Analysis",
)


def launch_gui(model_path: str, share: bool) -> None:
    """Launch the Gradio GUI."""
    os.environ["MODEL_PATH"] = model_path
    demo.launch(share=share)


if __name__ == "__main__":
    demo.launch()