erinmikail commited on
Commit
ac9ffd6
·
verified ·
1 Parent(s): 05a9484

update with appropriate keys + context builder

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -1,38 +1,38 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
  import ldclient
4
- from ldclient.config import Config
5
 
6
  # Initialize LaunchDarkly client
7
- ld_client = launchdarkly_api.LDClient("LAUNCHDARKLY_SDK_KEY")
 
8
 
9
- # Model descriptions
10
- model_descriptions = {
11
- "bert-base-uncased": "BERT base model (uncased)",
12
- "roberta-base": "RoBERTa base model",
13
- "distilbert-base-uncased": "DistilBERT base model (uncased)",
14
- "albert-base-v2": "ALBERT base model v2"
15
- }
 
 
16
 
17
- # Create a function to get the active model from LaunchDarkly
18
- def get_active_model():
19
- if ld_client.variation("use_bert", {"key": "user"}):
20
- return pipeline("sentiment-analysis", model="bert-base-uncased"), "bert-base-uncased"
21
- elif ld_client.variation("use_roberta", {"key": "user"}):
22
- return pipeline("sentiment-analysis", model="roberta-base"), "roberta-base"
23
- elif ld_client.variation("use_distilbert", {"key": "user"}):
24
- return pipeline("sentiment-analysis", model="distilbert-base-uncased"), "distilbert-base-uncased"
25
- elif ld_client.variation("use_albert", {"key": "user"}):
26
- return pipeline("sentiment-analysis", model="albert-base-v2"), "albert-base-v2"
27
- else:
28
- return pipeline("sentiment-analysis", model="distilbert-base-uncased"), "distilbert-base-uncased" # Default model
29
 
30
  # Streamlit app
31
- st.title("Sentiment Analysis Demo")
 
32
  user_input = st.text_area("Enter text for sentiment analysis:")
33
 
34
  if st.button("Analyze"):
35
- model, model_name = get_active_model()
 
 
 
 
 
 
36
  result = model(user_input)
37
- st.write(f"Model used: {model_descriptions[model_name]}")
38
- st.write(result)
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
  import ldclient
4
+ from ldclient import LDClient, Context
5
 
6
  # Initialize LaunchDarkly client
7
+ ldclient.set_config(Config("LAUNCHDARKLY_SDK_KEY"))
8
+ client = ldclient.get()
9
 
10
+ # Function to get the AI model configuration from LaunchDarkly
11
+ def get_model_config():
12
+ flag_key = "swap-sentiment-models" # Replace with your flag key
13
+ # Create context using Context builder
14
+ context = Context.builder("context-key-123abc").name("Sandy").build()
15
+ flag_variation = ld_client.variation(flag_key, context, default={})
16
+
17
+ model_id = flag_variation.get("modelID", "distilbert-base-uncased")
18
+ return model_id
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Streamlit app
22
+ st.title("Sentiment Analysis Demo with AI Model Flags")
23
+
24
  user_input = st.text_area("Enter text for sentiment analysis:")
25
 
26
  if st.button("Analyze"):
27
+ model_id = get_model_config()
28
+ model = pipeline("sentiment-analysis", model=model_id)
29
+
30
+ # Display model details
31
+ st.write(f"Using model: {model_id}")
32
+
33
+ # Perform sentiment analysis
34
  result = model(user_input)
35
+ st.write(result)
36
+
37
+ # Closing the LD client
38
+ ld_client.close()