Spaces:
Sleeping
Sleeping
Commit
Β·
7d4bdcd
1
Parent(s):
51c3f70
Updated app
Browse files- app.py +24 -20
- images/anti_joke.jpg +0 -0
- images/dad_joke.jpg +0 -0
- images/dark_joke.jpg +0 -0
- images/dropout.jpg +0 -0
- images/neutral.jpg +0 -0
- images/pun.png +0 -0
- images/riddle.jpg +0 -0
- images/stand-up-comedy.jpg +0 -0
- pages/guess_page.py +63 -0
- pages/pred_page.py +16 -10
- pred_jokes.json +0 -0
app.py
CHANGED
@@ -1,36 +1,40 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
from pages.pred_page import *
|
|
|
|
|
4 |
|
5 |
from streamlit_option_menu import option_menu
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
if __name__ == '__main__':
|
23 |
|
24 |
st.set_page_config( page_title="Home", layout="wide", initial_sidebar_state="collapsed", page_icon = "ποΈ")
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
selected2 = option_menu(None, ["Predict", "Take A Guess", "Tasks", 'Settings'],
|
30 |
icons=['house', 'cloud-upload', "list-task", 'gear'],
|
31 |
menu_icon="cast", default_index=0, orientation="horizontal")
|
32 |
|
33 |
if selected2 == "Predict":
|
34 |
-
|
|
|
35 |
elif selected2 == "Take A Guess":
|
36 |
-
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
from pages.pred_page import *
|
4 |
+
from pages.guess_page import *
|
5 |
+
import base64
|
6 |
|
7 |
from streamlit_option_menu import option_menu
|
8 |
|
9 |
+
def get_base64(bin_file):
|
10 |
+
with open(bin_file, 'rb') as f:
|
11 |
+
data = f.read()
|
12 |
+
return base64.b64encode(data).decode()
|
13 |
+
|
14 |
+
def set_background(png_file):
|
15 |
+
bin_str = get_base64(png_file)
|
16 |
+
page_bg_img = '''
|
17 |
+
<style>
|
18 |
+
.stApp {
|
19 |
+
background-image: url("data:image/jpg;base64,%s");
|
20 |
+
background-size: cover;
|
21 |
+
}
|
22 |
+
</style>
|
23 |
+
''' % bin_str
|
24 |
+
st.markdown(page_bg_img, unsafe_allow_html=True)
|
25 |
|
26 |
if __name__ == '__main__':
|
27 |
|
28 |
st.set_page_config( page_title="Home", layout="wide", initial_sidebar_state="collapsed", page_icon = "ποΈ")
|
29 |
|
30 |
+
selected2 = option_menu(None, ["Predict", "Take A Guess"],
|
|
|
|
|
|
|
31 |
icons=['house', 'cloud-upload', "list-task", 'gear'],
|
32 |
menu_icon="cast", default_index=0, orientation="horizontal")
|
33 |
|
34 |
if selected2 == "Predict":
|
35 |
+
set_background("images/stand-up-comedy.jpg")
|
36 |
+
pred_page_config()
|
37 |
elif selected2 == "Take A Guess":
|
38 |
+
set_background("images/dropout.jpg")
|
39 |
+
|
40 |
+
guess_page_config()
|
images/anti_joke.jpg
ADDED
![]() |
images/dad_joke.jpg
ADDED
![]() |
images/dark_joke.jpg
ADDED
![]() |
images/dropout.jpg
ADDED
![]() |
images/neutral.jpg
ADDED
![]() |
images/pun.png
ADDED
![]() |
images/riddle.jpg
ADDED
![]() |
images/stand-up-comedy.jpg
ADDED
![]() |
pages/guess_page.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from streamlit_extras.metric_cards import style_metric_cards
|
4 |
+
|
5 |
+
from streamlit_modal import Modal
|
6 |
+
|
7 |
+
import streamlit.components.v1 as components
|
8 |
+
|
9 |
+
import json, random
|
10 |
+
|
11 |
+
with open("pred_jokes.json", "r") as f:
|
12 |
+
jokes = json.load(f)
|
13 |
+
|
14 |
+
def make_grid(cols,rows):
|
15 |
+
grid = [0]*cols
|
16 |
+
for i in range(cols):
|
17 |
+
with st.container():
|
18 |
+
grid[i] = st.columns(rows)
|
19 |
+
return grid
|
20 |
+
|
21 |
+
def guess_page_config():
|
22 |
+
|
23 |
+
st.header("Guess The Type: Guess the category of the randomly generated joke below", divider='blue')
|
24 |
+
|
25 |
+
random_col, reveal_col, _, _, _, _, _, _, _, _, _, _ = st.columns(12)
|
26 |
+
|
27 |
+
with random_col:
|
28 |
+
random_but = st.button("Random", type="secondary")
|
29 |
+
|
30 |
+
with reveal_col:
|
31 |
+
reveal_but = st.button("Reveal", type="secondary")
|
32 |
+
|
33 |
+
if random_but:
|
34 |
+
random_joke = random.choice(jokes)
|
35 |
+
|
36 |
+
st.session_state.joke = random_joke
|
37 |
+
|
38 |
+
joke = st.session_state.joke["joke"].replace("\n", " ").strip()
|
39 |
+
|
40 |
+
tar_cat = random_joke["target_category"]
|
41 |
+
|
42 |
+
pred_cat = random_joke["pred_category"]
|
43 |
+
|
44 |
+
st.subheader(f"_{joke}_")
|
45 |
+
|
46 |
+
if reveal_but:
|
47 |
+
joke = st.session_state.joke["joke"].replace("\n", " ").strip()
|
48 |
+
|
49 |
+
st.subheader(f"_{joke}_")
|
50 |
+
|
51 |
+
tar_cat = st.session_state.joke["target_category"]
|
52 |
+
|
53 |
+
pred_cat = st.session_state.joke["pred_category"]
|
54 |
+
|
55 |
+
with st.container():
|
56 |
+
|
57 |
+
cat_grid = make_grid(4, 4)
|
58 |
+
|
59 |
+
cat_grid[0][0].metric(":green[Actual Category]", tar_cat, "π―")
|
60 |
+
|
61 |
+
cat_grid[0][1].metric(":red[Predicted Category]", pred_cat, "π―")
|
62 |
+
|
63 |
+
style_metric_cards(border_left_color="#9AD8E1", background_color="#000000")
|
pages/pred_page.py
CHANGED
@@ -4,16 +4,13 @@ from torch import nn
|
|
4 |
|
5 |
from transformers import GPT2Model, GPT2Tokenizer
|
6 |
|
7 |
-
from audio_recorder_streamlit import audio_recorder
|
8 |
-
|
9 |
-
import speech_recognition as sr
|
10 |
-
|
11 |
import torch.nn.functional as F
|
12 |
|
13 |
from streamlit_extras.metric_cards import style_metric_cards
|
14 |
|
15 |
from streamlit_card import card
|
16 |
|
|
|
17 |
|
18 |
class SimpleGPT2SequenceClassifier(nn.Module):
|
19 |
def __init__(self, hidden_size: int, num_classes:int ,max_seq_len:int, gpt_model_name:str):
|
@@ -33,6 +30,15 @@ class SimpleGPT2SequenceClassifier(nn.Module):
|
|
33 |
return linear_output
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def make_grid(cols,rows):
|
37 |
grid = [0]*cols
|
38 |
for i in range(cols):
|
@@ -103,7 +109,7 @@ def pred_page_config():
|
|
103 |
card(
|
104 |
title="Dad Joke",
|
105 |
text=f"Confidence: {prob_dict['Dad Joke π¨']}",
|
106 |
-
image="
|
107 |
styles={
|
108 |
"card": {
|
109 |
"width": "100%",
|
@@ -116,7 +122,7 @@ def pred_page_config():
|
|
116 |
card(
|
117 |
title="Riddle",
|
118 |
text=f"Confidence: {prob_dict['Riddle β']}",
|
119 |
-
image="
|
120 |
styles={
|
121 |
"card": {
|
122 |
"width": "100%",
|
@@ -129,7 +135,7 @@ def pred_page_config():
|
|
129 |
card(
|
130 |
title="Dark Joke",
|
131 |
text=f"Confidence: {prob_dict['Dark Joke π']}",
|
132 |
-
image="
|
133 |
styles={
|
134 |
"card": {
|
135 |
"width": "100%",
|
@@ -142,7 +148,7 @@ def pred_page_config():
|
|
142 |
card(
|
143 |
title="Pun",
|
144 |
text=f"Confidence: {prob_dict['Pun π']}",
|
145 |
-
image="
|
146 |
styles={
|
147 |
"card": {
|
148 |
"width": "100%",
|
@@ -155,7 +161,7 @@ def pred_page_config():
|
|
155 |
card(
|
156 |
title="Anti Joke",
|
157 |
text=f"Confidence: {prob_dict['Anti Joke π«']}",
|
158 |
-
image="
|
159 |
styles={
|
160 |
"card": {
|
161 |
"width": "100%",
|
@@ -168,7 +174,7 @@ def pred_page_config():
|
|
168 |
card(
|
169 |
title="Neutral",
|
170 |
text=f"Confidence: {prob_dict['Neutral π']}",
|
171 |
-
image="
|
172 |
styles={
|
173 |
"card": {
|
174 |
"width": "100%",
|
|
|
4 |
|
5 |
from transformers import GPT2Model, GPT2Tokenizer
|
6 |
|
|
|
|
|
|
|
|
|
7 |
import torch.nn.functional as F
|
8 |
|
9 |
from streamlit_extras.metric_cards import style_metric_cards
|
10 |
|
11 |
from streamlit_card import card
|
12 |
|
13 |
+
import base64
|
14 |
|
15 |
class SimpleGPT2SequenceClassifier(nn.Module):
|
16 |
def __init__(self, hidden_size: int, num_classes:int ,max_seq_len:int, gpt_model_name:str):
|
|
|
30 |
return linear_output
|
31 |
|
32 |
|
33 |
+
def get_img(filepath):
|
34 |
+
with open(filepath, "rb") as f:
|
35 |
+
data = f.read()
|
36 |
+
encoded = base64.b64encode(data)
|
37 |
+
|
38 |
+
data = "data:image/png;base64," + encoded.decode('utf-8')
|
39 |
+
|
40 |
+
return data
|
41 |
+
|
42 |
def make_grid(cols,rows):
|
43 |
grid = [0]*cols
|
44 |
for i in range(cols):
|
|
|
109 |
card(
|
110 |
title="Dad Joke",
|
111 |
text=f"Confidence: {prob_dict['Dad Joke π¨']}",
|
112 |
+
image=get_img("./images/dad_joke.jpg"),
|
113 |
styles={
|
114 |
"card": {
|
115 |
"width": "100%",
|
|
|
122 |
card(
|
123 |
title="Riddle",
|
124 |
text=f"Confidence: {prob_dict['Riddle β']}",
|
125 |
+
image=get_img("./images/riddle.jpg"),
|
126 |
styles={
|
127 |
"card": {
|
128 |
"width": "100%",
|
|
|
135 |
card(
|
136 |
title="Dark Joke",
|
137 |
text=f"Confidence: {prob_dict['Dark Joke π']}",
|
138 |
+
image=get_img("./images/dark_joke.jpg"),
|
139 |
styles={
|
140 |
"card": {
|
141 |
"width": "100%",
|
|
|
148 |
card(
|
149 |
title="Pun",
|
150 |
text=f"Confidence: {prob_dict['Pun π']}",
|
151 |
+
image=get_img("./images/pun.png"),
|
152 |
styles={
|
153 |
"card": {
|
154 |
"width": "100%",
|
|
|
161 |
card(
|
162 |
title="Anti Joke",
|
163 |
text=f"Confidence: {prob_dict['Anti Joke π«']}",
|
164 |
+
image=get_img("./images/anti_joke.jpg"),
|
165 |
styles={
|
166 |
"card": {
|
167 |
"width": "100%",
|
|
|
174 |
card(
|
175 |
title="Neutral",
|
176 |
text=f"Confidence: {prob_dict['Neutral π']}",
|
177 |
+
image=get_img("./images/neutral.jpg"),
|
178 |
styles={
|
179 |
"card": {
|
180 |
"width": "100%",
|
pred_jokes.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|