|
import streamlit as st |
|
from transformers import pipeline |
|
from ldclient import LDClient, Config, Context |
|
import os |
|
import torch |
|
|
|
|
|
ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY") |
|
|
|
|
|
ld_client = LDClient(Config(ld_sdk_key)) |
|
|
|
|
|
def get_model_config(): |
|
flag_key = "swap-sentiment-models" |
|
|
|
context = Context.builder("context-key-123abc").name("Erin").build() |
|
flag_variation = ld_client.variation(flag_key, context, default={}) |
|
|
|
model_id = flag_variation.get("modelID", "distilbert-base-uncased") |
|
return model_id |
|
|
|
|
|
def translate_label(label): |
|
label_mapping = { |
|
"LABEL_0": "π€¬ Negative", |
|
"LABEL_1": "πΆ Neutral", |
|
"LABEL_2": "π Positive" |
|
} |
|
return label_mapping.get(label, "Unknown") |
|
|
|
|
|
|
|
st.title("Sentiment Analysis Demo with AI Model Flags") |
|
|
|
user_input = st.text_area("Enter text for sentiment analysis:") |
|
|
|
if st.button("Analyze"): |
|
model_id = get_model_config() |
|
model = pipeline("sentiment-analysis", model=model_id) |
|
|
|
|
|
st.write(f"Using model: {model_id}") |
|
|
|
|
|
results = model(user_input) |
|
st.write("Results:") |
|
|
|
|
|
for result in results: |
|
label = translate_label(result['label']) |
|
score = result['score'] |
|
st.write(f"Sentiment: {label}, Confidence: {score:.2f}") |
|
|
|
|
|
ld_client.close() |
|
|