Skip to content

feat: implement batch sentiment analysis and add batch endpoint#26

Open
osamaa004 wants to merge 6 commits intoruxailab:mainfrom
osamaa004:feature/issue-10-batch-processing
Open

feat: implement batch sentiment analysis and add batch endpoint#26
osamaa004 wants to merge 6 commits intoruxailab:mainfrom
osamaa004:feature/issue-10-batch-processing

Conversation

@osamaa004
Copy link
Copy Markdown
Contributor

Overview:
This PR adds support for batch processing in the sentiment analysis service. It introduces a new endpoint /sentiment/analyze/batch that accepts a list of strings, optimizing performance by reducing inference overhead.

Key Changes:

Created BatchSentimentRequest schema for list-based inputs.

Implemented batch inference logic in SentimentService.

Verified integration with the audio transcription workflow.

Testing:
Tested via Swagger UI with multiple text inputs. Received 200 OK with correct sentiment labels and confidence scores.
Closes #10

Copy link
Copy Markdown
Collaborator

@BasmaElhoseny01 BasmaElhoseny01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First you need to fix conflicts
Re Check PR to only change files required to be changed for this PR for example no need for changing comments or variable names in non related files

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did u modify in this file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my understanding of the issue, I modified bertweet_model.py to implement Tensor-based batching directly within the model's forward pass.

Handling batches at the model level (using padding=True and truncation=True) leverages PyTorch's parallelism, making inference significantly faster than looping over strings. I've also ensured the method remains backward-compatible, so it still works perfectly for single strings (as it used to) while supporting lists of strings.

Regarding the logs, I've removed the initial debug logs to keep the PR focused and clean.

Screenshot 2026-03-21 051338

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same why did u changed in this file ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified the Service Layer to ensure compatibility with the new batch processing logic in the model. Since the model now returns a list of dictionaries instead of a single result, the Service Layer needed to be Type-Aware."

I implemented two main things there:

Handling Polymorphism: I used isinstance() to check if the input is a single string or a list. This keeps the API backward compatible, meaning it still works perfectly for single requests.

Standardizing the Response by using a list comprehension to call format_response, I ensured that the output format remains consistent and clean, whether the user sends one sentence or a many.

image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I’ve already cleaned up the .gitignore and reverted those unnecessary changes

@osamaa004 osamaa004 marked this pull request as draft March 20, 2026 23:12
@osamaa004 osamaa004 force-pushed the feature/issue-10-batch-processing branch from a5278a1 to 5d0b214 Compare March 21, 2026 02:37
@osamaa004 osamaa004 marked this pull request as ready for review March 21, 2026 03:35
@osamaa004
Copy link
Copy Markdown
Contributor Author

To verify everything is working perfectly, I ran the entire repository locally using one of the existing videos from the samples/ directory.

Here are the results showing the batch inference in actions.

2026-03-21.05-32-21.mp4
2026-03-21.05-31-16.mp4

Please let me know if you have any further feedback or suggestions for additional improvements. I'm looking forward to your guidance.

@BasmaElhoseny01 BasmaElhoseny01 self-assigned this Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature : Batch Processing Support

2 participants