Skip to content

Instantly share code, notes, and snippets.

@hugobowne
Created February 25, 2024 05:18
Show Gist options
  • Save hugobowne/ac5ded9a553ba918ea660e1a36e76708 to your computer and use it in GitHub Desktop.
Save hugobowne/ac5ded9a553ba918ea660e1a36e76708 to your computer and use it in GitHub Desktop.
story-time-genai
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "90e72c18",
"metadata": {},
"outputs": [],
"source": [
"# Import necessary libraries\n",
"from IPython.display import display, Image\n",
"import openai\n",
"\n",
"# Initialize OpenAI API (replace \"your_api_key\" with your actual OpenAI API key)\n",
"openai.api_key = 'your_api_key'\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5b08562b",
"metadata": {},
"outputs": [],
"source": [
"def generate_text(prompt):\n",
" # Updated to use the chat/completions endpoint with gpt-3.5-turbo\n",
" response = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ]\n",
" )\n",
" return response.choices[0].message['content'].strip()\n",
"\n",
"\n",
"def generate_and_display_image_dalle3(prompt):\n",
" try:\n",
" # Generate an image using DALL-E 3\n",
" response = openai.Image.create(\n",
" model=\"dall-e-3\", # Use the correct model name for DALL-E 3\n",
" prompt=prompt,\n",
" n=1, # Number of images to generate\n",
" size=\"1024x1024\" # Specify desired image size\n",
" )\n",
" \n",
" # Extract the URL of the generated image\n",
" # Note: The structure to extract the URL depends on OpenAI's response format, which may change\n",
" image_url = response['data'][0]['url']\n",
" \n",
" # Display the image in the notebook\n",
" display(Image(url=image_url))\n",
" except Exception as e:\n",
" print(f\"An error occurred: {e}\")\n",
"\n",
"def chatbot_conversation():\n",
" print(\"Hello! I'm your creative writing assistant.\")\n",
" theme = input(\"Tell me a theme or an idea to start with: \")\n",
" text_content = generate_text(f\"Write a short story about {theme}.\")\n",
" print(f\"Here's a story based on {theme}: {text_content}\")\n",
" \n",
" img_prompt = input(\"Would you like an image to go with your story? Type 'yes' or 'no': \")\n",
" if img_prompt.lower() == 'yes':\n",
" generate_and_display_image_dalle3(theme) # Here, you might use the theme or a specific aspect of the generated text\n",
" else:\n",
" print(\"Alright, if you need another story or an image, just ask!\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f9b3be0c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello! I'm your creative writing assistant.\n",
"Tell me a theme or an idea to start with: a slow evening ramble through the woods in fall, joyful, somewhat philosophical\n",
"Here's a story based on a slow evening ramble through the woods in fall, joyful, somewhat philosophical: As the sun dipped below the horizon, casting a warm golden glow over the forest, Sarah decided to take a slow evening ramble through the woods. The air was crisp with the familiar scent of autumn leaves, and the sounds of the forest enveloped her in a comforting embrace.\n",
"\n",
"With each step she took, the crunch of fallen leaves beneath her boots echoed through the trees, creating a rhythmic melody that seemed to match the gentle beating of her heart. The fading light filtered through the canopy above, casting intricate patterns on the forest floor, like a beautiful tapestry woven by nature itself.\n",
"\n",
"Sarah couldn't help but feel a sense of joy and wonder as she wandered through the woods, marveling at the beauty that surrounded her. The trees seemed to whisper ancient secrets, their branches swaying in the gentle breeze as if in conversation with the wind. Birds sang their evening songs, their melodious tunes adding to the symphony of nature.\n",
"\n",
"Lost in her thoughts, Sarah found herself reflecting on the fleeting nature of time. How quickly the seasons passed, how the leaves changed from vibrant greens to fiery reds and oranges before falling gracefully to the ground. It was a reminder that life was a series of cycles, each as beautiful and transient as the last.\n",
"\n",
"As she walked, Sarah felt a deep sense of connection to the world around her, a feeling of being part of something larger and infinitely mysterious. The woods seemed to hold infinite possibilities, a reminder that there was always more to discover and explore, both within ourselves and in the world beyond.\n",
"\n",
"With a contented sigh, Sarah continued her leisurely stroll through the woods, embracing the moments of peace and tranquility that surrounded her. In that moment, she felt a profound sense of gratitude for the simple joys of life, for the beauty of the natural world, and for the gift of being able to experience it all. And as the evening faded into night, she knew that she would carry that feeling of joy and wonder with her, long after her ramble through the woods had come to an end.\n",
"Would you like an image to go with your story? Type 'yes' or 'no': yes\n"
]
},
{
"data": {
"text/html": [
"<img src=\"https://oaidalleapiprodscus.blob.core.windows.net/private/org-Weh34Dwv2ZZlVm8I4OxyLdJ8/user-MRXkNyMtsas8hLIrYOY6glir/img-24wxwGW8WXRrZQgrHMPM3fK2.png?st=2024-02-25T04%3A12%3A35Z&se=2024-02-25T06%3A12%3A35Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-02-25T00%3A53%3A35Z&ske=2024-02-26T00%3A53%3A35Z&sks=b&skv=2021-08-06&sig=ubHgHtZ1F3urGSRCgodWP27wRHeN3NLqx%2BCTvilbMwk%3D\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Start the conversation\n",
"chatbot_conversation()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment