Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.py 6.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  1. ############# LIBRARIES ################
  2. import streamlit as st
  3. import joblib
  4. import pandas as pd
  5. import re
  6. from transformers import pipeline
  7. from langdetect import detect
  8. import plotly.express as px
  9. from PIL import Image
  10. #from package import sentiment_predictor
  11. # Model definition
  12. #model = pipeline("zero-shot-classification",
  13. # model='joeddav/xlm-roberta-large-xnli')
  14. # Save the model
  15. #joblib.dump(model, './model/sentiment_detet.pkl')
  16. ############# GBLOBAL VARIABLES ################
  17. flag_pattern = u"\U0001F1E0-\U0001F1FF"
  18. title = "Multi Language 🌎 Sentiment Classification Application "
  19. app_dsc = "Your App to predict the sentiment of a text in <i>English</i>, <i>French</i>, <i>German</i>, <i>Russian</i> or <i>Spanish</i>"
  20. dags_hub_repo_info = "© By Zoumana Keita | Source Code on DagsHub."
  21. metrics_info = "📊 Model Performances Per Language 👇🏼"
  22. side_bar_info = "Change the metric in the sidebar (Top/Left)"
  23. lang_error = "ℹ️ Please select one of the 5 languages above"
  24. dags_hub_repo = "https://dagshub.com/zoumana.keita/Multi_language_Sentiment_Classification"
  25. # Textual information
  26. usage_options = """
  27. <div id="block1" style="float:left;">
  28. <h3>Option 1</h3>
  29. <ul>
  30. <li>Select a text to predict</li>
  31. <li>Click Predict</li>
  32. </ul>
  33. </div>
  34. <div id="block2" style="float:right;">
  35. <h3>Option 2</h3">
  36. <ul>
  37. <li>Provide Your own text</li>
  38. <li>Click Predict</li>
  39. </ul>
  40. </div>
  41. """
  42. fr_text = "🇫🇷Je pense que tu peux mieux faire prochainement, n'abandonne surtout pas"
  43. en_text = "🇺🇸This covid19 is becoming a nightmare. I hope things will get better soon"
  44. ger_text = "🇩🇪ambiente eines billigen strandclubs in der türkei, der nachbar sitzt fast auf dem schoss weil es eng ist, die musik laut und unpassend ( fetenhits 80er ), gedränge und warme getränke die man gewöhnlich kalt trinkt. der eingang wird von 2 arroganten kleinen mädchen bedient, die sich auf irgendetwas was einbilden, unklar auf was. dazu gehen im laden afrikanische prostituierte auf männerfang. achja das essen: zu teuer, aber gut. für 1/3 des preises in anderen lokalen anzurufen. fazit: viel lärm um nichts"
  45. rus_text = "🇷🇺спасибо большое отелю за хорошую организацию. в октябре арендовали зал для проведения тренинга для команды из 50 человек. все скромно, но достойно. менеджер андрей оперативно реагировал на все наши просьбы. например: произошла нестыковка кофе-брейка по времени, нужно было организовать перерыв раньше - через 10 мин все было накрыто. нужно было передвинуть столы - без проблем. кондиционер включить / выключить - сразу откликались. порадовало наличие бесплатной парковки. спасибо!"
  46. sp_text = "🇪🇸Comida abundante, buena relacin calidad-precio si pides entrante + segundo se puede cenar por unos 20 euros"
  47. ############# HELPER FUNCTIONS ################
  48. @st.cache
  49. def load_model():
  50. return joblib.load(open('./model/sentiment_detet.pkl','rb'))
  51. #model = pipeline("zero-shot-classification",
  52. # model='joeddav/xlm-roberta-large-xnli')
  53. #return model
  54. def load_metrics_data():
  55. return pd.read_csv("./data/metrics.csv")
  56. @st.cache
  57. def predict_sentiment(text, model):
  58. # Get all the candidate labels
  59. candidate_labels = ['Positive', 'Negative', 'Neutral']
  60. # Run the result
  61. result = model(text, candidate_labels, multi_class = True)
  62. del result["sequence"]
  63. result_df = pd.DataFrame(result)
  64. # Plot the result
  65. fig = px.bar(result_df, x='labels', y='scores')
  66. return fig
  67. @st.cache
  68. def show_metrics(result_df, metric):
  69. if(metric == "F1 Score"):
  70. fig = px.bar(result_df, x='languages', y='f1_scores', title="F1 Scores")
  71. elif(metric == "Accuracy"):
  72. fig = px.bar(result_df, x='languages', y='accuracy_scores', title="Accuracy Scores")
  73. return fig
  74. #st.plotly_chart(fig, use_container_width=True)
  75. # Load the model
  76. zsmlc_classifier = load_model()
  77. # Read metrics data
  78. metrics_df = load_metrics_data()
  79. # Load the logo
  80. image = Image.open('./images/dagshub_logo.png')
  81. st.image(image)
  82. st.markdown("<h1 style='text-align: center;'>"+title+"</h1>", unsafe_allow_html=True)
  83. st.markdown("<h3 style='text-align: center;'>"+app_dsc+"</h3>", unsafe_allow_html=True)
  84. st.markdown("<a href= "+dags_hub_repo+"><p style= 'text-align: center;'>"+dags_hub_repo_info+"</p></a>", unsafe_allow_html=True)
  85. st.markdown("<h3 style='text-align: center;'> 💁🏽‍♂️ Usage of the App</h3>", unsafe_allow_html=True)
  86. st.markdown(usage_options, unsafe_allow_html=True)
  87. option = st.selectbox('',('', fr_text, en_text, ger_text, rus_text, sp_text))
  88. with st.form(key='my_form'):
  89. message_container = st.empty()
  90. text_message = message_container.text_input("Your message")
  91. submit_button = st.form_submit_button('Predict')
  92. if(option):
  93. message_container.empty()
  94. option = re.sub(flag_pattern, '', option) # remove flags
  95. text_message = option
  96. if(submit_button):
  97. language_code = detect(text_message)
  98. if(language_code not in ['en', 'fr', 'de', 'ru', 'es']):
  99. st.markdown("<h4 style='color: red;'> "+lang_error+"</h4>", unsafe_allow_html=True)
  100. else:
  101. my_fig = predict_sentiment(text_message, zsmlc_classifier)
  102. st.plotly_chart(my_fig, use_container_width=True)
  103. st.markdown("<h3 style='text-align: center;'>"+metrics_info+"</h3>", unsafe_allow_html=True)
  104. st.markdown("<div style='text-align: center;'>"+side_bar_info+"</div>", unsafe_allow_html=True)
  105. metric = st.sidebar.selectbox("Choice Model Performances 📊 To Plot", ["F1 Score", "Accuracy"])
  106. metrics_fig = show_metrics(metrics_df, metric)
  107. st.plotly_chart(metrics_fig, use_container_width=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...