{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":50947,"databundleVersionId":5741538,"sourceType":"competition"},{"sourceId":64148,"databundleVersionId":7669720,"sourceType":"competition"},{"sourceId":11382,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":8318}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **AI Assistant with Gemma 2B-it**","metadata":{}},{"cell_type":"markdown","source":"Introducing **Gemma** 2b-it, the pioneering AI-driven Data Science Assistant poised to revolutionize data exploration and analysis. Gemma stands as the epitome of cutting-edge technology, seamlessly integrating advanced algorithms like **ScaNN** (Scalable Nearest Neighbors) to enhance data querying and retrieval processes. 🚀\n\nWith Gemma, users delve into the depths of complex datasets effortlessly, harnessing the power of ScaNN to swiftly locate relevant information amidst vast oceans of data. ScaNN's efficient indexing and similarity search capabilities empower Gemma to provide rapid insights, ensuring that users navigate their data landscapes with unparalleled agility. 🔍\n\nFurthermore, Gemma employs a robust **RAG** (Red, Amber, Green) system to signify data quality and relevance. Leveraging machine learning and statistical techniques, Gemma assigns color-coded indicators to data points, guiding users towards trustworthy and actionable insights while flagging potential anomalies or inconsistencies. 📊\n\nIn essence, **Gemma 2b-it** emerges as a game-changer in the realm of data science, seamlessly blending advanced AI technologies with intuitive user interfaces. Whether navigating intricate datasets or conducting intricate analyses, Gemma stands as the quintessential companion for data scientists, analysts, and researchers, empowering them to unlock the full potential of their data-driven endeavors. 🌟\n","metadata":{}},{"cell_type":"markdown","source":"# **1.GEmini** :\n## **largest and most capable AI model**\n\nGEmini, your friendly AI assistant! 🌟 With **smart algorithms**, it helps you with appointments and suggestions. GEmini understands you well and gives accurate responses. It's always learning and getting better! 💡 In a world of AI, GEmini stands out, making life easier for you! 🚀","metadata":{}},{"cell_type":"markdown","source":"## **Access Gemma 2B-it Model**","metadata":{}},{"cell_type":"code","source":"from transformers import AutoModelForCausalLM, AutoTokenizer\n\n# Model name\nmodel_name = \"/kaggle/input/gemma/transformers/2b-it/2\"\n\n# Load tokenizer and model\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModelForCausalLM.from_pretrained(model_name)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T07:12:04.343014Z","iopub.execute_input":"2024-04-06T07:12:04.343397Z","iopub.status.idle":"2024-04-06T07:12:52.543468Z","shell.execute_reply.started":"2024-04-06T07:12:04.343365Z","shell.execute_reply":"2024-04-06T07:12:52.542682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example usage\ntext = \"How are you?\"\ninput_ids = tokenizer.encode(text, return_tensors=\"pt\")\noutput = model.generate(input_ids, max_length=50, num_return_sequences=1)\n\ndecoded_output = tokenizer.decode(output[0], skip_special_tokens=True)\nprint(decoded_output)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T07:13:07.907653Z","iopub.execute_input":"2024-04-06T07:13:07.908631Z","iopub.status.idle":"2024-04-06T07:13:27.588668Z","shell.execute_reply.started":"2024-04-06T07:13:07.908597Z","shell.execute_reply":"2024-04-06T07:13:27.587684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-04-06T07:26:59.258508Z","iopub.execute_input":"2024-04-06T07:26:59.258867Z","iopub.status.idle":"2024-04-06T07:26:59.263184Z","shell.execute_reply.started":"2024-04-06T07:26:59.258840Z","shell.execute_reply":"2024-04-06T07:26:59.262211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Define Assistant Functionalities:**","metadata":{}},{"cell_type":"code","source":"import torch\nimport numpy as np\n\ndef answer_question(question):\n    input_ids = tokenizer.encode(question, return_tensors=\"pt\")\n    with torch.no_grad():\n        output = model.generate(input_ids=input_ids, max_length=50, num_return_sequences=1)\n    response = tokenizer.decode(output[0], skip_special_tokens=True)\n    return response\n\ndef provide_insights(data):\n    # Placeholder insights, replace with actual analysis\n    insights = \"Placeholder insights: The data suggests a positive correlation between X and Y.\"\n    return insights\n\ndef generate_code_snippet(task):\n    # Placeholder code snippet, replace with actual code generation logic\n    code = \"Placeholder code snippet: \\nfor i in range(10):\\n    print(i)\"\n    return code\n\nquestion = \"What is linear regression?\"\nresponse = answer_question(question)\nprint(\"Response:\", response)\n\n# Generate example data\nnum_samples = 100\nnum_features = 2\ndata = np.random.randn(num_samples, num_features)\n\ninsights = provide_insights(data)\nprint(\"Insights:\", insights)\n\ntask = \"data preprocessing\"\ncode_snippet = generate_code_snippet(task)\nprint(\"Code Snippet:\", code_snippet)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T08:17:30.048112Z","iopub.execute_input":"2024-04-06T08:17:30.048748Z","iopub.status.idle":"2024-04-06T08:18:02.520543Z","shell.execute_reply.started":"2024-04-06T08:17:30.048708Z","shell.execute_reply":"2024-04-06T08:18:02.519598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Question 1: What is the difference between supervised and unsupervised learning?\nquestion1 = \"What is the difference between supervised and unsupervised learning?\"\nresponse1 = answer_question(question1)\nprint(\"Response 1:\", response1)\nprint(\"-------------------------------------------------------------------------\")\n\n# Question 2: Can you explain the concept of overfitting in machine learning?\nquestion2 = \"Can you explain the concept of overfitting in machine learning?\"\nresponse2 = answer_question(question2)\nprint(\"Response 2:\", response2)\nprint(\"-------------------------------------------------------------------------\")\n\n\n# Question 3: How does logistic regression differ from linear regression?\nquestion3 = \"How does logistic regression differ from linear regression?\"\nresponse3 = answer_question(question3)\nprint(\"Response 3:\", response3)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T08:20:07.825710Z","iopub.execute_input":"2024-04-06T08:20:07.826488Z","iopub.status.idle":"2024-04-06T08:21:36.966349Z","shell.execute_reply.started":"2024-04-06T08:20:07.826457Z","shell.execute_reply":"2024-04-06T08:21:36.965261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Implement Interaction Logic:**","metadata":{}},{"cell_type":"code","source":"from transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel_name = \"/kaggle/input/gemma/transformers/2b-it/2\"\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModelForCausalLM.from_pretrained(model_name)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T08:54:54.435867Z","iopub.execute_input":"2024-04-06T08:54:54.437005Z","iopub.status.idle":"2024-04-06T08:55:00.849046Z","shell.execute_reply.started":"2024-04-06T08:54:54.436955Z","shell.execute_reply":"2024-04-06T08:55:00.848170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Handle User Queries:**","metadata":{}},{"cell_type":"markdown","source":"Utilize the Gemma model to handle user queries related to data science. Tokenize the user input, pass it through the model, and generate responses. You may need to fine-tune the model further on specific data science tasks if needed.","metadata":{}},{"cell_type":"code","source":"def answer_question(question):\n    # Tokenize the user input\n    input_ids = tokenizer.encode(question, return_tensors=\"pt\")\n\n    # Generate a response based on the user input\n    with torch.no_grad():\n        output = model.generate(input_ids=input_ids, max_length=50, num_return_sequences=1)\n\n    # Decode the generated response\n    response = tokenizer.decode(output[0], skip_special_tokens=True)\n    return response\n\n# def main():\n#     print(\"Welcome to the Data Science Handle User Queries by AI Assistant!\")\n#     print(\"\\n\")\n\n#     while True:\n#         user_input = input(\"🤵You🤵:\")\n\n#         if user_input.lower() == \"exit\":\n#             print(\"Exiting...\")\n#             break\n\n#         response = answer_question(user_input)\n#         print(\"\\n\")\n        \n#         print(\" 🤖Assistant:🤖\", response)\n\n# if __name__ == \"__main__\":\n#     main()","metadata":{"execution":{"iopub.status.busy":"2024-04-06T09:20:56.075474Z","iopub.execute_input":"2024-04-06T09:20:56.075978Z","iopub.status.idle":"2024-04-06T09:23:09.722885Z","shell.execute_reply.started":"2024-04-06T09:20:56.075943Z","shell.execute_reply":"2024-04-06T09:23:09.721909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Error Handling**","metadata":{}},{"cell_type":"code","source":"def answer_question(question):\n    try:\n        # Tokenize the user input\n        input_ids = tokenizer.encode(question, return_tensors=\"pt\")\n\n        # Generate a response based on the user input\n        with torch.no_grad():\n            output = model.generate(input_ids=input_ids, max_length=50, num_return_sequences=1)\n\n        # Decode the generated response\n        response = tokenizer.decode(output[0], skip_special_tokens=True)\n        return response\n    except Exception as e:\n        return f\"Sorry, I encountered an error: {str(e)}\"\n\n# def main():\n#     print(\"Welcome to the Data Science AI Assistant!\")\n#     print(\"You can ask any data science-related question, and I'll do my best to answer.\")\n#     print(\"Type 'exit' to quit.\")\n#     print(\"\\n\")\n\n#     while True:\n#         try:\n#             user_input = input(\"🤵You🤵:\")\n#             print(\"\\n\")\n\n#             if user_input.lower() == \"exit\":\n#                 print(\"Exiting...\")\n#                 break\n\n#             response = answer_question(user_input)\n#             print(\" 🤖Assistant:🤖\", response)\n#             print(\"\\n\")\n#         except KeyboardInterrupt:\n#             print(\"\\nExiting...\")\n#             print(\"\\n\")\n#             break\n#         except Exception as e:\n#             print(\"An error occurred:\", str(e))\n#             print(\"\\n\")\n\n# if __name__ == \"__main__\":\n#     main()","metadata":{"execution":{"iopub.status.busy":"2024-04-06T09:25:00.215230Z","iopub.execute_input":"2024-04-06T09:25:00.215994Z","iopub.status.idle":"2024-04-06T09:28:04.268054Z","shell.execute_reply.started":"2024-04-06T09:25:00.215961Z","shell.execute_reply":"2024-04-06T09:28:04.267092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Testing and Refinement**","metadata":{}},{"cell_type":"code","source":"def answer_question(question):\n    try:\n        # Tokenize the user input\n        input_ids = tokenizer.encode(question, return_tensors=\"pt\")\n\n        # Generate a response based on the user input\n        with torch.no_grad():\n            output = model.generate(input_ids=input_ids, max_length=50, num_return_sequences=1)\n\n        # Decode the generated response\n        response = tokenizer.decode(output[0], skip_special_tokens=True)\n        return response\n    except Exception as e:\n        return f\"Sorry, I encountered an error: {str(e)}\"\n\n# def test_assistant():\n#     print(\"Testing Data Science AI Assistant...\")\n#     print(\"Enter test cases (type 'exit' to quit):\")\n#     while True:\n#         user_input = input(\"🤵You🤵:\")\n#         print(\"\\n\")\n#         if user_input.lower() == \"exit\":\n#             break\n#         response = answer_question(user_input)\n#         print(\"\\n\")\n#         print(\" 🤖Assistant:🤖\", response)\n#         print(\"\\n\")\n#     print(\"\\nTesting completed.\")\n\n# if __name__ == \"__main__\":\n#     test_assistant()","metadata":{"execution":{"iopub.status.busy":"2024-04-06T09:32:36.126465Z","iopub.execute_input":"2024-04-06T09:32:36.127217Z","iopub.status.idle":"2024-04-06T09:35:07.480185Z","shell.execute_reply.started":"2024-04-06T09:32:36.127186Z","shell.execute_reply":"2024-04-06T09:35:07.479157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}