https://github.com/anshul21107/empathy-engine
Converts text into speech with emotion β adjusting tone, speed, and volume for happy, sad, or neutral expressions.
https://github.com/anshul21107/empathy-engine
fastapi python3 pyttsx3-text-to-speech streamlit textblob
Last synced: about 1 month ago
JSON representation
Converts text into speech with emotion β adjusting tone, speed, and volume for happy, sad, or neutral expressions.
- Host: GitHub
- URL: https://github.com/anshul21107/empathy-engine
- Owner: Anshul21107
- Created: 2025-09-17T18:03:58.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-09-17T18:58:52.000Z (9 months ago)
- Last Synced: 2025-10-13T07:50:28.441Z (8 months ago)
- Topics: fastapi, python3, pyttsx3-text-to-speech, streamlit, textblob
- Language: Python
- Homepage:
- Size: 224 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ποΈ Empathy Engine (Assignment)
This project is an **Emotion-Aware Text-to-Speech (TTS) system** that detects emotions from input text and generates **emotionally aligned speech**.
It combines **sentiment analysis** with **speech synthesis parameters** (rate, volume, voice) to make the output sound more human-like and empathetic.
---
## π Features
- **Emotion Detection** β Uses `TextBlob` to analyze sentiment and map text into *happy*, *sad*, or *neutral* with intensity.
- **Dynamic Speech Synthesis** β Adjusts speech **rate**, **volume**, and **voice** based on detected emotion and intensity.
- **FastAPI Backend** β Exposes a `/speak` API endpoint to generate emotional speech.
- **Streamlit Frontend** β Simple UI for users to input text and listen to emotional speech.
- **Female Voice Support** β Configured `pyttsx3` to use a female voice (instead of default male).
---
## π οΈ Project Structure
```
.
βββ app.py # FastAPI backend (API to generate speech)
βββ config.py # Emotion mapping (rate, volume adjustments per emotion)
βββ emotion_detector.py # Emotion detection using TextBlob sentiment analysis
βββ streamlit_app.py # Streamlit frontend UI
βββ tts_engine.py # Text-to-Speech generation logic
```
---
## βοΈ How It Works
1. **User Input** β Text is entered via Streamlit frontend.
2. **Emotion Detection** β `emotion_detector.py` uses sentiment polarity:
- Positive β Happy π
- Negative β Sad π’
- Neutral β Neutral π
Intensity is scaled between 0 β 1.
3. **Parameter Mapping** β In `config.py`, each emotion has base values for:
- Speech **rate** (words per minute)
- Speech **volume** (loudness)
- Variations depending on intensity
4. **Speech Synthesis** β `tts_engine.py` uses `pyttsx3` to generate speech:
- Adjusts rate & volume dynamically
- Selects a **female voice** (`voices[1].id`)
5. **API Response** β `app.py` streams back `.wav` audio to the frontend.
6. **Frontend Playback** β Streamlit displays detected emotion + plays audio.
---
## βΆοΈ Usage
Follow these steps to set up and run the project:
### 1οΈβ£ Clone the Repository
```bash
git clone https://github.com/Anshul21107/Empathy-Engine
cd Empathy-Engine
```
### 2οΈβ£Create & Activate Virtual Environment
```bash
python -m venv venv
venv\Scripts\activate
```
### 3οΈβ£ Install Dependencies
```bash
pip install -r requirements.txt
```
### 4οΈβ£Run Backend (FastAPI)
```bash
python app.py
```
### 5οΈβ£ Run Frontend (Streamlit)
```bash
streamlit run streamlit_app.py
```
### Example
- Input: `"I am so excited about this new project!"`
- Detected: Emotion = `happy`, Intensity β `0.9`
- Output: Faster, louder, female-voiced audio with happy tone.
---
## π API Endpoint
**POST** `/speak`
```json
{
"text": "Your input sentence here"
}
```
**Response**: WAV audio stream + headers:
- `X-Emotion`: Detected emotion
- `X-Intensity`: Emotion intensity
---
## Points
- Implemented **sentiment-based prosody control** (rate, volume, voice).
- Used **FastAPI for scalable backend** + **Streamlit for quick prototyping UI**.
- Showed ability to integrate **NLP + TTS + Web APIs**.
- Designed `EMOTION_MAP` for **realistic emotion simulation**.
- Extended TTS to **female voice selection** (instead of default male).
---
## πΈ Screenshots
### πΉ FastAPI Endpoint

### πΉ Streamlit UI

## π Sample Audio Output
Hereβs an example of **emotionally generated speech of this text: "I am deeply saddened by this unfortunate event. It's truly heartbreaking."**:
π§ [βΆοΈ Listen to This Example](temp_audio.wav)
---
## π¨βπ» Author
Developed by **Anshul Katiyar**