Redfire-1234 commited on
Commit
8a06a41
·
verified ·
1 Parent(s): a9430e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -5,12 +5,23 @@ from nltk.corpus import stopwords
5
  from nltk.stem import WordNetLemmatizer
6
  from nltk.tokenize import word_tokenize
7
  import nltk
 
8
 
9
  # Download NLTK data
10
- nltk.download('punkt_tab') # Changed from 'punkt'
11
- nltk.download('stopwords')
12
- nltk.download('wordnet')
13
- nltk.download('omw-1.4') # Added for better lemmatization
 
 
 
 
 
 
 
 
 
 
14
 
15
  stop_words = set(stopwords.words('english'))
16
  lemmatizer = WordNetLemmatizer()
@@ -23,17 +34,25 @@ def preprocess_text(text):
23
  tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
24
  return " ".join(tokens)
25
 
26
- # --- Load models from local files in Space ---
27
  @st.cache_resource
28
  def load_models():
29
- with open('rf_goboult_model.pkl', 'rb') as f:
 
 
 
 
 
 
 
30
  goboult_model = pickle.load(f)
31
- with open('tfidf_goboult.pkl', 'rb') as f:
32
  goboult_tfidf = pickle.load(f)
33
- with open('rf_flipflop_model.pkl', 'rb') as f:
34
  flipflop_model = pickle.load(f)
35
- with open('tfidf_flipflop.pkl', 'rb') as f:
36
  flipflop_tfidf = pickle.load(f)
 
37
  return goboult_model, goboult_tfidf, flipflop_model, flipflop_tfidf
38
 
39
  goboult_model, goboult_tfidf, flipflop_model, flipflop_tfidf = load_models()
 
5
  from nltk.stem import WordNetLemmatizer
6
  from nltk.tokenize import word_tokenize
7
  import nltk
8
+ from huggingface_hub import hf_hub_download
9
 
10
  # Download NLTK data
11
+ try:
12
+ nltk.data.find('tokenizers/punkt_tab')
13
+ except LookupError:
14
+ nltk.download('punkt_tab')
15
+
16
+ try:
17
+ nltk.data.find('corpora/stopwords')
18
+ except LookupError:
19
+ nltk.download('stopwords')
20
+
21
+ try:
22
+ nltk.data.find('corpora/wordnet')
23
+ except LookupError:
24
+ nltk.download('wordnet')
25
 
26
  stop_words = set(stopwords.words('english'))
27
  lemmatizer = WordNetLemmatizer()
 
34
  tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
35
  return " ".join(tokens)
36
 
37
+ # --- Load models from Hugging Face Model Repo ---
38
  @st.cache_resource
39
  def load_models():
40
+ HF_MODEL_REPO = "Redfire-1234/Sentiment-analysis"
41
+
42
+ goboult_model_file = hf_hub_download(HF_MODEL_REPO, "rf_goboult_model.pkl")
43
+ goboult_tfidf_file = hf_hub_download(HF_MODEL_REPO, "tfidf_goboult.pkl")
44
+ flipflop_model_file = hf_hub_download(HF_MODEL_REPO, "rf_flipflop_model.pkl")
45
+ flipflop_tfidf_file = hf_hub_download(HF_MODEL_REPO, "tfidf_flipflop.pkl")
46
+
47
+ with open(goboult_model_file, 'rb') as f:
48
  goboult_model = pickle.load(f)
49
+ with open(goboult_tfidf_file, 'rb') as f:
50
  goboult_tfidf = pickle.load(f)
51
+ with open(flipflop_model_file, 'rb') as f:
52
  flipflop_model = pickle.load(f)
53
+ with open(flipflop_tfidf_file, 'rb') as f:
54
  flipflop_tfidf = pickle.load(f)
55
+
56
  return goboult_model, goboult_tfidf, flipflop_model, flipflop_tfidf
57
 
58
  goboult_model, goboult_tfidf, flipflop_model, flipflop_tfidf = load_models()