CoolFace
Apppublic

aar2dee2/chatty_vader

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.ipynb139 linesDownload Raw Back to root
1{2  "cells": [3    {4      "cell_type": "markdown",5      "metadata": {},6      "source": ["# Import required libraries"]7    },8    {9      "cell_type": "code",10      "execution_count": null,11      "metadata": {},12      "outputs": [],13      "source": [14        "import requests\n",15        "import json\n",16        "from gtts import gTTS\n",17        "import os\n",18        "from pydub import AudioSegment"19      ]20    },21    {22      "cell_type": "markdown",23      "metadata": {},24      "source": ["# 1. Function to take in audio output and return text output"]25    },26    {27      "cell_type": "code",28      "execution_count": null,29      "metadata": {},30      "outputs": [],31      "source": [32        "def transcribe_audio_to_text(file_path):\n",33        "    # Replace with your implementation for transcribing audio to text\n",34        "    pass"35      ]36    },37    {38      "cell_type": "markdown",39      "metadata": {},40      "source": [41        "# 2. Function to call the ChatGPT API with a text and system prompt and return the response"42      ]43    },44    {45      "cell_type": "code",46      "execution_count": null,47      "metadata": {},48      "outputs": [],49      "source": [50        "def call_chatgpt_api(prompt, system_prompt):\n",51        "    # Replace with your OpenAI API Key\n",52        "    api_key = 'your-api-key'\n",53        "\n",54        "    headers = {\n",55        "        'Content-Type': 'application/json',\n",56        "        'Authorization': f'Bearer {api_key}',\n",57        "    }\n",58        "\n",59        "    data = json.dumps({\n",60        "        'model': 'text-davinci-002',\n",61        "        'prompt': f'{system_prompt} {prompt}',\n",62        "        'max_tokens': 150,\n",63        "        'n': 1,\n",64        "        'stop': None,\n",65        "        'temperature': 0.5,\n",66        "    })\n",67        "\n",68        "    response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions', headers=headers, data=data)\n",69        "    response_text = response.json()['choices'][0]['text'].strip()\n",70        "\n",71        "    return response_text"72      ]73    },74    {75      "cell_type": "markdown",76      "metadata": {},77      "source": [78        "# 3. Function to convert text to speech using a suitable library and add intonation for Yoda's voice"79      ]80    },81    {82      "cell_type": "code",83      "execution_count": null,84      "metadata": {},85      "outputs": [],86      "source": [87        "def text_to_speech_yoda(text, output_file):\n",88        "    tts = gTTS(text, lang='en')\n",89        "    tts.save(output_file)\n",90        "\n",91        "    # Add intonation for Yoda voice (you may need to customize this for better results)\n",92        "    audio = AudioSegment.from_file(output_file, format=\"mp3\")\n",93        "    audio = audio.speedup(playback_speed=1.2)\n",94        "    audio.export(output_file, format=\"mp3\")"95      ]96    },97    {98      "cell_type": "markdown",99      "metadata": {},100      "source": [101        "# 4. Wrapper function that calls all of these functions in order"102      ]103    },104    {105      "cell_type": "code",106      "execution_count": null,107      "metadata": {},108      "outputs": [],109      "source": [110        "def process_audio(input_audio_file, output_audio_file, system_prompt):\n",111        "    transcribed_text = transcribe_audio_to_text(input_audio_file)\n",112        "    chatgpt_response = call_chatgpt_api(transcribed_text, system_prompt)\n",113        "    text_to_speech_yoda(chatgpt_response, output_audio_file)"114      ]115    }116  ],117  "metadata": {118    "kernelspec": {119      "display_name": "Python 3",120      "language": "python",121      "name": "python3"122    },123    "language_info": {124      "codemirror_mode": {125        "name": "ipython",126        "version": 3127      },128      "file_extension": ".py",129      "mimetype": "text/x-python",130      "name": "python",131      "nbconvert_exporter": "python",132      "pygments_lexer": "ipython3",133      "version": "3.8.5"134    }135  },136  "nbformat": 4,137  "nbformat_minor": 4138}139