Alejadro Sanchez-Giraldo commited on
Commit
4190c8f
·
1 Parent(s): 185801a

add test framework and skin change on flag

Browse files
Files changed (4) hide show
  1. .gitignore +6 -1
  2. app.py +29 -0
  3. requirements.txt +3 -1
  4. test/test_chatbot.py +27 -0
.gitignore CHANGED
@@ -2,4 +2,9 @@ myenv/
2
  .DS_Store
3
  .env
4
 
5
- __pycache__/
 
 
 
 
 
 
2
  .DS_Store
3
  .env
4
 
5
+ __pycache__/
6
+ .pytest_cache/
7
+
8
+ report.html
9
+ style.css
10
+ screenshots/
app.py CHANGED
@@ -3,6 +3,8 @@ from transformers import pipeline
3
  from ldclient import LDClient, Config, Context
4
  import os
5
 
 
 
6
  # Retrieve the LaunchDarkly SDK key from environment variables
7
  ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY")
8
 
@@ -21,6 +23,13 @@ def get_model_config(user_name):
21
  model_id = flag_variation.get("modelID", "distilbert-base-uncased")
22
  return model_id
23
 
 
 
 
 
 
 
 
24
  # Function to translate sentiment labels to user-friendly terms
25
  def translate_label(label):
26
  label_mapping = {
@@ -35,6 +44,26 @@ def translate_label(label):
35
  }
36
  return label_mapping.get(label, "Unknown")
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Streamlit app
39
  st.title("Sentiment Analysis Demo with AI Model Flags")
40
 
 
3
  from ldclient import LDClient, Config, Context
4
  import os
5
 
6
+ style = False
7
+
8
  # Retrieve the LaunchDarkly SDK key from environment variables
9
  ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY")
10
 
 
23
  model_id = flag_variation.get("modelID", "distilbert-base-uncased")
24
  return model_id
25
 
26
+ # Function to get Style from LaunchDarkly
27
+ def get_style_config():
28
+ flag_key = "style"
29
+ style_context = Context.builder("context-key-style").build()
30
+ flag_variation = ld_client.variation(flag_key, style_context,default=False)
31
+ return flag_variation
32
+
33
  # Function to translate sentiment labels to user-friendly terms
34
  def translate_label(label):
35
  label_mapping = {
 
44
  }
45
  return label_mapping.get(label, "Unknown")
46
 
47
+ style = get_style_config()
48
+
49
+ # popup with the styel value
50
+ st.write(f"Style: {style}")
51
+
52
+ if style:
53
+ custom_css = """
54
+ <style>
55
+ html, body {
56
+ height: 100%;
57
+ }
58
+ .main{
59
+ background: green;
60
+ }
61
+ </style>
62
+ """
63
+ st.markdown(custom_css, unsafe_allow_html=True)
64
+ else:
65
+ cust_css = ""
66
+
67
  # Streamlit app
68
  st.title("Sentiment Analysis Demo with AI Model Flags")
69
 
requirements.txt CHANGED
@@ -4,4 +4,6 @@ launchdarkly-server-sdk
4
  torch
5
  Flask
6
  datasets
7
- gunicorn
 
 
 
4
  torch
5
  Flask
6
  datasets
7
+ gunicorn
8
+ pytest-playwright
9
+ pytest-html
test/test_chatbot.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from playwright.sync_api import Page, expect, Browser
3
+
4
+ def test_has_title(page: Page):
5
+ page.goto("http://localhost:8501")
6
+
7
+ # Expect a title "to contain" a substring.
8
+ expect(page).to_have_title(re.compile("app"))
9
+
10
+ def test_get_started_link(browser: Browser):
11
+
12
+ for _ in range(30):
13
+ # create a new incognito browser context
14
+ context = browser.new_context()
15
+ # create a new page inside context.
16
+ page = context.new_page()
17
+ # clear cache
18
+ page.context.clear_cookies()
19
+ page.goto("http://localhost:8501")
20
+
21
+ # expect Sentiment Analysis Demo with AI Model Flags
22
+ locator = page.locator('.main')
23
+ expect(locator).to_contain_text("Sentiment Analysis Demo with AI Model Flags")
24
+ # screenshot
25
+ page.screenshot(path=f"test_get_started_link{_}.png")
26
+ # dispose context once it is no longer needed.
27
+ context.close()