Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,11 +48,11 @@ def read_root():
|
|
| 48 |
def predict_sentiment(input_data: TextInput):
|
| 49 |
# Step 1: Split text into chunks
|
| 50 |
chunks, chunk_texts, chunk_token_counts = split_text_into_chunks(input_data.text)
|
| 51 |
-
|
| 52 |
# Step 2: Analyze sentiment for each chunk and calculate the total token count
|
| 53 |
results, total_token_count = analyze_sentiment_chunks(chunks, chunk_texts, chunk_token_counts)
|
| 54 |
-
|
| 55 |
-
# Step 3:
|
| 56 |
total_neutral_score = 0
|
| 57 |
total_positive_score = 0
|
| 58 |
total_negative_score = 0
|
|
@@ -64,15 +64,27 @@ def predict_sentiment(input_data: TextInput):
|
|
| 64 |
total_positive_score += sentiment['score']
|
| 65 |
elif sentiment['label'] == "Negative":
|
| 66 |
total_negative_score += sentiment['score']
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if len(chunks) == 1:
|
| 69 |
-
return {
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
return {
|
| 72 |
"total_chunks": len(results),
|
| 73 |
"total_token_count": total_token_count, # Include total token count in the response
|
| 74 |
"total_neutral_score": total_neutral_score,
|
| 75 |
"total_positive_score": total_positive_score,
|
| 76 |
"total_negative_score": total_negative_score,
|
|
|
|
|
|
|
|
|
|
| 77 |
"results": results
|
| 78 |
}
|
|
|
|
| 48 |
def predict_sentiment(input_data: TextInput):
|
| 49 |
# Step 1: Split text into chunks
|
| 50 |
chunks, chunk_texts, chunk_token_counts = split_text_into_chunks(input_data.text)
|
| 51 |
+
|
| 52 |
# Step 2: Analyze sentiment for each chunk and calculate the total token count
|
| 53 |
results, total_token_count = analyze_sentiment_chunks(chunks, chunk_texts, chunk_token_counts)
|
| 54 |
+
|
| 55 |
+
# Step 3: Calculate total and overall sentiment scores
|
| 56 |
total_neutral_score = 0
|
| 57 |
total_positive_score = 0
|
| 58 |
total_negative_score = 0
|
|
|
|
| 64 |
total_positive_score += sentiment['score']
|
| 65 |
elif sentiment['label'] == "Negative":
|
| 66 |
total_negative_score += sentiment['score']
|
| 67 |
+
|
| 68 |
+
# Calculate overall scores (average per chunk)
|
| 69 |
+
num_chunks = len(results)
|
| 70 |
+
overall_neutral_score = total_neutral_score / num_chunks if num_chunks > 0 else 0
|
| 71 |
+
overall_positive_score = total_positive_score / num_chunks if num_chunks > 0 else 0
|
| 72 |
+
overall_negative_score = total_negative_score / num_chunks if num_chunks > 0 else 0
|
| 73 |
+
|
| 74 |
+
# Step 4: Return the aggregated sentiment analysis results
|
| 75 |
if len(chunks) == 1:
|
| 76 |
+
return {
|
| 77 |
+
"results": results
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
return {
|
| 81 |
"total_chunks": len(results),
|
| 82 |
"total_token_count": total_token_count, # Include total token count in the response
|
| 83 |
"total_neutral_score": total_neutral_score,
|
| 84 |
"total_positive_score": total_positive_score,
|
| 85 |
"total_negative_score": total_negative_score,
|
| 86 |
+
"overall_neutral_score": overall_neutral_score,
|
| 87 |
+
"overall_positive_score": overall_positive_score,
|
| 88 |
+
"overall_negative_score": overall_negative_score,
|
| 89 |
"results": results
|
| 90 |
}
|