Spaces:
Paused
Paused
File size: 13,682 Bytes
2d11cde 33bc31b 2d11cde 33bc31b 2d11cde 33bc31b 2d11cde 33bc31b 2d11cde 33bc31b 2d11cde 33bc31b 2d11cde | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | {
"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": [
"<OpenAIObject chat.completion id=chatcmpl-7TzbaXbPEpF3WRiRpj37LmLyGkvur at 0x7f18dbf1a510> 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": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}
|