{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "from dotenv import load_dotenv\n", "\n", "import openai\n", "\n", "if load_dotenv():\n", " openai.api_key = os.getenv('OPENAI_API_KEY')\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "with open(\"audio/use.mp3\", \"rb\") as file:\n", " transcript = openai.Audio.transcribe(\n", " model=\"whisper-1\", file=file, response_format=\"verbose_json\"\n", " )\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# transcript" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transcript['text']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.ceil(transcript['duration'])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.054" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cost = np.ceil(transcript['duration']) * 0.006\n", "cost" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "completion = openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\",\n", " messages=[{\"role\": \"user\", \"content\": \"Is Nigeria a great country?\"}],\n", ")\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " JSON: {\n", " \"id\": \"chatcmpl-7TzbaXbPEpF3WRiRpj37LmLyGkvur\",\n", " \"object\": \"chat.completion\",\n", " \"created\": 1687382678,\n", " \"model\": \"gpt-3.5-turbo-0301\",\n", " \"choices\": [\n", " {\n", " \"index\": 0,\n", " \"message\": {\n", " \"role\": \"assistant\",\n", " \"content\": \"As an AI language model, I do not have personal opinions. However, Nigeria has many great qualities such as rich cultural heritage, abundant natural resources, and diverse economy. However, like any country, it faces its own set of challenges such as political instability, corruption, and poverty. Ultimately, whether Nigeria is seen as a great country is subjective and depends on individual perspectives and experiences.\"\n", " },\n", " \"finish_reason\": \"stop\"\n", " }\n", " ],\n", " \"usage\": {\n", " \"prompt_tokens\": 14,\n", " \"completion_tokens\": 77,\n", " \"total_tokens\": 91\n", " }\n", "}\n" ] } ], "source": [ "from pprint import pprint\n", "\n", "pprint(completion)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('As an AI language model, I do not have personal opinions. However, Nigeria '\n", " 'has many great qualities such as rich cultural heritage, abundant natural '\n", " 'resources, and diverse economy. However, like any country, it faces its own '\n", " 'set of challenges such as political instability, corruption, and poverty. '\n", " 'Ultimately, whether Nigeria is seen as a great country is subjective and '\n", " 'depends on individual perspectives and experiences.')\n" ] } ], "source": [ "pprint(completion.choices[0].message.content)\n", "# pprint(completion['choices'][0]['message']['content'])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Veronica\n" ] } ], "source": [ "message_history = []\n", "\n", "user_input = input(\"> \")\n", "print(user_input)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "message_history.append({\"role\": \"user\", \"content\": f\"{user_input}\"})" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Veronica is a name of Latin origin that means \"true image\" or \"image of God.\" It is a popular name for girls and has been used in many different cultures and countries. The name can be shortened to various nicknames, including Roni, Vero, and Nica. Some notable people with the name Veronica include actress Veronica Lake, singer Veronica Maggio, and Saint Veronica, a woman who according to Christian tradition wiped the face of Jesus as he carried the cross.\n" ] } ], "source": [ "completion = openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\",\n", " messages=message_history,\n", ")\n", "\n", "reply_content = completion.choices[0].message.content\n", "print(reply_content)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Veronica is a name of Latin origin that means \"true image\" or \"image of '\n", " 'God.\" It is a popular name for girls and has been used in many different '\n", " 'cultures and countries. The name can be shortened to various nicknames, '\n", " 'including Roni, Vero, and Nica. Some notable people with the name Veronica '\n", " 'include actress Veronica Lake, singer Veronica Maggio, and Saint Veronica, a '\n", " 'woman who according to Christian tradition wiped the face of Jesus as he '\n", " 'carried the cross.')\n" ] } ], "source": [ "pprint(reply_content)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "message_history.append({\"role\": \"assistant\", \"content\": f\"{reply_content}\"})" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User input was: Which other similar names\n", "\n", "\n", "('Similar names to Veronica include:\\n'\n", " '\\n'\n", " '- Victoria\\n'\n", " '- Vanessa\\n'\n", " '- Valentina\\n'\n", " '- Vivian\\n'\n", " '- Verity\\n'\n", " '- Verna\\n'\n", " '- Vera\\n'\n", " '- Vincentia\\n'\n", " '- Vittoria\\n'\n", " '- Venetia')\n" ] } ], "source": [ "user_input = input(\"> \")\n", "print(f'User input was: {user_input}')\n", "print('\\n')\n", "\n", "message_history.append({\"role\": \"user\", \"content\": f\"{user_input}\"})\n", "\n", "completion = openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\",\n", " messages=message_history,\n", ")\n", "\n", "reply_content = completion.choices[0].message.content\n", "pprint(reply_content)\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User input was: Iphone or android?\n", "\n", "\n", "(\"Sorry, as an AI language model, I don't have personal preferences. It \"\n", " 'depends on your personal preference and needs. Both iPhone and Android have '\n", " 'their own advantages and disadvantages. iPhone has a more streamlined and '\n", " 'user-friendly interface, while Android offers more customization options and '\n", " 'a wider range of models and prices. Both platforms are great for content '\n", " 'creation, so it really comes down to your personal preference and budget.')\n", "User input was: Samsung or Tecno\n", "\n", "\n", "('In terms of quality and features, Samsung is generally known to be a better '\n", " 'brand compared to Tecno. Samsung offers a wide range of options for '\n", " 'different budgets and needs, from mid-range to high-end smartphones with '\n", " 'advanced cameras, processors, and other features that are great for content '\n", " 'creation. \\n'\n", " '\\n'\n", " 'While Tecno also offers affordable smartphones, they may not have as many '\n", " 'premium features as Samsung, and their cameras may not be as advanced. '\n", " 'However, Tecno can still be a good option if you are on a tight budget. '\n", " 'Ultimately, the choice between Samsung and Tecno depends on your personal '\n", " 'preferences and needs.')\n" ] } ], "source": [ "message_history = []\n", "\n", "def chat(user_input: str, system: str=None, role='user', model=\"gpt-3.5-turbo\"):\n", " if system:\n", " message_history.append({\"role\": 'system', \"content\": f\"{system}\"})\n", " message_history.append({\"role\": role, \"content\": f\"{user_input}\"})\n", "\n", " completion = openai.ChatCompletion.create(\n", " model=model,\n", " messages=message_history,\n", " )\n", "\n", " reply_content = completion.choices[0].message.content\n", " message_history.append({\"role\": \"assistant\", \"content\": f\"{reply_content}\"})\n", " return reply_content\n", "\n", "\n", "for _ in range(2):\n", " user_input = input('--> ')\n", " print(f'User input was: {user_input}')\n", " print('\\n')\n", " \n", " pprint(chat(user_input, system='You are a content creator on YouTube'))\n", " print('\\n')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/alli/miniconda3/envs/openai/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n", "/home/alli/miniconda3/envs/openai/lib/python3.11/site-packages/gradio/inputs.py:27: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " warnings.warn(\n", "/home/alli/miniconda3/envs/openai/lib/python3.11/site-packages/gradio/inputs.py:30: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " super().__init__(\n", "/home/alli/miniconda3/envs/openai/lib/python3.11/site-packages/gradio/inputs.py:30: UserWarning: `numeric` parameter is deprecated, and it has no effect\n", " super().__init__(\n", "/home/alli/miniconda3/envs/openai/lib/python3.11/site-packages/gradio/outputs.py:22: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "\n", "# Create the Gradio interface\n", "iface = gr.Interface(\n", " fn=chat,\n", " inputs=gr.inputs.Textbox(placeholder=\"Enter your message...\"),\n", " outputs=gr.outputs.Textbox()\n", ")\n", "\n", "# Start the interface\n", "iface.launch()" ] } ], "metadata": { "kernelspec": { "display_name": "openai", "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.11.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }