gordon-posit commited on
Commit
fe5044d
1 Parent(s): b92bfb2

Initial commit

Browse files
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+
2
+ .venv/
3
+ __pycache__/
4
+
5
+ .DS_Store
.vscode/launch.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Run Shiny app",
9
+ "type": "python",
10
+ "request": "launch",
11
+ "module": "shiny",
12
+ "args": ["run", "${file}"],
13
+ "jinja": true,
14
+ "justMyCode": true
15
+ },
16
+ {
17
+ "name": "Test",
18
+ "type": "python",
19
+ "request": "launch",
20
+ "module": "pytest",
21
+ "args": [],
22
+ "justMyCode": true
23
+ }
24
+ ]
25
+ }
.vscode/settings.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "files.trimTrailingWhitespace": true,
3
+ "files.insertFinalNewline": true,
4
+ "python.formatting.provider": "black",
5
+ "python.linting.flake8Enabled": true,
6
+ "editor.tabSize": 2,
7
+ "files.encoding": "utf8",
8
+ "files.eol": "\n",
9
+ "[python]": {
10
+ "editor.formatOnSave": true,
11
+ "editor.tabSize": 4,
12
+ "editor.codeActionsOnSave": {
13
+ "source.organizeImports": true
14
+ },
15
+ },
16
+ "isort.args":["--profile", "black"],
17
+ "editor.rulers": [
18
+ 88
19
+ ],
20
+ "files.exclude": {
21
+ "**/__pycache__": true,
22
+ "build/**": true
23
+ },
24
+ "autoDocstring.guessTypes": false,
25
+ "search.exclude": {
26
+ "build/**": true
27
+ },
28
+ }
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from shiny import App, reactive, ui
2
+ from query import query_output_server, query_output_ui
3
+ from htmltools import tags
4
+ import shinyswatch
5
+
6
+ app_ui = ui.page_fluid(
7
+ shinyswatch.theme("zephyr"),
8
+ ui.row(
9
+ ui.column(
10
+ 2,
11
+ ui.br(),
12
+ ui.br(),
13
+ ui.input_action_button("add_query", "Add Query"),
14
+ ),
15
+ ui.column(
16
+ 10,
17
+ ui.tags.div(query_output_ui("initial_query"), id="module_container"),
18
+ ),
19
+ ),
20
+ )
21
+
22
+
23
+ def server(input, output, session):
24
+ mod_counter = reactive.Value(0)
25
+
26
+ query_output_server("initial_query")
27
+
28
+ @reactive.Effect
29
+ @reactive.event(input.add_query)
30
+ def _():
31
+ counter = mod_counter.get() + 1
32
+ mod_counter.set(counter)
33
+ id = "query_" + str(counter)
34
+ ui.insert_ui(
35
+ selector="#module_container", where="afterBegin", ui=query_output_ui(id)
36
+ )
37
+ query_output_server(id)
38
+
39
+
40
+ app = App(app_ui, server)
query.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from plotnine import aes, geom_point, ggplot, labs, theme_light
2
+ from shiny import module, ui, reactive, render
3
+ import pandas as pd
4
+ import requests
5
+ from htmltools import tags
6
+
7
+
8
+ @module.ui
9
+ def query_output_ui():
10
+ out = ui.row(
11
+ ui.column(
12
+ 4,
13
+ ui.input_text("prompt", "Prompt", placeholder="Enter query"),
14
+ ),
15
+ ui.column(4, ui.output_table("score_table")),
16
+ ui.column(4, ui.output_plot("score_plot")),
17
+ )
18
+
19
+ return out
20
+
21
+
22
+ @module.server
23
+ def query_output_server(input, output, session):
24
+ @reactive.Calc
25
+ def response_table():
26
+ # This is included to both show the expected API response, and populate
27
+ # the downstream item with zeros before a prompt is entered.
28
+ if input.prompt() == "":
29
+ resp = [
30
+ [
31
+ {"label": "neutral", "score": 0},
32
+ {"label": "surprise", "score": 0},
33
+ {"label": "fear", "score": 0},
34
+ {"label": "anger", "score": 0},
35
+ {"label": "disgust", "score": 0},
36
+ {"label": "sadness", "score": 0},
37
+ {"label": "joy", "score": 0},
38
+ ]
39
+ ]
40
+ else:
41
+ resp = query(input.prompt())
42
+
43
+ df = pd.DataFrame(
44
+ {
45
+ "sentiment": [x["label"] for x in resp[0]],
46
+ "score": [x["score"] for x in resp[0]],
47
+ }
48
+ )
49
+ return df
50
+
51
+ @output
52
+ @render.plot
53
+ def score_plot():
54
+ return plot_response(response_table(), input.prompt())
55
+
56
+ @output
57
+ @render.table()
58
+ def score_table():
59
+ return response_table()
60
+
61
+
62
+ def plot_response(df, plot_title):
63
+ out = (
64
+ ggplot(df, aes(y="reorder(sentiment, score)", x="score"))
65
+ + geom_point()
66
+ + theme_light()
67
+ + labs(title=f'Prompt: "{plot_title}"', y="Sentiment", x="Score")
68
+ )
69
+ return out
70
+
71
+
72
+ def query(text):
73
+ API_URL = "https://api-inference.huggingface.co/models/j-hartmann/emotion-english-distilroberta-base"
74
+ headers = {"Authorization": "Bearer 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"}
75
+ payload = {"inputs": text}
76
+ response = requests.post(API_URL, headers=headers, json=payload)
77
+ return response.json()
requirements.txt ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ anyio==3.6.2
2
+ appdirs==1.4.4
3
+ asgiref==3.6.0
4
+ black==23.3.0
5
+ certifi==2022.12.7
6
+ charset-normalizer==3.1.0
7
+ click==8.1.3
8
+ contextvars==2.4
9
+ contourpy==1.0.7
10
+ cycler==0.11.0
11
+ fonttools==4.39.3
12
+ h11==0.14.0
13
+ htmltools==0.2.1
14
+ idna==3.4
15
+ immutables==0.19
16
+ Jinja2==3.1.2
17
+ kiwisolver==1.4.4
18
+ linkify-it-py==2.0.0
19
+ markdown-it-py==2.2.0
20
+ MarkupSafe==2.1.2
21
+ matplotlib==3.7.1
22
+ mdit-py-plugins==0.3.5
23
+ mdurl==0.1.2
24
+ mizani==0.8.1
25
+ mypy-extensions==1.0.0
26
+ numpy==1.24.2
27
+ packaging==23.0
28
+ palettable==3.3.1
29
+ pandas==2.0.0
30
+ pathspec==0.11.1
31
+ patsy==0.5.3
32
+ Pillow==9.5.0
33
+ platformdirs==3.2.0
34
+ plotnine==0.10.1
35
+ pyparsing==3.0.9
36
+ python-dateutil==2.8.2
37
+ python-multipart==0.0.6
38
+ pytz==2023.3
39
+ requests==2.28.2
40
+ scipy==1.10.1
41
+ shiny==0.3.0
42
+ shinyswatch==0.1.1
43
+ six==1.16.0
44
+ sniffio==1.3.0
45
+ starlette==0.26.1
46
+ statsmodels==0.13.5
47
+ tomli==2.0.1
48
+ typing_extensions==4.5.0
49
+ tzdata==2023.3
50
+ uc-micro-py==1.0.1
51
+ urllib3==1.26.15
52
+ uvicorn==0.21.1
53
+ websockets==11.0.1
54
+ XStatic-bootswatch==3.3.7.0
sentence-sentiment.code-workspace ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ {
2
+ "folders": [
3
+ {
4
+ "path": "."
5
+ }
6
+ ]
7
+ }