Created
December 18, 2024 16:56
-
-
Save Cloud-Dark/294fd539d18af4a64733c073c19ea44a to your computer and use it in GitHub Desktop.
AI Submission 1.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyPFzqiqjfrEaTqnmAjX+pwu", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/Cloud-Dark/294fd539d18af4a64733c073c19ea44a/ai-submission-1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "nF5JWctge4iS", | |
| "outputId": "30ff1abc-0c9a-48a3-f5cb-5a30247e1ce5" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Python 3.10.12\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!python --version" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "!pip install -r requirements.txt" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "apG5aa43fIbW", | |
| "outputId": "391db012-cd4b-46e4-d4c2-f70c3d3d478c" | |
| }, | |
| "execution_count": 2, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (from -r requirements.txt (line 1)) (4.46.3)\n", | |
| "Collecting gradio (from -r requirements.txt (line 2))\n", | |
| " Downloading gradio-5.9.1-py3-none-any.whl.metadata (16 kB)\n", | |
| "Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from -r requirements.txt (line 3)) (2.5.1+cu121)\n", | |
| "Requirement already satisfied: numpy<2.0.0 in /usr/local/lib/python3.10/dist-packages (from -r requirements.txt (line 4)) (1.26.4)\n", | |
| "Requirement already satisfied: pytest in /usr/local/lib/python3.10/dist-packages (from -r requirements.txt (line 5)) (8.3.4)\n", | |
| "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (3.16.1)\n", | |
| "Requirement already satisfied: huggingface-hub<1.0,>=0.23.2 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (0.26.5)\n", | |
| "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (24.2)\n", | |
| "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (6.0.2)\n", | |
| "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (2024.9.11)\n", | |
| "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (2.32.3)\n", | |
| "Requirement already satisfied: tokenizers<0.21,>=0.20 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (0.20.3)\n", | |
| "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (0.4.5)\n", | |
| "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers->-r requirements.txt (line 1)) (4.66.6)\n", | |
| "Collecting aiofiles<24.0,>=22.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n", | |
| "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (3.7.1)\n", | |
| "Collecting fastapi<1.0,>=0.115.2 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading fastapi-0.115.6-py3-none-any.whl.metadata (27 kB)\n", | |
| "Collecting ffmpy (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading ffmpy-0.4.0-py3-none-any.whl.metadata (2.9 kB)\n", | |
| "Collecting gradio-client==1.5.2 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading gradio_client-1.5.2-py3-none-any.whl.metadata (7.1 kB)\n", | |
| "Requirement already satisfied: httpx>=0.24.1 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (0.28.1)\n", | |
| "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (3.1.4)\n", | |
| "Collecting markupsafe~=2.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.0 kB)\n", | |
| "Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (3.10.12)\n", | |
| "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (2.2.2)\n", | |
| "Requirement already satisfied: pillow<12.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (11.0.0)\n", | |
| "Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (2.10.3)\n", | |
| "Collecting pydub (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n", | |
| "Collecting python-multipart>=0.0.18 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n", | |
| "Collecting ruff>=0.2.2 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading ruff-0.8.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n", | |
| "Collecting safehttpx<0.2.0,>=0.1.6 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n", | |
| "Collecting semantic-version~=2.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n", | |
| "Collecting starlette<1.0,>=0.40.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading starlette-0.42.0-py3-none-any.whl.metadata (6.0 kB)\n", | |
| "Collecting tomlkit<0.14.0,>=0.12.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)\n", | |
| "Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (0.15.1)\n", | |
| "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio->-r requirements.txt (line 2)) (4.12.2)\n", | |
| "Collecting uvicorn>=0.14.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading uvicorn-0.34.0-py3-none-any.whl.metadata (6.5 kB)\n", | |
| "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.5.2->gradio->-r requirements.txt (line 2)) (2024.10.0)\n", | |
| "Requirement already satisfied: websockets<15.0,>=10.0 in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.5.2->gradio->-r requirements.txt (line 2)) (14.1)\n", | |
| "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->-r requirements.txt (line 3)) (3.4.2)\n", | |
| "Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch->-r requirements.txt (line 3)) (1.13.1)\n", | |
| "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch->-r requirements.txt (line 3)) (1.3.0)\n", | |
| "Requirement already satisfied: exceptiongroup>=1.0.0rc8 in /usr/local/lib/python3.10/dist-packages (from pytest->-r requirements.txt (line 5)) (1.2.2)\n", | |
| "Requirement already satisfied: iniconfig in /usr/local/lib/python3.10/dist-packages (from pytest->-r requirements.txt (line 5)) (2.0.0)\n", | |
| "Requirement already satisfied: pluggy<2,>=1.5 in /usr/local/lib/python3.10/dist-packages (from pytest->-r requirements.txt (line 5)) (1.5.0)\n", | |
| "Requirement already satisfied: tomli>=1 in /usr/local/lib/python3.10/dist-packages (from pytest->-r requirements.txt (line 5)) (2.2.1)\n", | |
| "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio->-r requirements.txt (line 2)) (3.10)\n", | |
| "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio->-r requirements.txt (line 2)) (1.3.1)\n", | |
| "Collecting starlette<1.0,>=0.40.0 (from gradio->-r requirements.txt (line 2))\n", | |
| " Downloading starlette-0.41.3-py3-none-any.whl.metadata (6.0 kB)\n", | |
| "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio->-r requirements.txt (line 2)) (2024.8.30)\n", | |
| "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio->-r requirements.txt (line 2)) (1.0.7)\n", | |
| "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx>=0.24.1->gradio->-r requirements.txt (line 2)) (0.14.0)\n", | |
| "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio->-r requirements.txt (line 2)) (2.8.2)\n", | |
| "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio->-r requirements.txt (line 2)) (2024.2)\n", | |
| "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio->-r requirements.txt (line 2)) (2024.2)\n", | |
| "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio->-r requirements.txt (line 2)) (0.7.0)\n", | |
| "Requirement already satisfied: pydantic-core==2.27.1 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio->-r requirements.txt (line 2)) (2.27.1)\n", | |
| "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio->-r requirements.txt (line 2)) (8.1.7)\n", | |
| "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio->-r requirements.txt (line 2)) (1.5.4)\n", | |
| "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio->-r requirements.txt (line 2)) (13.9.4)\n", | |
| "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers->-r requirements.txt (line 1)) (3.4.0)\n", | |
| "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers->-r requirements.txt (line 1)) (2.2.3)\n", | |
| "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio->-r requirements.txt (line 2)) (1.17.0)\n", | |
| "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio->-r requirements.txt (line 2)) (3.0.0)\n", | |
| "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio->-r requirements.txt (line 2)) (2.18.0)\n", | |
| "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio->-r requirements.txt (line 2)) (0.1.2)\n", | |
| "Downloading gradio-5.9.1-py3-none-any.whl (57.2 MB)\n", | |
| "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.2/57.2 MB\u001b[0m \u001b[31m12.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
| "\u001b[?25hDownloading gradio_client-1.5.2-py3-none-any.whl (320 kB)\n", | |
| "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m320.4/320.4 kB\u001b[0m \u001b[31m19.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
| "\u001b[?25hDownloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n", | |
| "Downloading fastapi-0.115.6-py3-none-any.whl (94 kB)\n", | |
| "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m94.8/94.8 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
| "\u001b[?25hDownloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)\n", | |
| "Downloading python_multipart-0.0.20-py3-none-any.whl (24 kB)\n", | |
| "Downloading ruff-0.8.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB)\n", | |
| "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m11.2/11.2 MB\u001b[0m \u001b[31m89.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
| "\u001b[?25hDownloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n", | |
| "Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n", | |
| "Downloading starlette-0.41.3-py3-none-any.whl (73 kB)\n", | |
| "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.2/73.2 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
| "\u001b[?25hDownloading tomlkit-0.13.2-py3-none-any.whl (37 kB)\n", | |
| "Downloading uvicorn-0.34.0-py3-none-any.whl (62 kB)\n", | |
| "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.3/62.3 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
| "\u001b[?25hDownloading ffmpy-0.4.0-py3-none-any.whl (5.8 kB)\n", | |
| "Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", | |
| "Installing collected packages: pydub, uvicorn, tomlkit, semantic-version, ruff, python-multipart, markupsafe, ffmpy, aiofiles, starlette, safehttpx, gradio-client, fastapi, gradio\n", | |
| " Attempting uninstall: markupsafe\n", | |
| " Found existing installation: MarkupSafe 3.0.2\n", | |
| " Uninstalling MarkupSafe-3.0.2:\n", | |
| " Successfully uninstalled MarkupSafe-3.0.2\n", | |
| "Successfully installed aiofiles-23.2.1 fastapi-0.115.6 ffmpy-0.4.0 gradio-5.9.1 gradio-client-1.5.2 markupsafe-2.1.5 pydub-0.25.1 python-multipart-0.0.20 ruff-0.8.3 safehttpx-0.1.6 semantic-version-2.10.0 starlette-0.41.3 tomlkit-0.13.2 uvicorn-0.34.0\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import gradio as gr\n", | |
| "from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n", | |
| "import torch\n", | |
| "import re\n", | |
| "\n", | |
| "# Load the Hugging Face model and tokenizer\n", | |
| "model_name = \"microsoft/DialoGPT-medium\"\n", | |
| "chatbot = pipeline(\"text-generation\", model=model_name)\n" | |
| ], | |
| "metadata": { | |
| "id": "LWl5QTBLfVCo" | |
| }, | |
| "execution_count": 22, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Import necessary libraries\n", | |
| "import gradio as gr\n", | |
| "\n", | |
| "# Initialize conversation history\n", | |
| "conversation_history = []\n", | |
| "\n", | |
| "# Function to generate chatbot response\n", | |
| "def chatbot_response(prompt):\n", | |
| " global conversation_history\n", | |
| "\n", | |
| " # Combine previous conversation history with the new prompt\n", | |
| " full_prompt = \"\\n\".join(conversation_history) + f\"\\nYou: {prompt}\\nBot:\"\n", | |
| "\n", | |
| " # Generate response from the model\n", | |
| " outputs = chatbot(prompt, max_length=1000, pad_token_id=50256, do_sample=True)\n", | |
| " response = outputs[0]['generated_text'][len(prompt):].strip()\n", | |
| "\n", | |
| " # Update conversation history\n", | |
| " conversation_history.append(f\"You: {prompt}\")\n", | |
| " conversation_history.append(f\"Bot: {response}\")\n", | |
| "\n", | |
| " # Display last few interactions (limit to recent history)\n", | |
| " history = \"\\n\".join(conversation_history[-6:]) # Show last 3 interactions\n", | |
| " return history\n", | |
| "\n", | |
| "# Create a Gradio interface\n", | |
| "interface = gr.Interface(\n", | |
| " fn=chatbot_response, # Function to handle inputs and return outputs\n", | |
| " inputs=\"text\", # Input type\n", | |
| " outputs=\"text\", # Output type\n", | |
| " title=\"Chatbot with Conversation History\",\n", | |
| " description=\"A simple chatbot that displays the conversation history.\"\n", | |
| ")\n", | |
| "\n", | |
| "# Launch the interface\n", | |
| "interface.launch()\n" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 650 | |
| }, | |
| "id": "odGt7hhAhGxh", | |
| "outputId": "76c0df34-f49f-4a73-8114-c220e4f83914" | |
| }, | |
| "execution_count": 23, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Running Gradio in a Colab notebook requires sharing enabled. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n", | |
| "\n", | |
| "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", | |
| "* Running on public URL: https://1982bb469c97917671.gradio.live\n", | |
| "\n", | |
| "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/plain": [ | |
| "<IPython.core.display.HTML object>" | |
| ], | |
| "text/html": [ | |
| "<div><iframe src=\"https://1982bb469c97917671.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" | |
| ] | |
| }, | |
| "metadata": {} | |
| }, | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 23 | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment