{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": ["# Import required libraries"] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "from gtts import gTTS\n", "import os\n", "from pydub import AudioSegment" ] }, { "cell_type": "markdown", "metadata": {}, "source": ["# 1. Function to take in audio output and return text output"] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def transcribe_audio_to_text(file_path):\n", " # Replace with your implementation for transcribing audio to text\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Function to call the ChatGPT API with a text and system prompt and return the response" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def call_chatgpt_api(prompt, system_prompt):\n", " # Replace with your OpenAI API Key\n", " api_key = 'your-api-key'\n", "\n", " headers = {\n", " 'Content-Type': 'application/json',\n", " 'Authorization': f'Bearer {api_key}',\n", " }\n", "\n", " data = json.dumps({\n", " 'model': 'text-davinci-002',\n", " 'prompt': f'{system_prompt} {prompt}',\n", " 'max_tokens': 150,\n", " 'n': 1,\n", " 'stop': None,\n", " 'temperature': 0.5,\n", " })\n", "\n", " response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions', headers=headers, data=data)\n", " response_text = response.json()['choices'][0]['text'].strip()\n", "\n", " return response_text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Function to convert text to speech using a suitable library and add intonation for Yoda's voice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def text_to_speech_yoda(text, output_file):\n", " tts = gTTS(text, lang='en')\n", " tts.save(output_file)\n", "\n", " # Add intonation for Yoda voice (you may need to customize this for better results)\n", " audio = AudioSegment.from_file(output_file, format=\"mp3\")\n", " audio = audio.speedup(playback_speed=1.2)\n", " audio.export(output_file, format=\"mp3\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Wrapper function that calls all of these functions in order" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def process_audio(input_audio_file, output_audio_file, system_prompt):\n", " transcribed_text = transcribe_audio_to_text(input_audio_file)\n", " chatgpt_response = call_chatgpt_api(transcribed_text, system_prompt)\n", " text_to_speech_yoda(chatgpt_response, output_audio_file)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }