Skip to content

Commit

Permalink
feat(quivr-whisper): add initial project files (#3492)
Browse files Browse the repository at this point in the history
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
  • Loading branch information
StanGirard authored Nov 20, 2024
1 parent 58946d0 commit 169587b
Show file tree
Hide file tree
Showing 12 changed files with 1,878 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/quivr-whisper/.env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUIVR_API_KEY=XXXX
QUIVR_CHAT_ID=1XXXX
QUIVR_BRAIN_ID=XXXX
QUIVR_URL=XXXX
OPENAI_API_KEY=XXXX
1 change: 1 addition & 0 deletions examples/quivr-whisper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
1 change: 1 addition & 0 deletions examples/quivr-whisper/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11.9
3 changes: 3 additions & 0 deletions examples/quivr-whisper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# quivr-whisper

Describe your project here.
65 changes: 65 additions & 0 deletions examples/quivr-whisper/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Quivr-Whisper

Quivr-Whisper is a web application that allows users to ask questions via audio input. It leverages OpenAI's Whisper model for speech transcription and synthesizes responses using OpenAI's text-to-speech capabilities. The application queries the Quivr API to get a response based on the transcribed audio input.



https://github.com/StanGirard/quivr-whisper/assets/19614572/9cc270c9-07e4-4ce1-bcff-380f195c9313



## Features

- Audio input for asking questions
- Speech transcription using OpenAI's Whisper model
- Integration with Quivr API for intelligent responses
- Speech synthesis of the response for audio playback

## Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

### Prerequisites

What things you need to install the software and how to install them:

- Python 3.6+
- pip for Python 3
- Flask
- OpenAI Python package
- Requests package

### Installing

A step by step series of examples that tell you how to get a development environment running:

1. Clone the repository to your local machine.
```bash
git clone https://github.com/stangirard/quivr-whisper.git
cd Quivr-talk
```

2. Install the required packages.
```bash
pip install flask openai requests python-dotenv
```

3. Create a `.env` file in the root directory of the project and add your API keys and other configuration variables.
```env
OPENAI_API_KEY='your_openai_api_key'
QUIVR_API_KEY='your_quivr_api_key'
QUIVR_CHAT_ID='your_quivr_chat_id'
QUIVR_BRAIN_ID='your_quivr_brain_id'
QUIVR_URL='https://api.quivr.app' # Optional, only if different from the default
```

4. Run the Flask application.
```bash
flask run
```

Your app should now be running on `http://localhost:5000`.

## Usage

To use Quivr-talk, navigate to `http://localhost:5000` in your web browser, click on "Ask a question to Quivr", and record your question. Wait for the transcription and response to be synthesized, and you will hear the response played back to you.
78 changes: 78 additions & 0 deletions examples/quivr-whisper/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from flask import Flask, render_template, request, jsonify
import openai
import base64
import os
import requests
from dotenv import load_dotenv
from tempfile import NamedTemporaryFile

app = Flask(__name__)
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")

quivr_token = os.getenv("QUIVR_API_KEY", "")
quivr_chat_id = os.getenv("QUIVR_CHAT_ID", "")
quivr_brain_id = os.getenv("QUIVR_BRAIN_ID", "")
quivr_url = (
os.getenv("QUIVR_URL", "https://api.quivr.app")
+ f"/chat/{quivr_chat_id}/question?brain_id={quivr_brain_id}"
)

headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {quivr_token}",
}


@app.route("/")
def index():
return render_template("index.html")


@app.route("/transcribe", methods=["POST"])
def transcribe_audio():
audio_file = request.files["audio_data"]
transcript = transcribe_audio_file(audio_file)
quivr_response = ask_quivr_question(transcript)
audio_base64 = synthesize_speech(quivr_response)
return jsonify({"audio_base64": audio_base64})


def transcribe_audio_file(audio_file):
with NamedTemporaryFile(suffix=".webm", delete=False) as temp_audio_file:
audio_file.save(temp_audio_file)
temp_audio_file_path = temp_audio_file.name

try:
with open(temp_audio_file_path, "rb") as f:
transcript_response = openai.audio.transcriptions.create(
model="whisper-1", file=f
)
transcript = transcript_response.text
finally:
os.unlink(temp_audio_file_path)

return transcript


def ask_quivr_question(transcript):
response = requests.post(quivr_url, headers=headers, json={"question": transcript})
if response.status_code == 200:
quivr_response = response.json().get("assistant")
return quivr_response
else:
print(f"Error from Quivr API: {response.status_code}, {response.text}")
return "Sorry, I couldn't understand that."


def synthesize_speech(text):
speech_response = openai.audio.speech.create(
model="tts-1", voice="nova", input=text
)
audio_content = speech_response.content
audio_base64 = base64.b64encode(audio_content).decode("utf-8")
return audio_base64


if __name__ == "__main__":
app.run(debug=True)
28 changes: 28 additions & 0 deletions examples/quivr-whisper/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[project]
name = "quivr-whisper"
version = "0.1.0"
description = "Add your description here"
authors = [
{ name = "Stan Girard", email = "[email protected]" }
]
dependencies = [
"flask>=3.1.0",
"openai>=1.54.5",
"quivr-core>=0.0.24",
]
readme = "README.md"
requires-python = ">= 3.11"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = []

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/quivr_whisper"]
Loading

0 comments on commit 169587b

Please sign in to comment.