Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -234,27 +234,30 @@ def classify_emotion(text, classifier):
|
|
234 |
return final_emotion
|
235 |
|
236 |
def get_embedding_for_text(text, tokenizer, model):
|
237 |
-
"""Get embedding for complete text
|
238 |
-
|
239 |
-
chunks = split_text(text, max_length=512)
|
240 |
chunk_embeddings = []
|
241 |
|
242 |
for chunk in chunks:
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
|
|
|
|
|
|
|
|
|
|
256 |
|
257 |
-
# Weight each chunk based on its content
|
258 |
if chunk_embeddings:
|
259 |
weights = np.array([len(chunk.split()) for chunk in chunks])
|
260 |
weights = weights / weights.sum()
|
|
|
234 |
return final_emotion
|
235 |
|
236 |
def get_embedding_for_text(text, tokenizer, model):
|
237 |
+
"""Get embedding for complete text."""
|
238 |
+
chunks = split_text(text)
|
|
|
239 |
chunk_embeddings = []
|
240 |
|
241 |
for chunk in chunks:
|
242 |
+
try:
|
243 |
+
inputs = tokenizer(
|
244 |
+
chunk,
|
245 |
+
return_tensors="pt",
|
246 |
+
padding=True,
|
247 |
+
truncation=True,
|
248 |
+
max_length=512
|
249 |
+
)
|
250 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
251 |
+
|
252 |
+
with torch.no_grad():
|
253 |
+
outputs = model(**inputs)
|
254 |
+
|
255 |
+
embedding = outputs.last_hidden_state[:, 0, :].cpu().numpy()
|
256 |
+
chunk_embeddings.append(embedding[0])
|
257 |
+
except Exception as e:
|
258 |
+
st.warning(f"Error processing chunk: {str(e)}")
|
259 |
+
continue
|
260 |
|
|
|
261 |
if chunk_embeddings:
|
262 |
weights = np.array([len(chunk.split()) for chunk in chunks])
|
263 |
weights = weights / weights.sum()
|