Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -47,7 +47,6 @@ emotions_target = pd.Categorical(df['emotion']).codes
|
|
47 |
emotion_classes = pd.Categorical(df['emotion']).categories
|
48 |
|
49 |
# Memory-efficient Neural Network with PyTorch
|
50 |
-
|
51 |
class MemoryEfficientNN(nn.Module):
|
52 |
def __init__(self, input_size, hidden_size, num_classes):
|
53 |
super(MemoryEfficientNN, self).__init__()
|
@@ -65,21 +64,19 @@ class MemoryEfficientNN(nn.Module):
|
|
65 |
return self.layers(x.long())
|
66 |
|
67 |
# Memory-efficient dataset
|
68 |
-
class
|
69 |
-
def __init__(self,
|
70 |
-
|
71 |
-
self.
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
def forward(self, x):
|
82 |
-
return self.layers(x.long())
|
83 |
X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
|
84 |
input_size = X_train.shape[1]
|
85 |
hidden_size = 64
|
@@ -133,14 +130,8 @@ emotions = {
|
|
133 |
'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
|
134 |
'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
|
135 |
'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
|
136 |
-
'envy': {'percentage': 10, 'motivation': 'jealous', 'intensity': 0}
|
137 |
-
|
138 |
-
total_percentage = 200
|
139 |
-
default_percentage = total_percentage / len(emotions)
|
140 |
-
for emotion in emotions:
|
141 |
-
emotions[emotion]['percentage'] = default_percentage
|
142 |
-
|
143 |
-
emotion_history_file = 'emotion_history.json'
|
144 |
|
145 |
def load_historical_data(file_path=emotion_history_file):
|
146 |
if os.path.exists(file_path):
|
@@ -272,7 +263,6 @@ def get_sentiment(text):
|
|
272 |
result = sentiment_pipeline(text)[0]
|
273 |
return f"Sentiment: {result['label']}, Score: {result['score']:.4f}"
|
274 |
|
275 |
-
|
276 |
def process_input(text):
|
277 |
try:
|
278 |
normalized_text = normalize_context(text)
|
@@ -304,6 +294,7 @@ def process_input(text):
|
|
304 |
error_message = f"An error occurred: {str(e)}"
|
305 |
print(error_message) # Logging the error
|
306 |
return error_message, error_message, error_message, error_message
|
|
|
307 |
iface = gr.Interface(
|
308 |
fn=process_input,
|
309 |
inputs="text",
|
@@ -314,4 +305,6 @@ iface = gr.Interface(
|
|
314 |
gr.Textbox(label="GPT Generated Text")
|
315 |
],
|
316 |
live=True
|
317 |
-
)
|
|
|
|
|
|
47 |
emotion_classes = pd.Categorical(df['emotion']).categories
|
48 |
|
49 |
# Memory-efficient Neural Network with PyTorch
|
|
|
50 |
class MemoryEfficientNN(nn.Module):
|
51 |
def __init__(self, input_size, hidden_size, num_classes):
|
52 |
super(MemoryEfficientNN, self).__init__()
|
|
|
64 |
return self.layers(x.long())
|
65 |
|
66 |
# Memory-efficient dataset
|
67 |
+
class MemoryEfficientDataset(IterableDataset):
|
68 |
+
def __init__(self, X, y, batch_size):
|
69 |
+
self.X = X
|
70 |
+
self.y = torch.LongTensor(y) # Convert labels to long tensors
|
71 |
+
self.batch_size = batch_size
|
72 |
+
|
73 |
+
def __iter__(self):
|
74 |
+
for i in range(0, len(self.y), self.batch_size):
|
75 |
+
X_batch = self.X[i:i+self.batch_size].toarray()
|
76 |
+
y_batch = self.y[i:i+self.batch_size]
|
77 |
+
yield torch.FloatTensor(X_batch), y_batch
|
78 |
+
|
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]
|
82 |
hidden_size = 64
|
|
|
130 |
'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
|
131 |
'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
|
132 |
'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
|
133 |
+
'envy': {'percentage': 10, 'motivation': 'jealous', 'intensity': 0},
|
134 |
+
emotion_history_file = 'emotion_history.json'
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
def load_historical_data(file_path=emotion_history_file):
|
137 |
if os.path.exists(file_path):
|
|
|
263 |
result = sentiment_pipeline(text)[0]
|
264 |
return f"Sentiment: {result['label']}, Score: {result['score']:.4f}"
|
265 |
|
|
|
266 |
def process_input(text):
|
267 |
try:
|
268 |
normalized_text = normalize_context(text)
|
|
|
294 |
error_message = f"An error occurred: {str(e)}"
|
295 |
print(error_message) # Logging the error
|
296 |
return error_message, error_message, error_message, error_message
|
297 |
+
|
298 |
iface = gr.Interface(
|
299 |
fn=process_input,
|
300 |
inputs="text",
|
|
|
305 |
gr.Textbox(label="GPT Generated Text")
|
306 |
],
|
307 |
live=True
|
308 |
+
)
|
309 |
+
|
310 |
+
iface.launch(share=True)
|