Spaces:
Sleeping
Sleeping
Yuvvan Talreja commited on
Commit ·
cf104cb
1
Parent(s): e46560e
Fixes
Browse files- Dockerfile +10 -13
- requirements.txt +1 -0
- topic_segmenter.py +5 -65
Dockerfile
CHANGED
|
@@ -10,28 +10,25 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 10 |
&& apt-get clean \
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
| 13 |
-
# Create cache directories with proper permissions
|
| 14 |
-
RUN mkdir -p /tmp/transformers_cache /tmp/hf_home /tmp/xdg_cache /tmp/nltk_data \
|
| 15 |
-
&& chmod 777 /tmp/transformers_cache /tmp/hf_home /tmp/xdg_cache /tmp/nltk_data
|
| 16 |
-
|
| 17 |
# Copy requirements first for better caching
|
| 18 |
COPY requirements.txt .
|
| 19 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
# Install additional dependencies directly
|
| 22 |
RUN pip install --no-cache-dir sentence-transformers>=2.2.2
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
ENV HF_HOME="/tmp/hf_home"
|
| 27 |
-
ENV XDG_CACHE_HOME="/tmp/xdg_cache"
|
| 28 |
-
ENV NLTK_DATA="/tmp/nltk_data"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
RUN python -
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
RUN
|
|
|
|
| 35 |
|
| 36 |
# Copy the rest of the application
|
| 37 |
COPY . .
|
|
|
|
| 10 |
&& apt-get clean \
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Copy requirements first for better caching
|
| 14 |
COPY requirements.txt .
|
| 15 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
|
| 17 |
+
# Install TensorFlow (CPU version to save space)
|
| 18 |
+
RUN pip install --no-cache-dir tensorflow-cpu==2.12.0
|
| 19 |
+
|
| 20 |
# Install additional dependencies directly
|
| 21 |
RUN pip install --no-cache-dir sentence-transformers>=2.2.2
|
| 22 |
|
| 23 |
+
# Download sentence-transformers model
|
| 24 |
+
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Run NLTK downloads
|
| 27 |
+
RUN python -m nltk.downloader punkt wordnet
|
| 28 |
|
| 29 |
+
# Create cache directories to avoid permission issues
|
| 30 |
+
RUN mkdir -p /root/.cache/torch/hub
|
| 31 |
+
RUN mkdir -p /root/.cache/huggingface
|
| 32 |
|
| 33 |
# Copy the rest of the application
|
| 34 |
COPY . .
|
requirements.txt
CHANGED
|
@@ -14,3 +14,4 @@ spacy>=3.0.0
|
|
| 14 |
librosa>=0.8.0
|
| 15 |
soundfile>=0.10.0
|
| 16 |
thinc>=8.0.0
|
|
|
|
|
|
| 14 |
librosa>=0.8.0
|
| 15 |
soundfile>=0.10.0
|
| 16 |
thinc>=8.0.0
|
| 17 |
+
tensorflow-cpu>=2.12.0
|
topic_segmenter.py
CHANGED
|
@@ -1,68 +1,12 @@
|
|
| 1 |
import numpy as np
|
| 2 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
from collections import defaultdict
|
| 6 |
from sklearn.cluster import AgglomerativeClustering
|
| 7 |
-
import logging
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
# Set NLTK data path to tmp directory
|
| 11 |
-
nltk_data_dir = "/tmp/nltk_data"
|
| 12 |
-
os.makedirs(nltk_data_dir, exist_ok=True)
|
| 13 |
-
os.environ["NLTK_DATA"] = nltk_data_dir
|
| 14 |
-
|
| 15 |
-
# Ensure NLTK resources are available
|
| 16 |
-
try:
|
| 17 |
-
import nltk
|
| 18 |
-
nltk.data.path.append(nltk_data_dir)
|
| 19 |
-
|
| 20 |
-
# Download resources if needed
|
| 21 |
-
for resource in ['punkt', 'wordnet', 'omw-1.4']:
|
| 22 |
-
try:
|
| 23 |
-
nltk.download(resource, download_dir=nltk_data_dir, quiet=True)
|
| 24 |
-
except Exception as e:
|
| 25 |
-
logging.warning(f"Error downloading NLTK resource {resource}: {str(e)}")
|
| 26 |
-
|
| 27 |
-
# Now import specific NLTK components
|
| 28 |
-
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 29 |
-
from nltk.stem import WordNetLemmatizer
|
| 30 |
-
except Exception as e:
|
| 31 |
-
logging.warning(f"NLTK import/setup issue: {str(e)}")
|
| 32 |
-
# Fallback implementations if NLTK fails
|
| 33 |
-
def sent_tokenize(text):
|
| 34 |
-
# Simple fallback sentence tokenizer
|
| 35 |
-
return text.replace('\n', ' ').split('. ')
|
| 36 |
-
|
| 37 |
-
def word_tokenize(text):
|
| 38 |
-
# Simple fallback word tokenizer
|
| 39 |
-
return text.split()
|
| 40 |
-
|
| 41 |
-
class WordNetLemmatizer:
|
| 42 |
-
def lemmatize(self, word):
|
| 43 |
-
return word
|
| 44 |
-
|
| 45 |
-
# Try to import KeyBERT, but provide fallback if it fails
|
| 46 |
-
try:
|
| 47 |
-
from keybert import KeyBERT
|
| 48 |
-
except ImportError as e:
|
| 49 |
-
logging.warning(f"KeyBERT import error: {str(e)}")
|
| 50 |
-
# Define a simple fallback KeyBERT class
|
| 51 |
-
class KeyBERT:
|
| 52 |
-
def __init__(self, model=None):
|
| 53 |
-
self.model = model
|
| 54 |
-
|
| 55 |
-
def extract_keywords(self, text, **kwargs):
|
| 56 |
-
# Simple fallback keyword extraction using frequency
|
| 57 |
-
if not text:
|
| 58 |
-
return [("topic", 1.0)]
|
| 59 |
-
words = text.lower().split()
|
| 60 |
-
word_count = defaultdict(int)
|
| 61 |
-
for word in words:
|
| 62 |
-
if len(word) > 3: # Skip very short words
|
| 63 |
-
word_count[word] += 1
|
| 64 |
-
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
|
| 65 |
-
return sorted_words[:kwargs.get('top_n', 5)]
|
| 66 |
|
| 67 |
class TopicSegmenter:
|
| 68 |
def __init__(self, window_size=3, similarity_threshold=0.2, context_size=2, min_segment_size=2, topic_similarity_threshold=0.3,
|
|
@@ -80,20 +24,16 @@ class TopicSegmenter:
|
|
| 80 |
import os
|
| 81 |
os.environ["TOKENIZERS_PARALLELISM"] = "false" # Prevent parallelism warning
|
| 82 |
|
| 83 |
-
# Set cache directories
|
| 84 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
| 85 |
-
os.environ["HF_HOME"] = "/tmp/hf_home"
|
| 86 |
-
|
| 87 |
# Try using sentence-transformers directly first
|
| 88 |
try:
|
| 89 |
from sentence_transformers import SentenceTransformer
|
| 90 |
-
embedding_model = SentenceTransformer(model_name
|
| 91 |
self.keyword_extractor = KeyBERT(model=embedding_model)
|
| 92 |
except (ImportError, ValueError, RuntimeError) as e:
|
| 93 |
# Fallback to default model with lower-level settings
|
| 94 |
-
logging.warning(f"Using fallback model for KeyBERT: {str(e)}")
|
| 95 |
self.keyword_extractor = KeyBERT(model='distilbert-base-nli-mean-tokens')
|
| 96 |
except Exception as e:
|
|
|
|
| 97 |
logging.error(f"Error initializing KeyBERT model: {str(e)}")
|
| 98 |
# Last resort fallback to simple keyword extraction without a model
|
| 99 |
self.keyword_extractor = None
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
+
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 5 |
+
from nltk.stem import WordNetLemmatizer
|
| 6 |
+
from keybert import KeyBERT
|
| 7 |
import re
|
| 8 |
from collections import defaultdict
|
| 9 |
from sklearn.cluster import AgglomerativeClustering
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
class TopicSegmenter:
|
| 12 |
def __init__(self, window_size=3, similarity_threshold=0.2, context_size=2, min_segment_size=2, topic_similarity_threshold=0.3,
|
|
|
|
| 24 |
import os
|
| 25 |
os.environ["TOKENIZERS_PARALLELISM"] = "false" # Prevent parallelism warning
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Try using sentence-transformers directly first
|
| 28 |
try:
|
| 29 |
from sentence_transformers import SentenceTransformer
|
| 30 |
+
embedding_model = SentenceTransformer(model_name)
|
| 31 |
self.keyword_extractor = KeyBERT(model=embedding_model)
|
| 32 |
except (ImportError, ValueError, RuntimeError) as e:
|
| 33 |
# Fallback to default model with lower-level settings
|
|
|
|
| 34 |
self.keyword_extractor = KeyBERT(model='distilbert-base-nli-mean-tokens')
|
| 35 |
except Exception as e:
|
| 36 |
+
import logging
|
| 37 |
logging.error(f"Error initializing KeyBERT model: {str(e)}")
|
| 38 |
# Last resort fallback to simple keyword extraction without a model
|
| 39 |
self.keyword_extractor = None
|