santit96 commited on
Commit
1eb51e0
Β·
1 Parent(s): 0ba753c

Now if model doesnt exist it is downloaded from huggingface. Update readme for huggingface deployment

Browse files
.env.example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ MODEL_FILENAME = "some_name.h5"
2
+ MODEL_REPOSITORY_NAME = "org/repo_name"
.gitignore CHANGED
@@ -2,3 +2,4 @@ __pycache__
2
  *.csv
3
  .DS_Store
4
  *.h5
 
 
2
  *.csv
3
  .DS_Store
4
  *.h5
5
+ .env
README.md CHANGED
@@ -1,3 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
1
  # bert-sentiment-analysis
2
 
3
  Prototype that classifies text into positive or negative sentiments using a fine tuned bert model
@@ -8,7 +19,7 @@ Prototype that classifies text into positive or negative sentiments using a fine
8
 
9
  ## Usage
10
 
11
- 1. Download the [trained model](https://drive.google.com/file/d/1yI1yEsAco-U-Ma9uDrJSQV21DnF2n1vU/view?usp=sharing) and move it to the *models* directory
12
  2. Use the tool:
13
  * To use it as a **streamlit web app** run:
14
 
 
1
+ ---
2
+ title: Sentiment classificator
3
+ emoji: 🎭
4
+ colorFrom: blue
5
+ colorTo: red
6
+ sdk: streamlit
7
+ sdk_version: 1.25.0
8
+ pinned: false
9
+ app_file: sentiment_analysis.py
10
+ ---
11
+
12
  # bert-sentiment-analysis
13
 
14
  Prototype that classifies text into positive or negative sentiments using a fine tuned bert model
 
19
 
20
  ## Usage
21
 
22
+ 1. Download the [trained model](https://huggingface.co/rootstrap-org/bert-sentiment-classifier/blob/main/sentiments_bert_model.h5) and move it to the *models* directory
23
  2. Use the tool:
24
  * To use it as a **streamlit web app** run:
25
 
models/models.py CHANGED
@@ -2,19 +2,29 @@
2
  Module to load the project models
3
  """
4
  import os
5
- import tensorflow_text
6
  import tensorflow as tf
7
  import tensorflow_hub as hub
 
 
 
8
 
9
-
10
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
 
 
11
 
12
 
13
  def load_sentiments_model():
14
  """
15
  Load pretrained model
16
  """
17
- model_path = os.path.join(CURRENT_DIR, "sentiments_bert_model.h5")
 
 
 
 
 
18
  model = tf.keras.models.load_model(
19
  model_path, custom_objects={"KerasLayer": hub.KerasLayer}, compile=False
20
  )
 
2
  Module to load the project models
3
  """
4
  import os
5
+
6
  import tensorflow as tf
7
  import tensorflow_hub as hub
8
+ import tensorflow_text
9
+ from dotenv import load_dotenv
10
+ from huggingface_hub import hf_hub_download
11
 
12
+ load_dotenv()
13
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
14
+ MODEL_FILENAME = os.getenv("MODEL_FILENAME")
15
+ MODEL_REPOSITORY_NAME = os.getenv("MODEL_REPOSITORY_NAME")
16
 
17
 
18
  def load_sentiments_model():
19
  """
20
  Load pretrained model
21
  """
22
+ model_path = os.path.join(CURRENT_DIR, MODEL_FILENAME)
23
+
24
+ # If model doesnt exist download from huggingface
25
+ if not os.path.exists(model_path):
26
+ hf_hub_download(MODEL_REPOSITORY_NAME, MODEL_FILENAME, local_dir=CURRENT_DIR)
27
+
28
  model = tf.keras.models.load_model(
29
  model_path, custom_objects={"KerasLayer": hub.KerasLayer}, compile=False
30
  )
requirements.txt CHANGED
@@ -1,3 +1,5 @@
 
 
1
  streamlit
2
  tensorflow
3
  tensorflow-text
 
1
+ huggingface_hub
2
+ python-dotenv
3
  streamlit
4
  tensorflow
5
  tensorflow-text
sentiment_analysis.py CHANGED
@@ -2,6 +2,7 @@
2
  Sentiment analysis streamlit webpage
3
  """
4
  import streamlit as st
 
5
  from sentiment_classificator import classify_sentiment
6
 
7
 
@@ -9,9 +10,9 @@ def get_representative_emoji(sentiment: str) -> str:
9
  """
10
  From a sentiment return the representative emoji
11
  """
12
- if sentiment == 'positive':
13
  return "πŸ˜ƒ"
14
- elif sentiment == 'negative':
15
  return "😞"
16
  else:
17
  return "😐"
@@ -24,7 +25,7 @@ def main() -> None:
24
  st.title("Sentiment Classification")
25
 
26
  # Initialize session state variables
27
- if 'enter_pressed' not in st.session_state:
28
  st.session_state.enter_pressed = False
29
 
30
  # Input text box and button
 
2
  Sentiment analysis streamlit webpage
3
  """
4
  import streamlit as st
5
+
6
  from sentiment_classificator import classify_sentiment
7
 
8
 
 
10
  """
11
  From a sentiment return the representative emoji
12
  """
13
+ if sentiment == "positive":
14
  return "πŸ˜ƒ"
15
+ elif sentiment == "negative":
16
  return "😞"
17
  else:
18
  return "😐"
 
25
  st.title("Sentiment Classification")
26
 
27
  # Initialize session state variables
28
+ if "enter_pressed" not in st.session_state:
29
  st.session_state.enter_pressed = False
30
 
31
  # Input text box and button
sentiment_classificator.py CHANGED
@@ -2,7 +2,9 @@
2
  Module to classify text into positive or negative sentiments
3
  """
4
  import sys
 
5
  import tensorflow as tf
 
6
  from models.models import load_sentiments_model
7
 
8
  sentiments_model = load_sentiments_model()
@@ -25,8 +27,7 @@ def classify_sentiment(input_text: str) -> str:
25
 
26
  if __name__ == "__main__":
27
  if len(sys.argv) < 2:
28
- print(
29
- f"Usage: python {sys.argv[0]} <text to classify>")
30
  sys.exit(1)
31
  # Get the input string from command line argument
32
  input_text = sys.argv[1]
 
2
  Module to classify text into positive or negative sentiments
3
  """
4
  import sys
5
+
6
  import tensorflow as tf
7
+
8
  from models.models import load_sentiments_model
9
 
10
  sentiments_model = load_sentiments_model()
 
27
 
28
  if __name__ == "__main__":
29
  if len(sys.argv) < 2:
30
+ print(f"Usage: python {sys.argv[0]} <text to classify>")
 
31
  sys.exit(1)
32
  # Get the input string from command line argument
33
  input_text = sys.argv[1]