Alejadro Sanchez-Giraldo commited on
Commit
4f516f9
1 Parent(s): ca5fd4e

app creation

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -19,3 +19,50 @@ user = {
19
  }
20
 
21
  context = Context(user)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  }
20
 
21
  context = Context(user)
22
+
23
+
24
+ # Function to get the AI model configuration from LaunchDarkly
25
+ def get_model_config():
26
+ flag_key = "model-swap" # Replace with your flag key
27
+
28
+ # Create a context using Context Builder—it can be anything, but for this use case, I’m just defaulting to myself.
29
+
30
+ context = Context.builder("context-key-123abc").name("AJ").build()
31
+ flag_variation = ld_client.variation(flag_key, context, default={})
32
+
33
+ model_id = flag_variation.get("modelID", "distilbert-base-uncased")
34
+ return model_id
35
+
36
+ # Function to translate sentiment labels to user-friendly terms
37
+ def translate_label(label):
38
+ label_mapping = {
39
+ "LABEL_0": "🤬 Negative",
40
+ "LABEL_1": "😶 Neutral",
41
+ "LABEL_2": "😃 Positive"
42
+ }
43
+ return label_mapping.get(label, "Unknown")
44
+
45
+ # Streamlit app
46
+ st.title("Sentiment Analysis Demo with AI Model Flags")
47
+
48
+ user_input = st.text_area("Enter text for sentiment analysis:")
49
+
50
+ if st.button("Analyze"):
51
+ model_id = get_model_config()
52
+ model = pipeline("sentiment-analysis", model=model_id)
53
+
54
+ # Display model details
55
+ st.write(f"Using model: {model_id}")
56
+
57
+ # Perform sentiment analysis
58
+ results = model(user_input)
59
+ st.write("Results:")
60
+
61
+ # Translate and display the results
62
+ for result in results:
63
+ label = translate_label(result['label'])
64
+ score = result['score']
65
+ st.write(f"Sentiment: {label}, Confidence: {score:.2f}")
66
+
67
+ # Closing the LD client
68
+ ld_client.close()