File size: 6,443 Bytes
ea290b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **How to use HugChat - the unofficial Hugging Chat API**"
      ],
      "metadata": {
        "id": "R4FiZs77vKXr"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Install prerequisite libraries**"
      ],
      "metadata": {
        "id": "6jmfeB1PvL8_"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "g9bTbqH96XS2",
        "outputId": "452331b1-e565-4228-f241-9e80cfb956f6"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting hugchat==0.1.0\n",
            "  Downloading hugchat-0.1.0-py3-none-any.whl (24 kB)\n",
            "Collecting python-dotenv\n",
            "  Downloading python_dotenv-1.0.0-py3-none-any.whl (19 kB)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from hugchat==0.1.0) (2.27.1)\n",
            "Collecting requests-toolbelt (from hugchat==0.1.0)\n",
            "  Downloading requests_toolbelt-1.0.0-py2.py3-none-any.whl (54 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.5/54.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (1.26.16)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (2023.7.22)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (3.4)\n",
            "Installing collected packages: python-dotenv, requests-toolbelt, hugchat\n",
            "Successfully installed hugchat-0.1.0 python-dotenv-1.0.0 requests-toolbelt-1.0.0\n"
          ]
        }
      ],
      "source": [
        "! pip install hugchat==0.1.0 python-dotenv"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Load Hugging Face credentials**"
      ],
      "metadata": {
        "id": "tXioNOYMv1ti"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from dotenv import dotenv_values\n",
        "\n",
        "secrets = dotenv_values('hf.env')"
      ],
      "metadata": {
        "id": "tdJjllXGueGX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "hf_email = secrets['EMAIL']\n",
        "hf_pass = secrets['PASS']"
      ],
      "metadata": {
        "id": "tNHBfLF788f2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **LLM response generation**"
      ],
      "metadata": {
        "id": "_yc18ezBzML6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from hugchat import hugchat\n",
        "from hugchat.login import Login"
      ],
      "metadata": {
        "id": "H21niuMc8xcv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Function for generating LLM response\n",
        "def generate_response(prompt_input, email, passwd):\n",
        "    # Hugging Face Login\n",
        "    sign = Login(email, passwd)\n",
        "    cookies = sign.login()\n",
        "    # Create ChatBot\n",
        "    chatbot = hugchat.ChatBot(cookies=cookies.get_dict())\n",
        "    return chatbot.chat(prompt_input)"
      ],
      "metadata": {
        "id": "4k9iUzyWzwSh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "prompt = \"What is Streamlit?\"\n",
        "response = generate_response(prompt, hf_email, hf_pass)"
      ],
      "metadata": {
        "id": "TD0YIqY1zQmK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response"
      ],
      "metadata": {
        "id": "2Om92sQ4z3e2",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 125
        },
        "outputId": "89e9b29a-3a54-4e61-dd13-b75395fa82b6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Streamlit is a lightweight Python library for creating interactive data analysis tools such as visualizations and reports. It makes use of modern web technologies like React, Redux, and WebSockets to provide fast, responsive UI components that can be used to build custom user interfaces. At its core, Streamlit provides a set of high-level abstractions built on top of NumPy and Pandas that make it easy to create beautiful charts, tables, maps, and other types of output. It works seamlessly with popular libraries like Altair, Bokeh, Matplotlib, and Seaborn, allowing you to combine their unique strengths into powerful analytical tools. Overall, Streamlit simplifies the process of building data science applications by providing a unified interface that integrates development environments with deployment tools, making it possible to iterate quickly and easily share results.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "aIWbxGM-1LDh"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}