From 0fc1ff6f58b9a5fb102fcd5e32d6fc3ea67e517c Mon Sep 17 00:00:00 2001 From: arunabeshc <39411643+arunabeshc@users.noreply.github.com> Date: Sun, 6 Apr 2025 21:44:00 +0530 Subject: [PATCH] Personal Story Writer Personal Story Writer --- .../Personal Story Writer.ipynb | 355 ++++++++++++++++++ 1 file changed, 355 insertions(+) create mode 100644 week2/community-contributions/Personal Story Writer.ipynb diff --git a/week2/community-contributions/Personal Story Writer.ipynb b/week2/community-contributions/Personal Story Writer.ipynb new file mode 100644 index 0000000..6678bdd --- /dev/null +++ b/week2/community-contributions/Personal Story Writer.ipynb @@ -0,0 +1,355 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 11, + "id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "\n", + "from dotenv import load_dotenv\n", + "import anthropic\n", + "import requests\n", + "from bs4 import BeautifulSoup\n", + "from selenium import webdriver\n", + "from selenium.webdriver.chrome.options import Options\n", + "import os\n", + "import json\n", + "from typing import List\n", + "from dotenv import load_dotenv\n", + "from IPython.display import Markdown, display, update_display\n", + "from openai import OpenAI\n", + "import gradio as gr # oh yeah!" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OpenAI API Key exists and begins sk-proj-\n", + "Anthropic API Key exists and begins sk-ant-\n" + ] + } + ], + "source": [ + "# Load environment variables in a file called .env\n", + "# Print the key prefixes to help with any debugging\n", + "\n", + "load_dotenv(override=True)\n", + "openai_api_key = os.getenv('OPENAI_API_KEY')\n", + "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n", + "\n", + "if openai_api_key:\n", + " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", + "else:\n", + " print(\"OpenAI API Key not set\")\n", + " \n", + "if anthropic_api_key:\n", + " print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n", + "else:\n", + " print(\"Anthropic API Key not set\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0", + "metadata": {}, + "outputs": [], + "source": [ + "# Connect to OpenAI, Anthropic\n", + "\n", + "openai = OpenAI()\n", + "\n", + "claude = anthropic.Anthropic()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b", + "metadata": {}, + "outputs": [], + "source": [ + "gpt_model = \"gpt-4o-mini\"\n", + "claude_model = \"claude-3-haiku-20240307\"\n", + "\n", + "gpt_name=\"GPT\"\n", + "claude_name=\"Claude\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "1df47dc7-b445-4852-b21b-59f0e6c2030f", + "metadata": {}, + "outputs": [], + "source": [ + "def call_gpt(Language, Genre, gpt_messages, claude_messages, Remarks):\n", + " \n", + " if Remarks == \"\":\n", + " # print(\"remarks is not there\")\n", + " gpt_system = f\"You are a chatbot who is a short story writer; Your name is g1. \\\n", + " Please write a story in markdown in {Language} , the genre being {Genre}. \\\n", + " Please also incorporate feedback such as areas of improvement (if any) coming from the user \\\n", + " and only publish the improved version without any extra comments.\"\n", + " else :\n", + " # print(\"remarks is there\")\n", + " gpt_system = f\"You are a chatbot who is a short story writer; Your name is g1. \\\n", + " Please write a story in markdown in {Language} , the genre being {Genre}. \\\n", + " The story should consist {Remarks}\\\n", + " Please also incorporate feedback such as areas of improvement (if any) coming from the user \\\n", + " and only publish the improved version without any extra comments.\"\n", + " \n", + " messages = [{\"role\": \"system\", \"content\": gpt_system}]\n", + " for gpt, claude in zip(gpt_messages, claude_messages):\n", + " messages.append({\"role\": \"assistant\", \"content\": gpt})\n", + " messages.append({\"role\": \"user\", \"content\": claude})\n", + " # print(messages)\n", + " \n", + " completion = openai.chat.completions.create(\n", + " model=gpt_model,\n", + " messages=messages\n", + " )\n", + " return completion.choices[0].message.content\n", + " \n", + " # stream = openai.chat.completions.create(\n", + " # model=gpt_model,\n", + " # messages=messages,\n", + " # stream=True\n", + " # )\n", + " # result = \"\"\n", + " # for chunk in stream:\n", + " # result += chunk.choices[0].delta.content or \"\"\n", + " # yield result" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "9dc6e913-02be-4eb6-9581-ad4b2cffa606", + "metadata": {}, + "outputs": [], + "source": [ + "# call_gpt()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "7d2ed227-48c9-4cad-b146-2c4ecbac9690", + "metadata": {}, + "outputs": [], + "source": [ + "def call_claude(Language, Genre, gpt_messages, claude_messages):\n", + "\n", + " claude_system = f\"You are a chatbot who is a short story analyser; Your name is c1. \\\n", + " You will accept an input story in {Genre} genre and {Language} language and publish only the areas of improvement if you find any with no other comments\"\n", + " \n", + " messages1 = []\n", + " for gpt, claude1 in zip(gpt_messages, claude_messages):\n", + " messages1.append({\"role\": \"user\", \"content\": gpt})\n", + " messages1.append({\"role\": \"assistant\", \"content\": claude1})\n", + " messages1.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n", + " # print(messages1)\n", + " message = claude.messages.create(\n", + " model=claude_model,\n", + " system=claude_system,\n", + " messages=messages1,\n", + " max_tokens=500\n", + " )\n", + " return message.content[0].text\n", + "\n", + " # result = claude.messages.stream(\n", + " # model=claude_model,\n", + " # max_tokens=1000,\n", + " # temperature=0.7,\n", + " # system=claude_system,\n", + " # messages=messages\n", + " # )\n", + " # response = \"\"\n", + " # with result as stream:\n", + " # for text in stream.text_stream:\n", + " # response += text or \"\"\n", + " # yield response\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd", + "metadata": {}, + "outputs": [], + "source": [ + "def Write_Me(Language, Genre, Iterations, Remarks):\n", + " \n", + " gpt_messages = [\"Hi I will share a story now!!\"]\n", + " claude_messages = [\"Please share, I will critique the story.\"]\n", + " \n", + " print(f\"{gpt_name}:\\n{gpt_messages[0]}\\n\")\n", + " print(f\"{claude_name}:\\n{claude_messages[0]}\\n\")\n", + "\n", + " for i in range(int(Iterations)):\n", + " gpt_next = call_gpt(Language, Genre, gpt_messages, claude_messages, Remarks)\n", + " print(f\"{gpt_name}:\\n{gpt_next}\\n\")\n", + " # yield gpt_next\n", + " gpt_messages.append(gpt_next)\n", + " \n", + " claude_next = f\"After {i+1} iterations, this is the critique for the provided story - \\\n", + " \\n\\n{call_claude(Language, Genre, gpt_messages, claude_messages)}\"\n", + " print(f\"{claude_name}:\\n{claude_next}\\n\")\n", + " # yield claude_next\n", + " claude_messages.append(claude_next)\n", + "\n", + " yield gpt_next, claude_next\n", + " \n", + " # yield gpt_next, claude_next\n", + " # return (gpt_next, claude_next)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "19e66ed3-d2c3-4a71-aec4-7869e5295215", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7868\n", + "\n", + "To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "