Sephfox commited on
Commit
8a5748f
·
verified ·
1 Parent(s): 36ff6a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -11
app.py CHANGED
@@ -68,21 +68,14 @@ class MemoryEfficientNN(nn.Module):
68
  class MemoryEfficientDataset(IterableDataset):
69
  def __init__(self, X, y, batch_size):
70
  self.X = X
71
- self.y = y.astype(int) # Convert labels to integers
72
  self.batch_size = batch_size
73
 
74
  def __iter__(self):
75
  for i in range(0, len(self.y), self.batch_size):
76
  X_batch = self.X[i:i+self.batch_size].toarray()
77
- y_batch = self.y[i:i+self.batch_size]
78
- yield torch.FloatTensor(X_batch), torch.LongTensor(y_batch)
79
-
80
- def __iter__(self):
81
- for i in range(0, len(self.y), self.batch_size):
82
- X_batch = self.X[i:i+self.batch_size].toarray()
83
- y_batch = self.y[i:i+self.batch_size]
84
- yield torch.FloatTensor(X_batch), torch.LongTensor(y_batch)
85
-
86
  # Train Memory-Efficient Neural Network
87
  X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
88
  input_size = X_train.shape[1]
@@ -276,6 +269,7 @@ def get_sentiment(text):
276
  result = sentiment_pipeline(text)[0]
277
  return f"Sentiment: {result['label']}, Score: {result['score']:.4f}"
278
 
 
279
  def process_input(text):
280
  try:
281
  normalized_text = normalize_context(text)
@@ -306,7 +300,6 @@ def process_input(text):
306
  error_message = f"An error occurred: {str(e)}"
307
  print(error_message) # Logging the error
308
  return error_message, error_message, error_message, error_message
309
-
310
  iface = gr.Interface(
311
  fn=process_input,
312
  inputs="text",
 
68
  class MemoryEfficientDataset(IterableDataset):
69
  def __init__(self, X, y, batch_size):
70
  self.X = X
71
+ self.y = torch.LongTensor(y.astype(int)) # Convert labels to long tensors
72
  self.batch_size = batch_size
73
 
74
  def __iter__(self):
75
  for i in range(0, len(self.y), self.batch_size):
76
  X_batch = self.X[i:i+self.batch_size].toarray()
77
+ y_batch = self.y[i:i+self.batch_size].unsqueeze(1) # Add a new dimension to the labels
78
+ yield torch.FloatTensor(X_batch), y_batch
 
 
 
 
 
 
 
79
  # Train Memory-Efficient Neural Network
80
  X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
81
  input_size = X_train.shape[1]
 
269
  result = sentiment_pipeline(text)[0]
270
  return f"Sentiment: {result['label']}, Score: {result['score']:.4f}"
271
 
272
+
273
  def process_input(text):
274
  try:
275
  normalized_text = normalize_context(text)
 
300
  error_message = f"An error occurred: {str(e)}"
301
  print(error_message) # Logging the error
302
  return error_message, error_message, error_message, error_message
 
303
  iface = gr.Interface(
304
  fn=process_input,
305
  inputs="text",