File size: 2,828 Bytes
71faf96
 
 
 
ca5fd4e
4190c8f
 
ca5fd4e
 
 
 
 
 
4f516f9
0a21b81
4f516f9
 
 
 
db12616
4f516f9
 
 
 
 
4190c8f
 
 
 
 
 
 
4f516f9
 
 
 
 
77d5f1f
 
 
 
 
 
4f516f9
 
 
4190c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f516f9
 
 
 
 
0a21b81
 
 
 
 
 
 
4f516f9
0a21b81
4f516f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import streamlit as st
from transformers import pipeline
from ldclient import LDClient, Config, Context
import os

style = False

# Retrieve the LaunchDarkly SDK key from environment variables
ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY")

# Initialize LaunchDarkly client with the correct configuration
ld_client = LDClient(Config(ld_sdk_key))

# Function to get the AI model configuration from LaunchDarkly
def get_model_config(user_name):
    flag_key = "model-swap"  # Replace with your flag key

    # Create a context using Context Builder—it can be anything, but for this use case, I’m just defaulting to myself.

    context = Context.builder(f"context-key-{user_name}").name(user_name).build()
    flag_variation = ld_client.variation(flag_key, context, default={})
    
    model_id = flag_variation.get("modelID", "distilbert-base-uncased")
    return model_id

# Function to get Style from LaunchDarkly
def get_style_config():
    flag_key = "style"
    style_context = Context.builder("context-key-style").build()
    flag_variation = ld_client.variation(flag_key, style_context,default=False)
    return flag_variation

# Function to translate sentiment labels to user-friendly terms
def translate_label(label):
    label_mapping = {
        "LABEL_0": "🤬 Negative",
        "LABEL_1": "😶 Neutral",
        "LABEL_2": "😃 Positive",
        "1 star": "🤬 Negative",
        "2 stars": "🤬 Negative",
        "3 stars": "😶 Neutral",
        "4 stars": "😃 Positive",
        "5 stars": "😃 Positive"
    }
    return label_mapping.get(label, "Unknown")

style = get_style_config()

# popup with the styel value
st.write(f"Style: {style}")

if style:
    custom_css = """
        <style>
            html, body {
                height: 100%;
            }
            .main{
                background: green;
            }
        </style>
        """
    st.markdown(custom_css, unsafe_allow_html=True)
else:
    cust_css = ""
    
# Streamlit app
st.title("Sentiment Analysis Demo with AI Model Flags")

user_input = st.text_area("Enter text for sentiment analysis:")

# Add an input box for the user to enter their name
name = st.text_input("Enter your name", "AJ")

# if no name is anter add anonymous
if not name:
    name = "Anonymous"

if st.button("Analyze"):
    model_id = get_model_config(name)
    model = pipeline("sentiment-analysis", model=model_id)
    
    # Display model details
    st.write(f"Using model: {model_id}")
    
    # Perform sentiment analysis
    results = model(user_input)
    st.write("Results:")

    # Translate and display the results
    for result in results:
        label = translate_label(result['label'])
        score = result['score']
        st.write(f"Sentiment: {label}, Confidence: {score:.2f}")

# Closing the LD client
ld_client.close()