{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7921029,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1> I ask Gemini AI to give me insight from data because i lazy to do it by myself 😴 </h1>\n\n![Gemini Logo](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTNbxW_H3oK7hJnKAwUQFGWdiCZtfvMpFN3LY0pUGZRXg&s)\n\n\n**Competition: Home Credit - Credit Risk Model Stability**\n\nI also stupid and didn't have enough experience to give any insight, just ask Generative AI. You can't use this as the main benchmark for insight. \n\n- Give me suggestions if there is something missing and needs to be added. \n- Give an upvote if you find this helpful.\n\n*Thanks*\n\nNote:\n- I can't add multimodal input such as image and video in conversation. But it can and possible to do while using `.generate_content()`. Read this [https://ai.google.dev/api/python/google/generativeai/GenerativeModel#multimodal_input](https://ai.google.dev/api/python/google/generativeai/GenerativeModel#multimodal_input)\n\nTo-do:\n- [x] Create a function that use 'prompt_message' and 'file' as input and result as output. I can't use file as input.\n- [x] Create section to explain feature in the data\n- [ ] Add all information on EDA and ask Gemini-AI to create best strategy to build ML model.\n","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom IPython.display import display, Markdown, Latex \n\nimport os\n\n# i use gemini to help me understand the data\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nGOOGLE_API_KEY = user_secrets.get_secret(\"gemini_api\")\n# Using model\nimport google.generativeai as genai\ngenai.configure(api_key=GOOGLE_API_KEY)\nmodel = genai.GenerativeModel('gemini-pro')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:21.806040Z","iopub.execute_input":"2024-04-06T12:09:21.806388Z","iopub.status.idle":"2024-04-06T12:09:22.519579Z","shell.execute_reply.started":"2024-04-06T12:09:21.806361Z","shell.execute_reply":"2024-04-06T12:09:22.518247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> If you want to copy this notebook and edit it, get your own Gemini Api here: [https://aistudio.google.com/app/apikey](https://aistudio.google.com/app/apikey)","metadata":{}},{"cell_type":"code","source":"def conversation(input_text: str, messages: list):\n    \"\"\"\n    Function for conducting conversation with the Gemini-AI model.\n\n    Args:\n        input_text (str): User input text.\n        messages (list): List of previous messages in the conversation.\n\n    Returns:\n        list: Updated list of messages including the model's response.\n    \"\"\"\n    if isinstance(input_text, str):\n        messages.append({'role':'user', 'parts': [input_text]})\n        response = model.generate_content(messages)\n        display(Markdown(response.text))\n        messages.append(response.candidates[0].content)\n        return messages\n    else:\n        print('You need to make sure parameter input is on string format!')\n        return messages","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:22.521751Z","iopub.execute_input":"2024-04-06T12:09:22.522318Z","iopub.status.idle":"2024-04-06T12:09:22.530762Z","shell.execute_reply.started":"2024-04-06T12:09:22.522287Z","shell.execute_reply":"2024-04-06T12:09:22.529295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Example how function `conversation()` works","metadata":{}},{"cell_type":"code","source":"# Initialize message\nmessages_ex = []\n\n# Try conversation with Gemini-AI!\nmessages_ex = conversation(\"Hello there!\", messages_ex)\n\n# Asking about ML\nmessages_ex = conversation(\"Explain machine learning for 5 years old kid\", messages_ex)\n\n# Try to access the last conversation if it's works!\nmessages_ex = conversation(\"Did you remember our last conversation? What is it?\", messages_ex)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T12:09:22.532168Z","iopub.execute_input":"2024-04-06T12:09:22.532506Z","iopub.status.idle":"2024-04-06T12:09:28.131358Z","shell.execute_reply.started":"2024-04-06T12:09:22.532476Z","shell.execute_reply":"2024-04-06T12:09:28.130336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Finally the function worked! ","metadata":{}},{"cell_type":"markdown","source":"# Features Explanation\n\nLet's try asking about what are the features in dataset!","metadata":{}},{"cell_type":"markdown","source":"## Features Table","metadata":{}},{"cell_type":"code","source":"feature_def = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/feature_definitions.csv')\n\n# Create headers for Markdown tables\nmarkdown_tabel = \"| \" + \" | \".join(feature_def.columns) + \" |\\n\"\nmarkdown_tabel += \"| \" + \" | \".join([\"-\" for _ in range(len(feature_def.columns))]) + \" |\\n\"\n\n# Add row values to a Markdown table\nfor i in range(len(feature_def)):\n    markdown_tabel += \"| \" + \" | \".join(str(val) for val in feature_def.iloc[i]) + \" |\\n\"\n\ndisplay(Markdown(markdown_tabel))\n","metadata":{"scrolled":true,"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:28.133876Z","iopub.execute_input":"2024-04-06T12:09:28.134152Z","iopub.status.idle":"2024-04-06T12:09:28.159749Z","shell.execute_reply.started":"2024-04-06T12:09:28.134130Z","shell.execute_reply":"2024-04-06T12:09:28.158561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Asking Gemini-AI","metadata":{}},{"cell_type":"markdown","source":"### Initialize chat","metadata":{}},{"cell_type":"code","source":"# Initialize chat about features\n\nmessage_features = []\n\ninput_prompt = f'''\nHello, i want to ask you something. I've try to understand a such big dataset from Kaggle Competition. But it has a lot of features.\nLuckily, the host give us a description about the features. Here's the feature table:\n\n{markdown_tabel}\n\nDo you understand? Just say yes, if you do. I will ask you again later.\n'''\n\nmessage_features = conversation(input_prompt, message_features)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:28.161306Z","iopub.execute_input":"2024-04-06T12:09:28.161593Z","iopub.status.idle":"2024-04-06T12:09:32.469418Z","shell.execute_reply.started":"2024-04-06T12:09:28.161570Z","shell.execute_reply":"2024-04-06T12:09:32.468438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Ask to give story/description about feature in those data","metadata":{}},{"cell_type":"code","source":"input_prompt = f'''\nLet's say that i don't understand at all about the data and i want you to explain it based on features.\n\nCan you give me a possible story or description of dataset based on those features? \n'''\n\nmessage_features = conversation(input_prompt, message_features)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:32.470748Z","iopub.execute_input":"2024-04-06T12:09:32.471469Z","iopub.status.idle":"2024-04-06T12:09:44.012936Z","shell.execute_reply.started":"2024-04-06T12:09:32.471394Z","shell.execute_reply":"2024-04-06T12:09:44.011931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Ask about features that are possibly related and grouped under the same topic","metadata":{}},{"cell_type":"code","source":"input_prompt = f'''\nOkay thanks, now i want to ask you more question. \nHow do you think the correlation between those feature based on the description. \nCan you group the features on the same topic based on description?\n'''\n\nmessage_features = conversation(input_prompt, message_features)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:44.014256Z","iopub.execute_input":"2024-04-06T12:09:44.014523Z","iopub.status.idle":"2024-04-06T12:09:54.159512Z","shell.execute_reply.started":"2024-04-06T12:09:44.014500Z","shell.execute_reply":"2024-04-06T12:09:54.158277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Ask about feature importance based on description","metadata":{}},{"cell_type":"code","source":"input_prompt = f'''\nWhat do you think the most importance feature for modelling based on the description?\n\nGive the list 10 most importance feature based on description\n'''\n\nmessage_features = conversation(input_prompt, message_features)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-06T12:09:54.161368Z","iopub.execute_input":"2024-04-06T12:09:54.161701Z","iopub.status.idle":"2024-04-06T12:10:02.394228Z","shell.execute_reply.started":"2024-04-06T12:09:54.161677Z","shell.execute_reply":"2024-04-06T12:10:02.392851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Asking about the right strategy in building an ML model based on feature description","metadata":{}},{"cell_type":"code","source":"input_prompt = f'''\nGive me strategy on a list step by step about building right ML for the data based on those feature description.\n'''\n\nmessage_features = conversation(input_prompt, message_features)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T12:12:22.179695Z","iopub.execute_input":"2024-04-06T12:12:22.180112Z","iopub.status.idle":"2024-04-06T12:12:31.045538Z","shell.execute_reply.started":"2024-04-06T12:12:22.180081Z","shell.execute_reply":"2024-04-06T12:12:31.044104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reference:\n- Get your own Gemini API: [https://aistudio.google.com/app/apikey](https://aistudio.google.com/app/apikey)\n- Quickstart: [https://ai.google.dev/tutorials/quickstart_colab](https://ai.google.dev/tutorials/quickstart_colab)\n- Deep Documentation: [https://ai.google.dev/api/python/google/generativeai/GenerativeModel](https://ai.google.dev/api/python/google/generativeai/GenerativeModel)","metadata":{}}]}