{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<!-- Codes by HTMLcodes.ws -->\n<h1 style = \"color:Blue;font-family:newtimeroman;font-size:250%;text-align:center;border-radius:15px 50px;\">Essay Topic: Generative AI</h1>","metadata":{}},{"cell_type":"markdown","source":"# **Introduction:**\n\nGenerative AI, a branch of artificial intelligence, harnesses the power of artificial neural networks to generate new content based on existing data. This cutting-edge technology empowers machines to create diverse forms of content, including text, images, audio, and more. Generative AI models derive their creative abilities from learning patterns and structures present in the training data, enabling them to produce new samples that bear similarities to the data they were trained on. This essay explores the fascinating realm of generative AI, its foundational concepts, and its wide-ranging applications.\n\n# **The Journey of Artificial Intelligence:**\n\nArtificial intelligence (AI), a discipline within computer science, revolves around the development of intelligent systems capable of reasoning, learning, and autonomous action. At its core, AI seeks to create machines that emulate human-like thinking and behavior. Machine learning, a subfield of AI, enables computers to learn from input data and make predictions without explicit programming. This field can be categorized into supervised and unsupervised learning, depending on the availability of labeled data for training purposes.\n\n# **Deep Learning and Generative AI:**\n\nDeep learning, a subset of machine learning, empowers artificial neural networks to process complex patterns, thereby enhancing their capabilities. Deep learning is particularly well-suited for generative AI applications. In the realm of generative AI, large language models and other generative AI models employ deep learning techniques to generate fresh content based on their acquired knowledge from existing data. These models leverage transformers, composed of an encoder and decoder, to process and generate content, facilitating the creation of novel and contextually relevant samples.\n\n# **Applications of Generative AI:**\n\nGenerative AI finds versatile applications across various domains. One prominent use case is text-to-text translation, where models learn to translate text from one language to another. This capability enables seamless communication and understanding across linguistic barriers. Furthermore, generative AI facilitates text-to-image generation, allowing machines to create visual representations based on learned patterns. This application fuels creativity in areas such as artwork generation, scenic creation, and even the synthesis of lifelike human faces.\n\nBeyond text-to-text and text-to-image, generative AI extends its capabilities to text-to-video generation, text-to-3D object generation, and text-to-task actions. It empowers developers to automate code generation, perform sentiment analysis, generate captions for images, and recognize objects within visual data. With its transformative potential, generative AI drives innovation and opens new avenues for human-machine interaction.\n\n# **Google Cloud and Generative AI Studio:**\n\nLeading the way in generative AI, Google Cloud offers developers an array of tools and resources to build and deploy generative AI models. Generative AI Studio, a notable offering, empowers developers to explore and customize generative AI models seamlessly. Leveraging the power of Google Cloud, developers can prototype and experiment with new ideas rapidly. This accessibility drives innovation, enabling developers to harness the creative potential of generative AI.\n\n# **Conclusion:**\n\nGenerative AI, fueled by artificial neural networks, revolutionizes the creation of new content. By learning from existing data, generative AI models produce diverse forms of content, including text, images, audio, and more. Deep learning techniques and transformer-based architectures enable machines to generate fresh and contextually relevant samples. The applications of generative AI span various domains, from language translation and image generation to code synthesis and sentiment analysis. With Google Cloud's Generative AI Studio and other resources, developers are empowered to push the boundaries of creativity and redefine human-machine collaboration. Generative AI paves the way for a future where intelligent systems actively contribute to the creative landscape.","metadata":{}},{"cell_type":"code","source":"from IPython.display import HTML\n\nHTML('<div align=\"center\"><iframe align = \"middle\" width=\"690\" height=\"440\" src=\"https://www.youtube.com/embed/G2fqAlgmoPo?t=519\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></div>')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-06-22T05:15:06.365341Z","iopub.execute_input":"2023-06-22T05:15:06.365775Z","iopub.status.idle":"2023-06-22T05:15:06.374111Z","shell.execute_reply.started":"2023-06-22T05:15:06.365739Z","shell.execute_reply":"2023-06-22T05:15:06.372848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# References:\n\n* [Introduction to Generative AI](https://youtu.be/G2fqAlgmoPo)\n* [How AppSheet is innovating with Generative AI](https://youtu.be/9thyji4QRa8?list=RDCMUCJS9pqu9BzkAMNTmzNMNhvg&t=222)\n* [Prototyping language apps with Generative AI Studio](https://youtu.be/9_zwIyutN7o?t=254)\n* [Stable Diffusion in KerasCV](https://keras.io/guides/keras_cv/generate_images_with_stable_diffusion/)\n\n","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-22T02:05:49.668958Z","iopub.execute_input":"2023-06-22T02:05:49.669368Z","iopub.status.idle":"2023-06-22T02:06:31.167725Z","shell.execute_reply.started":"2023-06-22T02:05:49.669335Z","shell.execute_reply":"2023-06-22T02:06:31.166679Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Coding","metadata":{}},{"cell_type":"markdown","source":"# Example 1: Text-to-Image Generation\n\nGenerative AI can be used to generate images based on textual descriptions. For instance, given a textual prompt describing a \"photograph of an astronaut in a dark alley with purple lights\"and \"photograph of an astronaut riding a horse\", a generative AI model can create a corresponding image.","metadata":{}},{"cell_type":"markdown","source":"[Stable Diffusion](https://github.com/CompVis/stable-diffusion) is a powerful, open-source text-to-image generation model. While there exist multiple open-source implementations that allow you to easily create images from textual prompts, KerasCV's offers a few distinct advantages. These include [XLA compilation](https://www.tensorflow.org/xla) and [mixed precision support](https://www.tensorflow.org/guide/mixed_precision), which together achieve state-of-the-art generation speed.\n\n\nThis gives rise to the Stable Diffusion architecture. Stable Diffusion consists of three parts:\n\nA text encoder, which turns your prompt into a latent vector.\nA diffusion model, which repeatedly \"denoises\" a 64x64 latent image patch.\nA decoder, which turns the final 64x64 latent patch into a higher-resolution 512x512 image.\nFirst, your text prompt gets projected into a latent vector space by the text encoder, which is simply a pretrained, frozen language model. Then that prompt vector is concatenated to a randomly generated noise patch, which is repeatedly \"denoised\" by the diffusion model over a series of \"steps\" (the more steps you run the clearer and nicer your image will be -- the default value is 50 steps).\n\nFinally, the 64x64 latent image is sent through the decoder to properly render it in high resolution.\n\n![image](https://i.imgur.com/2uC8rYJ.png)\n\n[Image Source: Stable Diffusion in KerasCV](https://keras.io/guides/keras_cv/generate_images_with_stable_diffusion/)","metadata":{}},{"cell_type":"code","source":"%%capture\n!pip install tensorflow keras_cv --upgrade --quiet","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:23:50.691683Z","iopub.execute_input":"2023-06-22T02:23:50.692126Z","iopub.status.idle":"2023-06-22T02:24:06.726119Z","shell.execute_reply.started":"2023-06-22T02:23:50.692097Z","shell.execute_reply":"2023-06-22T02:24:06.724393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!pip install pycocotools","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:24:20.976656Z","iopub.execute_input":"2023-06-22T02:24:20.977079Z","iopub.status.idle":"2023-06-22T02:25:00.363912Z","shell.execute_reply.started":"2023-06-22T02:24:20.977038Z","shell.execute_reply":"2023-06-22T02:25:00.362400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!pip install --upgrade keras-cv","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nimport keras_cv\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:25:26.766503Z","iopub.execute_input":"2023-06-22T02:25:26.767365Z","iopub.status.idle":"2023-06-22T02:25:26.772733Z","shell.execute_reply.started":"2023-06-22T02:25:26.767328Z","shell.execute_reply":"2023-06-22T02:25:26.771625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras_cv.models.StableDiffusion(img_width=512, img_height=512)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:25:31.332820Z","iopub.execute_input":"2023-06-22T02:25:31.333231Z","iopub.status.idle":"2023-06-22T02:25:31.338974Z","shell.execute_reply.started":"2023-06-22T02:25:31.333198Z","shell.execute_reply":"2023-06-22T02:25:31.338040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = model.text_to_image(\"photograph of an astronaut riding a horse\", batch_size=3)\n\n\ndef plot_images(images):\n    plt.figure(figsize=(20, 20))\n    for i in range(len(images)):\n        ax = plt.subplot(1, len(images), i + 1)\n        plt.imshow(images[i])\n        plt.axis(\"off\")\n\n\nplot_images(images)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:38:02.416119Z","iopub.execute_input":"2023-06-22T03:38:02.417616Z","iopub.status.idle":"2023-06-22T04:30:57.253411Z","shell.execute_reply.started":"2023-06-22T03:38:02.417569Z","shell.execute_reply":"2023-06-22T04:30:57.251271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = model.text_to_image(\"photograph of an astronaut in a dark alley with purple lights\", batch_size=4)\n\n\ndef plot_images(images):\n    plt.figure(figsize=(20, 20))\n    for i in range(len(images)):\n        ax = plt.subplot(1, len(images), i + 1)\n        plt.imshow(images[i])\n        plt.axis(\"off\")\n\n\nplot_images(images)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:25:39.731707Z","iopub.execute_input":"2023-06-22T02:25:39.732377Z","iopub.status.idle":"2023-06-22T03:36:53.961378Z","shell.execute_reply.started":"2023-06-22T02:25:39.732342Z","shell.execute_reply":"2023-06-22T03:36:53.956629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example 2: Sentiment Analysis\n\nGenerative AI models can perform sentiment analysis on text data, determining the emotional tone conveyed by a given text. This analysis can be visualized using a sentiment distribution plot.\n\n* [\"Sentiment140\"](https://www.kaggle.com/kazanova/sentiment140) - This dataset consists of 1.6 million tweets labeled with sentiment (positive or negative). It can be used for training a sentiment analysis model.","metadata":{}},{"cell_type":"code","source":"import re\nimport string\nimport nltk\nimport matplotlib.pyplot as plt\nfrom nltk.corpus import stopwords\nfrom nltk.stem import WordNetLemmatizer\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.naive_bayes import MultinomialNB\nplt.rcParams['figure.figsize'] = (12,6)\nplt.style.use('fivethirtyeight')\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:13:01.840210Z","iopub.execute_input":"2023-06-22T05:13:01.840637Z","iopub.status.idle":"2023-06-22T05:13:01.848838Z","shell.execute_reply.started":"2023-06-22T05:13:01.840606Z","shell.execute_reply":"2023-06-22T05:13:01.847313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/sentiment140/training.1600000.processed.noemoticon.csv', header=None, encoding='ISO-8859-1',\n                   names=['target', 'id', 'date', 'flag', 'user', 'text'])","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:51:13.255961Z","iopub.execute_input":"2023-06-22T04:51:13.256483Z","iopub.status.idle":"2023-06-22T04:51:18.919977Z","shell.execute_reply.started":"2023-06-22T04:51:13.256444Z","shell.execute_reply":"2023-06-22T04:51:18.918681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:51:23.875304Z","iopub.execute_input":"2023-06-22T04:51:23.875826Z","iopub.status.idle":"2023-06-22T04:51:23.891791Z","shell.execute_reply.started":"2023-06-22T04:51:23.875786Z","shell.execute_reply":"2023-06-22T04:51:23.890081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data[[\"target\",\"text\"]]\ndata.sample(5)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:51:28.288912Z","iopub.execute_input":"2023-06-22T04:51:28.289393Z","iopub.status.idle":"2023-06-22T04:51:28.413572Z","shell.execute_reply.started":"2023-06-22T04:51:28.289356Z","shell.execute_reply":"2023-06-22T04:51:28.412279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.target = data['target'].replace(4,1)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:51:53.770754Z","iopub.execute_input":"2023-06-22T04:51:53.771205Z","iopub.status.idle":"2023-06-22T04:51:53.800184Z","shell.execute_reply.started":"2023-06-22T04:51:53.771170Z","shell.execute_reply":"2023-06-22T04:51:53.799155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:51:57.339746Z","iopub.execute_input":"2023-06-22T04:51:57.340922Z","iopub.status.idle":"2023-06-22T04:51:57.369817Z","shell.execute_reply.started":"2023-06-22T04:51:57.340877Z","shell.execute_reply":"2023-06-22T04:51:57.368189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing the distribution of the target variable\n\nsentiment= {0:'Negative', 1:'Positive'}\nprint(data['target'].apply(lambda x: sentiment[x]).value_counts())\ndata['target'].apply(lambda x: sentiment[x]).value_counts().plot(kind='bar')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:32:40.286638Z","iopub.execute_input":"2023-06-22T04:32:40.287114Z","iopub.status.idle":"2023-06-22T04:32:41.859620Z","shell.execute_reply.started":"2023-06-22T04:32:40.287079Z","shell.execute_reply":"2023-06-22T04:32:41.858328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from textblob import TextBlob\n\n# Function to classify sentiment\ndef get_sentiment(text):\n    blob = TextBlob(text)\n    sentiment = blob.sentiment.polarity\n    if sentiment > 0:\n        return \"Positive\"\n    elif sentiment < 0:\n        return \"Negative\"\n    else:\n        return \"Neutral\"\n\n# To add a new column with the sentiment classification\ndata[\"sentiment\"] = data[\"text\"].apply(get_sentiment)\n\n# To print the number of texts in each category\nprint(\"Sentiment Distribution:\\n\", data[\"sentiment\"].value_counts())\n\n# To print 10 text from each category\nprint(\"\\nPositive text:\")\nprint(data[data[\"sentiment\"] == \"Positive\"][\"text\"].head(10))\nprint(\"\\nNegative text:\")\nprint(data[data[\"sentiment\"] == \"Negative\"][\"text\"].head(10))\nprint(\"\\nNeutral text:\")\nprint(data[data[\"sentiment\"] == \"Neutral\"][\"text\"].head(10))\n\n# To visualize the distribution of sentiment\nsns.countplot(x=\"sentiment\", data=data)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:36:15.968935Z","iopub.execute_input":"2023-06-22T04:36:15.969597Z","iopub.status.idle":"2023-06-22T04:44:06.556371Z","shell.execute_reply.started":"2023-06-22T04:36:15.969544Z","shell.execute_reply":"2023-06-22T04:44:06.555352Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nltk.download('wordnet')\nnltk.download('stopwords')\nstop_words = set(stopwords.words('english'))\nlemmatizer = WordNetLemmatizer()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:52:05.629435Z","iopub.execute_input":"2023-06-22T04:52:05.629839Z","iopub.status.idle":"2023-06-22T04:52:05.637936Z","shell.execute_reply.started":"2023-06-22T04:52:05.629810Z","shell.execute_reply":"2023-06-22T04:52:05.637038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_tweet(text):\n    text = re.sub(r'http\\S+', '', text) # Remove URLs\n    text = re.sub(r'@[A-Za-z0-9]+', '', text) # Remove mentions\n    text = re.sub(r'#', '', text) # Remove hashtags\n    text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation\n    text = ' '.join([lemmatizer.lemmatize(word.lower()) for word in text.split() if word.lower() not in stop_words]) # Tokenize, lemmatize, and remove stop words\n    return text","metadata":{"execution":{"iopub.status.busy":"2023-06-22T04:52:09.576078Z","iopub.execute_input":"2023-06-22T04:52:09.577183Z","iopub.status.idle":"2023-06-22T04:52:09.584743Z","shell.execute_reply.started":"2023-06-22T04:52:09.577139Z","shell.execute_reply":"2023-06-22T04:52:09.583341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!unzip /usr/share/nltk_data/corpora/wordnet.zip -d /usr/share/nltk_data/corpora/","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:11:55.504095Z","iopub.execute_input":"2023-06-22T05:11:55.504596Z","iopub.status.idle":"2023-06-22T05:11:55.510856Z","shell.execute_reply.started":"2023-06-22T05:11:55.504546Z","shell.execute_reply":"2023-06-22T05:11:55.509175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['text'] = data['text'].apply(preprocess_tweet)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:08:05.490622Z","iopub.execute_input":"2023-06-22T02:08:05.492531Z","iopub.status.idle":"2023-06-22T02:09:54.285434Z","shell.execute_reply.started":"2023-06-22T02:08:05.492481Z","shell.execute_reply":"2023-06-22T02:09:54.284510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['text']","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:10:00.167962Z","iopub.execute_input":"2023-06-22T02:10:00.168395Z","iopub.status.idle":"2023-06-22T02:10:00.179241Z","shell.execute_reply.started":"2023-06-22T02:10:00.168362Z","shell.execute_reply":"2023-06-22T02:10:00.178373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(data) * 0.8)\ntrain_data = data[:train_size]\ntest_data = data[train_size:]","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:10:04.827600Z","iopub.execute_input":"2023-06-22T02:10:04.827976Z","iopub.status.idle":"2023-06-22T02:10:04.832941Z","shell.execute_reply.started":"2023-06-22T02:10:04.827948Z","shell.execute_reply":"2023-06-22T02:10:04.832152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer = TfidfVectorizer(max_features=5000)\ntrain_features = vectorizer.fit_transform(train_data['text'])\ntest_features = vectorizer.transform(test_data['text'])","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:10:10.593485Z","iopub.execute_input":"2023-06-22T02:10:10.594477Z","iopub.status.idle":"2023-06-22T02:10:35.682461Z","shell.execute_reply.started":"2023-06-22T02:10:10.594433Z","shell.execute_reply":"2023-06-22T02:10:35.681278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train a classifier\nclf = MultinomialNB()\nclf.fit(train_features, train_data['target'])","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:10:40.856289Z","iopub.execute_input":"2023-06-22T02:10:40.856697Z","iopub.status.idle":"2023-06-22T02:10:41.110432Z","shell.execute_reply.started":"2023-06-22T02:10:40.856667Z","shell.execute_reply":"2023-06-22T02:10:41.109583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Text generation\ndef generate_tweet(seed_sentence, n=10):\n    current_sentence = seed_sentence\n    perplexity = 0\n    for i in range(n):\n        vectorized_sentence = vectorizer.transform([current_sentence])\n        prediction = clf.predict(vectorized_sentence)[0]\n        if prediction == 0:\n            next_word = np.random.choice(train_data[train_data['target'] == 0]['text'])\n        else:\n            next_word = np.random.choice(train_data[train_data['target'] == 4]['text'])\n        current_sentence += ' ' + next_word\n        \n        # Calculate perplexity\n        prob = clf.predict_proba(vectorized_sentence)\n        perplexity += math.log(prob[0][prediction])\n    \n    # Calculate average perplexity\n    avg_perplexity = math.exp(-perplexity/n)\n    return current_sentence, avg_perplexity","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:10:45.341236Z","iopub.execute_input":"2023-06-22T02:10:45.341640Z","iopub.status.idle":"2023-06-22T02:10:45.351061Z","shell.execute_reply.started":"2023-06-22T02:10:45.341612Z","shell.execute_reply":"2023-06-22T02:10:45.349607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\n# Example usage\nseed_sentence = \"I am feeling\"\ngenerated_tweet, perplexity = generate_tweet(seed_sentence)\nprint(\"Generated tweet: \", generated_tweet)\nprint(\"Perplexity score: \", perplexity)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T02:10:51.131711Z","iopub.execute_input":"2023-06-22T02:10:51.132097Z","iopub.status.idle":"2023-06-22T02:10:51.527004Z","shell.execute_reply.started":"2023-06-22T02:10:51.132068Z","shell.execute_reply":"2023-06-22T02:10:51.525781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example 3: Object Recognition\n\nGenerative AI models can identify and recognize objects within images. By providing an image as input, the model can generate bounding boxes and labels for detected objects.\n\n* [\"Open Images Dataset\"](https://www.kaggle.com/c/open-images-2019-object-detection) - This dataset provides a large collection of images with annotations for various objects, making it suitable for training object recognition models. ","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\n%matplotlib inline\nfrom skimage.io import imread, imshow, imsave\nimport cv2 # opencv version 3.4.2\nfrom skimage.filters import prewitt_h,prewitt_v\nfrom skimage.color import rgb2hsv\nimport scipy.misc\nimport scipy.ndimage\nimport sklearn.metrics\nfrom sklearn.cluster import KMeans\nimport matplotlib as mpl\nfrom skimage import measure\nimport imageio\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:12:05.866155Z","iopub.execute_input":"2023-06-22T05:12:05.866657Z","iopub.status.idle":"2023-06-22T05:12:05.880186Z","shell.execute_reply.started":"2023-06-22T05:12:05.866620Z","shell.execute_reply":"2023-06-22T05:12:05.878716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import skimage\nimport skimage.io\n\ntest_img_f = '../input/open-images-2019-object-detection/test/0004fdbc5b94c7c2.jpg'\nim = skimage.io.imread(test_img_f)\nim_g = skimage.io.imread(test_img_f, as_gray=True)\n\n#skimage.io.imshow(im)\nim.dtype","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:12:13.346771Z","iopub.execute_input":"2023-06-22T05:12:13.347235Z","iopub.status.idle":"2023-06-22T05:12:13.428631Z","shell.execute_reply.started":"2023-06-22T05:12:13.347202Z","shell.execute_reply":"2023-06-22T05:12:13.427702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = imread('../input/open-images-2019-object-detection/test/0004fdbc5b94c7c2.jpg', as_gray=True)\nimshow(image)\nplt.ylabel('Height {}'.format(image.shape[0]))\nplt.xlabel('Width {}'.format(image.shape[1]))","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:13:17.062416Z","iopub.execute_input":"2023-06-22T05:13:17.062859Z","iopub.status.idle":"2023-06-22T05:13:17.642512Z","shell.execute_reply.started":"2023-06-22T05:13:17.062824Z","shell.execute_reply":"2023-06-22T05:13:17.641248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = imread('../input/open-images-2019-object-detection/test/0004fdbc5b94c7c2.jpg')\nprint('Type of the image : ' , type(image))\n\nprint('Shape of the image : {}'.format(image.shape))\n\nprint('Image Hight {}'.format(image.shape[0]))\n\nprint('Image Width {}'.format(image.shape[1]))\n\nprint('Dimension of Image {}'.format(image.ndim))","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:13:27.983937Z","iopub.execute_input":"2023-06-22T05:13:27.984399Z","iopub.status.idle":"2023-06-22T05:13:28.002258Z","shell.execute_reply.started":"2023-06-22T05:13:27.984365Z","shell.execute_reply":"2023-06-22T05:13:28.001028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = imread('../input/open-images-2019-object-detection/test/0004fdbc5b94c7c2.jpg')\nprint('Image size {}'.format(image.size))\n\nprint('Maximum RGB value in this image {}'.format(image.max()))\n\nprint('Minimum RGB value in this image {}'.format(image.min()))","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:13:31.956348Z","iopub.execute_input":"2023-06-22T05:13:31.956775Z","iopub.status.idle":"2023-06-22T05:13:31.972858Z","shell.execute_reply.started":"2023-06-22T05:13:31.956739Z","shell.execute_reply":"2023-06-22T05:13:31.971610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RGB to HSV(Hue, Saturation, Value)\ninp_image = imread(\"../input/open-images-2019-object-detection/test/0004fdbc5b94c7c2.jpg\")\nhsv_img = rgb2hsv(inp_image)\nplt.ylabel('Height {}'.format(image.shape[0]))\nplt.xlabel('Width {}'.format(image.shape[1]))\nimshow(hsv_img)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:13:37.371905Z","iopub.execute_input":"2023-06-22T05:13:37.372965Z","iopub.status.idle":"2023-06-22T05:13:38.239103Z","shell.execute_reply.started":"2023-06-22T05:13:37.372925Z","shell.execute_reply":"2023-06-22T05:13:38.237923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grayscale = imread('../input/open-images-2019-object-detection/test/0004fdbc5b94c7c2.jpg')\ncounts, vals = np.histogram(grayscale, bins=range(2 ** 8))\nplt.plot(range(0, (2 ** 8) - 1), counts)\nplt.title('Grayscale image histogram')\nplt.xlabel('Pixel intensity')\nplt.ylabel('Count')","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:13:44.833905Z","iopub.execute_input":"2023-06-22T05:13:44.834325Z","iopub.status.idle":"2023-06-22T05:13:45.235169Z","shell.execute_reply.started":"2023-06-22T05:13:44.834294Z","shell.execute_reply":"2023-06-22T05:13:45.233940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv(\"/kaggle/input/2023-kaggle-ai-report/sample_submission.csv\")\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:14:27.697562Z","iopub.execute_input":"2023-06-22T05:14:27.698067Z","iopub.status.idle":"2023-06-22T05:14:27.721196Z","shell.execute_reply.started":"2023-06-22T05:14:27.698029Z","shell.execute_reply":"2023-06-22T05:14:27.720341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.loc[0]['value']='Generative AI'\nsubmission.loc[1]['value']='https://www.kaggle.com/code/jocelyndumlao/ai-report-generative-ai/'\nsubmission.loc[2]['value']=''\nsubmission.loc[3]['value']=''\nsubmission.loc[4]['value']=''\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:14:32.857078Z","iopub.execute_input":"2023-06-22T05:14:32.857781Z","iopub.status.idle":"2023-06-22T05:14:32.870832Z","shell.execute_reply.started":"2023-06-22T05:14:32.857745Z","shell.execute_reply":"2023-06-22T05:14:32.869650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T05:14:41.331323Z","iopub.execute_input":"2023-06-22T05:14:41.331762Z","iopub.status.idle":"2023-06-22T05:14:41.356698Z","shell.execute_reply.started":"2023-06-22T05:14:41.331730Z","shell.execute_reply":"2023-06-22T05:14:41.355687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\"> 📌,\"Hey there! If you found my notebook helpful and decide to fork it, I kindly request you to consider giving it an upvote. Your support encourages me to continue creating valuable content and helps others discover this resource as well. Together, we can contribute to fostering a community of knowledge sharing and empowering each other. Thank you for your consideration, and happy coding!\"😃</div>","metadata":{}}]}