Spaces:
Runtime error
Runtime error
Upload 38 files
Browse files- .chroma/index/id_to_uuid_83410de6-262c-4114-9a28-af63c54d424b.pkl +3 -0
- .chroma/index/index_83410de6-262c-4114-9a28-af63c54d424b.bin +3 -0
- .chroma/index/index_metadata_83410de6-262c-4114-9a28-af63c54d424b.pkl +3 -0
- .chroma/index/uuid_to_id_83410de6-262c-4114-9a28-af63c54d424b.pkl +3 -0
- .env +1 -0
- LICENSE +21 -0
- chroma.log +19 -0
- chroma_data/chroma.sqlite3 +0 -0
- conversation.txt +104 -0
- documents/embeddings.txt +73 -0
- documents/guide.txt +123 -0
- documents/stable.txt +348 -0
- gen-lang-client-0582614236-c66336e67562.json +13 -0
- key.txt +1 -0
- modules/__pycache__/document_loader.cpython-311.pyc +0 -0
- modules/__pycache__/embedding_generator.cpython-311.pyc +0 -0
- modules/__pycache__/forms.cpython-311.pyc +0 -0
- modules/__pycache__/text_generator.cpython-311.pyc +0 -0
- modules/document_loader.py +30 -0
- modules/embedding_generator.py +59 -0
- modules/forms.py +7 -0
- modules/text_generator.py +17 -0
- requirements.txt +11 -0
- routes/__pycache__/main_routes.cpython-311.pyc +0 -0
- routes/main_routes.py +189 -0
- static/css/styles.css +182 -0
- static/img/00006-2189979373-removebg-preview.png +0 -0
- static/img/PaLM2_Doc_Genie.png +0 -0
- static/img/cosign_sims.png +0 -0
- static/img/doc_view.png +0 -0
- static/img/document-genie.png +0 -0
- static/img/genie.png +0 -0
- static/img/logo.png +0 -0
- static/img/optimization_visual.png +0 -0
- static/main.js +94 -0
- templates/chat.html +112 -0
- templates/index.html +121 -0
- webui_app.py +15 -0
.chroma/index/id_to_uuid_83410de6-262c-4114-9a28-af63c54d424b.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4301dcffb6737094ff349135ed948c16f04c601c9ffc052a4b890998d34d4956
|
3 |
+
size 64
|
.chroma/index/index_83410de6-262c-4114-9a28-af63c54d424b.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c532550bf754939ad0915d1ca6a053bf75f6d241c496f1f2a81bcc8a6cbc4c36
|
3 |
+
size 4336
|
.chroma/index/index_metadata_83410de6-262c-4114-9a28-af63c54d424b.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1bb3bdbdd679c205e16e070981690bffee596097fd9a6ddaa586efbd188358b5
|
3 |
+
size 103
|
.chroma/index/uuid_to_id_83410de6-262c-4114-9a28-af63c54d424b.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d8de009ae17181908c75a1a1a39910ccd9cc4c0c68466d4fb29216e05eac4514
|
3 |
+
size 52
|
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
API_KEY = AIzaSyCMyiuFNaTTrkZU8HSIOszpZh2Z8Vq3Rdc
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Nick Williams
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
chroma.log
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[33mWARNING[0m: [05-02-2024 13:49:02] chroma_server_nofile is not supported on Windows. chroma_server_nofile will not be set.
|
2 |
+
[32mINFO[0m: [05-02-2024 13:49:02] Anonymized telemetry enabled. See https://docs.trychroma.com/telemetry for more information.
|
3 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component System
|
4 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component OpenTelemetryClient
|
5 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component SimpleAssignmentPolicy
|
6 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component SqliteDB
|
7 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component Posthog
|
8 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component LocalSegmentManager
|
9 |
+
[36mDEBUG[0m: [05-02-2024 13:49:02] Starting component SegmentAPI
|
10 |
+
[32mINFO[0m: [05-02-2024 13:49:03] Started server process [[36m11964[0m]
|
11 |
+
[32mINFO[0m: [05-02-2024 13:49:03] Waiting for application startup.
|
12 |
+
[32mINFO[0m: [05-02-2024 13:49:03] Application startup complete.
|
13 |
+
[32mINFO[0m: [05-02-2024 13:49:03] Uvicorn running on [1mhttp://localhost:8000[0m (Press CTRL+C to quit)
|
14 |
+
[32mINFO[0m: [05-02-2024 13:49:15] ::1:61827 - "GET / HTTP/1.1" 404
|
15 |
+
[32mINFO[0m: [05-02-2024 13:49:16] ::1:61827 - "GET /favicon.ico HTTP/1.1" 404
|
16 |
+
[32mINFO[0m: [05-02-2024 13:49:34] Shutting down
|
17 |
+
[32mINFO[0m: [05-02-2024 13:49:34] Waiting for application shutdown.
|
18 |
+
[32mINFO[0m: [05-02-2024 13:49:34] Application shutdown complete.
|
19 |
+
[32mINFO[0m: [05-02-2024 13:49:34] Finished server process [[36m11964[0m]
|
chroma_data/chroma.sqlite3
ADDED
Binary file (147 kB). View file
|
|
conversation.txt
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
User Query: explain lsdr in stable diffusion
|
2 |
+
Document Genie Says:
|
3 |
+
**LSDR in Stable Diffusion**
|
4 |
+
|
5 |
+
LSDR (Layer-wise Spectral Normalization for Residual Networks) is a technique that helps to stabilize the training of deep neural networks. It does this by normalizing the activations of each layer in the network, which helps to prevent the network from becoming too deep and overfitting to the training data.
|
6 |
+
|
7 |
+
Stable diffusion is a technique for training generative adversarial networks (GANs). GANs are a type of neural network that can be used to generate new data, such as images or text. Stable diffusion helps to stabilize the training of GANs by preventing them from collapsing into a mode collapse, where the generator network only produces a single output.
|
8 |
+
|
9 |
+
LSDR and stable diffusion can be combined to improve the training of both deep neural networks and GANs. By normalizing the activations of each layer in the network, LSDR helps to prevent the network from becoming too deep and overfitting to the training data. This can help to improve the performance of the network on new data. Stable diffusion helps to stabilize the training of GANs by preventing them from collapsing into a mode collapse, which can also improve the performance of the GAN on new data.
|
10 |
+
|
11 |
+
Here is a more detailed explanation of how LSDR and stable diffusion work:
|
12 |
+
|
13 |
+
* **LSDR:** LSDR works by normalizing the activations of each layer in the network. This is done by subtracting the mean activation of the layer from each activation and then dividing by the standard deviation of the activations. This helps to prevent the activations from becoming too large or too small, which can help to stabilize the training of the network.
|
14 |
+
* **Stable diffusion:** Stable diffusion works by adding noise to the input of the generator network. This noise helps to prevent the generator network from collapsing into a mode collapse, where it only produces a single output. The noise is added in a way that ensures that the generator network still produces realistic outputs.
|
15 |
+
|
16 |
+
LSDR and stable diffusion can be used together to improve the training of both deep neural networks and GANs. By normalizing the activations of each layer in the network, LSDR helps to prevent the network from becoming too deep and overfitting to the training data. This can help to improve the performance of the network on new data. Stable diffusion helps to stabilize the training of GANs by preventing them from collapsing into a mode collapse, which can also improve the performance of the GAN on new data.
|
17 |
+
|
18 |
+
Here are some of the benefits of using LSDR and stable diffusion:
|
19 |
+
|
20 |
+
* **Improved performance:** LSDR and stable diffusion can help to improve the performance of deep neural networks and GANs on new data.
|
21 |
+
* **Stabilized training:** LSDR and stable diffusion can help to stabilize the training of deep neural networks and GANs, making them less likely to overfit to the training data.
|
22 |
+
* **Reduced mode collapse:** Stable diffusion can help to reduce mode collapse in GANs, making them more likely to produce realistic outputs.
|
23 |
+
|
24 |
+
LSDR and stable diffusion are both powerful techniques that can be used to improve the training of deep neural networks and GANs. By combining these two techniques, it is possible to achieve state-of-the-art results on a variety of tasks.
|
25 |
+
User Query: explain lsdr in stable diffusion
|
26 |
+
Document Genie Says:
|
27 |
+
**LSDR in Stable Diffusion**
|
28 |
+
|
29 |
+
LSDR (Layer-wise Spectral Discriminator Regularization) is a technique used to improve the stability of diffusion models. Diffusion models are a type of generative model that can be used to create images, videos, and other types of data. They work by starting with a random noise vector and then gradually transforming it into the desired output, using a process called diffusion.
|
30 |
+
|
31 |
+
One of the challenges with diffusion models is that they can be unstable, meaning that they can produce different results for the same input. This is because the diffusion process is non-deterministic, and small changes in the input can lead to large changes in the output. LSDR helps to improve the stability of diffusion models by adding a regularizer to the loss function. This regularizer encourages the model to produce outputs that have similar spectral features, which helps to reduce the variability in the results.
|
32 |
+
|
33 |
+
LSDR has been shown to improve the stability of diffusion models on a variety of tasks, including image generation, video generation, and text-to-image generation. It is a promising technique for improving the performance of diffusion models and making them more reliable.
|
34 |
+
|
35 |
+
Here is a more detailed explanation of how LSDR works. The diffusion process starts with a random noise vector $z$. This vector is then passed through a series of layers, each of which applies a linear transformation to the vector and then adds a noise term. The output of the final layer is the generated image.
|
36 |
+
|
37 |
+
The LSDR regularizer is added to the loss function for the diffusion model. The loss function is a measure of the difference between the generated image and the target image. The LSDR regularizer encourages the model to produce outputs that have similar spectral features. This is done by calculating the spectral features of the generated image and the target image, and then adding a penalty to the loss function if the two sets of spectral features are too different.
|
38 |
+
|
39 |
+
The LSDR regularizer helps to improve the stability of the diffusion model by reducing the variability in the generated images. This is because the regularizer encourages the model to produce outputs that are similar to each other, even if the input is different. This makes the diffusion model more reliable and easier to use.
|
40 |
+
|
41 |
+
LSDR is a promising technique for improving the stability of diffusion models. It is a simple and effective technique that can be used to improve the performance of diffusion models on a variety of tasks.
|
42 |
+
User Query: explain lsdr in stable diffusion
|
43 |
+
Document Genie Says:
|
44 |
+
**LSDR in Stable Diffusion**
|
45 |
+
|
46 |
+
LSDR (layer-wise spectral decorrelation regularization) is a technique that can be used to improve the stability of diffusion models. Diffusion models are a type of generative model that are trained by iteratively applying a diffusion process to a latent representation of the data. This process starts with a high-dimensional latent representation of the data and gradually reduces the dimensionality of the representation until it reaches a low-dimensional representation that can be used to reconstruct the original data.
|
47 |
+
|
48 |
+
One of the challenges with diffusion models is that they can be unstable, meaning that small changes to the latent representation can lead to large changes in the reconstructed data. This instability can make it difficult to train diffusion models and can also lead to artifacts in the reconstructed data.
|
49 |
+
|
50 |
+
LSDR can help to improve the stability of diffusion models by decorrelating the features in the latent representation. This is done by adding a regularization term to the loss function that encourages the features in the latent representation to be uncorrelated. This helps to prevent the latent representation from becoming too entangled, which can lead to instability.
|
51 |
+
|
52 |
+
LSDR has been shown to improve the stability of diffusion models on a variety of tasks, including image generation, text generation, and video generation. It can also help to reduce artifacts in the reconstructed data.
|
53 |
+
|
54 |
+
Here is a more detailed explanation of how LSDR works.
|
55 |
+
|
56 |
+
The diffusion process is a Markov chain that starts with a high-dimensional latent representation of the data and gradually reduces the dimensionality of the representation until it reaches a low-dimensional representation that can be used to reconstruct the original data. The transition probabilities for the Markov chain are determined by a convolutional neural network (CNN).
|
57 |
+
|
58 |
+
The LSDR regularization term is added to the loss function for the CNN. This term encourages the features in the latent representation to be uncorrelated. This is done by penalizing the correlation between pairs of features in the latent representation.
|
59 |
+
|
60 |
+
The LSDR regularization term can be written as follows:
|
61 |
+
|
62 |
+
$$\mathcal{L}_{LSDR} = \sum_{i,j} \left( \frac{1}{N} \sum_{n=1}^N \langle z_i^n, z_j^n \rangle - \frac{1}{N^2} \sum_{n,m=1}^N \langle z_i^n, z_j^m \rangle \right)^2$$
|
63 |
+
|
64 |
+
where $z_i^n$ is the $i$th feature of the latent representation for the $n$th data sample, $N$ is the number of data samples, and $\langle \cdot, \cdot \rangle$ denotes the inner product.
|
65 |
+
|
66 |
+
The LSDR regularization term encourages the features in the latent representation to be uncorrelated by penalizing the correlation between pairs of features. This helps to prevent the latent representation from becoming too entangled, which can lead to instability.
|
67 |
+
|
68 |
+
LSDR has been shown to improve the stability of diffusion models on a variety of tasks, including image generation, text generation, and video generation. It can also help to reduce artifacts in the reconstructed data.
|
69 |
+
User Query: explain lsdr in stable diffusion
|
70 |
+
Document Genie Says:
|
71 |
+
**LSDR in Stable Diffusion**
|
72 |
+
|
73 |
+
LSDR (Layer-wise Spectral Decomposition Regularization) is a regularization technique that can be used to improve the stability of stable diffusion models. Stable diffusion models are a type of generative model that are trained by iteratively applying a diffusion process to a random noise vector. This process gradually transforms the noise vector into a realistic image. However, stable diffusion models can sometimes be unstable, meaning that they can produce artifacts or diverge during training. LSDR can help to improve the stability of stable diffusion models by regularizing the spectral properties of the diffusion process.
|
74 |
+
|
75 |
+
To understand how LSDR works, it is helpful to first understand the concept of spectral decomposition. The spectral decomposition of a matrix is a way of representing the matrix as a sum of orthogonal matrices. This means that the matrices in the sum are all perpendicular to each other, and they do not overlap. The spectral decomposition of a matrix can be used to understand the properties of the matrix. For example, the eigenvalues of a matrix are the values that appear in the diagonal of the spectral decomposition. The eigenvalues of a matrix can tell us about the stability of the matrix.
|
76 |
+
|
77 |
+
LSDR works by regularizing the eigenvalues of the diffusion process. This means that LSDR encourages the eigenvalues of the diffusion process to be small. This has the effect of making the diffusion process more stable, and it reduces the likelihood of artifacts or divergence during training.
|
78 |
+
|
79 |
+
LSDR has been shown to improve the stability of stable diffusion models on a variety of tasks. For example, LSDR has been shown to improve the stability of stable diffusion models for image generation, text generation, and video generation.
|
80 |
+
|
81 |
+
Here is a more detailed explanation of how LSDR works. LSDR is based on the idea that the eigenvalues of the diffusion process can be used to control the stability of the model. The eigenvalues of the diffusion process are the values that appear in the diagonal of the spectral decomposition of the diffusion process. The eigenvalues of the diffusion process can be used to control the stability of the model because they determine how quickly the diffusion process converges to a stable state.
|
82 |
+
|
83 |
+
LSDR works by regularizing the eigenvalues of the diffusion process. This means that LSDR encourages the eigenvalues of the diffusion process to be small. This has the effect of making the diffusion process more stable, and it reduces the likelihood of artifacts or divergence during training.
|
84 |
+
|
85 |
+
LSDR is a powerful technique that can be used to improve the stability of stable diffusion models. LSDR has been shown to improve the stability of stable diffusion models on a variety of tasks, including image generation, text generation, and video generation.
|
86 |
+
User Query: explain lsdr in stable diffusion
|
87 |
+
Document Genie Says:
|
88 |
+
**LSDR in Stable Diffusion**
|
89 |
+
|
90 |
+
LSDR (Layer-wise Spectral Decomposition Regularization) is a regularization technique that can be used to improve the stability of stable diffusion models. Stable diffusion models are a type of generative model that are trained by iteratively applying a diffusion process to a random noise vector. This process gradually transforms the noise vector into a realistic image. However, stable diffusion models can sometimes be unstable, which can lead to artifacts in the generated images. LSDR helps to improve the stability of stable diffusion models by regularizing the spectral properties of the diffusion process.
|
91 |
+
|
92 |
+
The spectral properties of a diffusion process are determined by the eigenvalues of the diffusion operator. The eigenvalues of the diffusion operator are related to the timescales of the diffusion process. The larger the eigenvalues, the faster the diffusion process. LSDR penalizes the eigenvalues of the diffusion operator, which helps to slow down the diffusion process and make it more stable.
|
93 |
+
|
94 |
+
LSDR has been shown to improve the stability of stable diffusion models without significantly affecting the quality of the generated images. This makes LSDR a valuable tool for improving the performance of stable diffusion models.
|
95 |
+
|
96 |
+
Here is a more detailed explanation of LSDR.
|
97 |
+
|
98 |
+
Stable diffusion models are trained by iteratively applying a diffusion process to a random noise vector. The diffusion process is defined by a convolutional neural network (CNN) that takes a noise vector as input and outputs a new noise vector. The diffusion process is applied iteratively, with the output of each iteration being used as the input to the next iteration.
|
99 |
+
|
100 |
+
The diffusion process can be represented as a matrix, called the diffusion operator. The diffusion operator is a linear operator that maps a noise vector to a new noise vector. The eigenvalues of the diffusion operator are the square roots of the eigenvalues of the Hessian of the diffusion process. The eigenvalues of the diffusion operator are related to the timescales of the diffusion process. The larger the eigenvalues, the faster the diffusion process.
|
101 |
+
|
102 |
+
LSDR penalizes the eigenvalues of the diffusion operator. This helps to slow down the diffusion process and make it more stable. LSDR is implemented by adding a regularization term to the loss function of the stable diffusion model. The regularization term is a function of the eigenvalues of the diffusion operator.
|
103 |
+
|
104 |
+
LSDR has been shown to improve the stability of stable diffusion models without significantly affecting the quality of the generated images. This makes LSDR a valuable tool for improving the performance of stable diffusion models.
|
documents/embeddings.txt
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PaLM API Quickstart - Embeddings Edition Python
|
2 |
+
|
3 |
+
|
4 |
+
In this notebook, you'll learn how to get started with the PaLM API, which gives you access to Google's latest large language models. Here, you'll learn how to use the PaLM API's embedding generation features, and see an example of what you can do with these embeddings.
|
5 |
+
|
6 |
+
Setup
|
7 |
+
Note: At this time, the PaLM API is only available in the US.
|
8 |
+
First, download and install the PaLM API Python library.
|
9 |
+
|
10 |
+
|
11 |
+
pip install -U google-generativeai
|
12 |
+
|
13 |
+
import numpy as np
|
14 |
+
import google.generativeai as palm
|
15 |
+
Grab an API Key
|
16 |
+
To get started, you'll need to create an API key.
|
17 |
+
|
18 |
+
|
19 |
+
palm.configure(api_key='PALM_KEY')
|
20 |
+
What are embeddings?
|
21 |
+
Embeddings are a technique used to represent text (like words, sentences, or entire paragraphs) as a list of floating point numbers in an array. These numbers aren't random. The key idea is that text with similar meanings will have similar embeddings. You can use the relationship between them for many important tasks.
|
22 |
+
|
23 |
+
Embedding generation
|
24 |
+
In this section, you will see how to generate embeddings for a piece of text using PaLM API's palm.generate_embeddings function. Here are a list of models that support this function.
|
25 |
+
|
26 |
+
|
27 |
+
for model in palm.list_models():
|
28 |
+
if 'embedText' in model.supported_generation_methods:
|
29 |
+
print(model.name)
|
30 |
+
|
31 |
+
models/embedding-gecko-001
|
32 |
+
Use the function palm.generate_embeddings and pass in the name of the model as well as some text. You will get a list of floating point values. Start with a query "What do squirrels eat?" and see how related two different strings are to it.
|
33 |
+
|
34 |
+
|
35 |
+
x = 'What do squirrels eat?'
|
36 |
+
|
37 |
+
close_to_x = 'nuts and acorns'
|
38 |
+
|
39 |
+
different_from_x = 'This morning I woke up in San Francisco, and took a walk to the Bay Bridge. It was a good, sunny morning with no fog.'
|
40 |
+
|
41 |
+
model = "models/embedding-gecko-001"
|
42 |
+
|
43 |
+
# Create an embedding
|
44 |
+
embedding_x = palm.generate_embeddings(model=model, text=x)
|
45 |
+
embedding_close_to_x = palm.generate_embeddings(model=model, text=close_to_x)
|
46 |
+
embedding_different_from_x = palm.generate_embeddings(model=model, text=different_from_x)
|
47 |
+
|
48 |
+
print(embedding_x)
|
49 |
+
|
50 |
+
{'embedding': [-0.025894878, -0.02103396, 0.003574992, 0.00822288, 0.03276648, -0.10068223, -0.037702546, 0.01079403, 0.0001406235, -0.029412385, 0.01919925, 0.0048481044, 0.070619866, -0.013349887, 0.028378602, -0.018658886, -0.038629908, 0.056883123, 0.06332366, 0.039849922, -0.085393265, -0.016251814, -0.025535949, 0.0049480307, 0.048581485, -0.11295683, 0.033869933, 0.015498774, -0.07306243, 0.000857902, -0.022031788, -0.005298939, -0.08311722, -0.027091762, 0.042790364, 0.023175264, 0.011238991, -0.02432924, -0.0044626957, 0.05167071, 0.023430848, 0.027325166, -0.01492389, -0.018770715, -0.003783692, 0.040971957, -0.044652887, 0.033220302, -0.05659744, -0.055191413, -0.0023204528, -0.043687623, 0.030044463, -0.015966717, -0.04318426, 0.015735775, -0.038352676, -0.005009736, -0.03289721, 0.016246213, -0.005696393, -0.0010992853, -0.02768714, -0.03534994, -0.045970507, 0.05784305, -0.026696421, -0.013302212, 0.007055761, -0.05885901, 0.03330113, 0.04399591, 0.020755561, 0.0028288597, 0.037333105, 0.0103595415, -0.01942964, 0.033088185, 0.009558319, -0.06524442, -0.07101354, -0.053975347, -0.003952934, -0.11641813, -0.039488368, -0.0033782825, -0.017735159, 0.03198736, 0.014555729, 0.050724585, -0.07849815, -0.0070436746, 0.017992217, -0.003975652, -0.0039650565, 0.08063971, -0.011685766, -0.018323965, 0.007763516, 0.012011537, 0.028457757, -0.099603206, 0.0328822, 0.0063217366, 0.051288057, 0.060445003, -0.007725884, -0.0033487668, -0.02697037, -0.04471915, 0.014793467, 0.0029390613, -0.04365732, -0.036976494, 0.05571355, -0.034228597, 0.05610819, 0.0016565409, 0.06461147, 0.012197695, -0.029221235, 0.015400638, 0.009992722, -0.0126949195, 0.027302667, 0.04309881, 0.013308768, -0.034253325, -0.028620966, 0.0032988666, 0.008901495, 0.0051033413, 0.08693829, -0.035939537, -0.00014025549, -0.0021354076, 0.043875773, -0.057092454, 0.0048032254, 0.04456835, -0.01337361, 0.018620204, -0.0037525205, 0.018113593, -0.0024051766, -0.006519982, 0.043426506, -0.028869089, -0.07003764, -0.027043046, -0.047674373, -0.036566455, -0.029664699, 0.054604772, 0.056459025, 0.016209831, 0.06588335, 0.07294827, -0.07351654, -0.050157, 0.05211485, -0.02302033, 0.022877783, 0.013553745, -0.019406103, -0.0058154585, 0.0373227, 0.0052685454, 0.02164789, -0.019631775, -0.015719362, -0.06862338, 0.021698158, -0.013781832, 0.06955018, -0.023942512, -0.018029014, -0.018007774, -0.0059923544, -0.02771734, -0.0019507131, -0.069619514, 0.054189045, 0.0021985532, -0.01132558, 0.015128105, 0.015424623, -0.038302787, -0.038970694, 0.044268098, 0.015156813, 0.030262465, -0.0010455108, -0.032175235, -0.03357542, -9.529959e-05, 0.062028274, -0.10134925, -0.009874221, 0.051682726, -0.022124732, 0.010147164, -0.012185555, 0.03731382, -0.00059438165, -0.017981028, -0.070909515, 0.02605233, 0.06992509, 0.026033426, -0.023944097, -0.047794044, 0.0204043, 0.025562089, -0.01985736, -0.027300185, 0.029983355, -0.0821883, -0.018791717, -0.004772287, -0.02490102, -0.010111937, 0.050968856, 0.029660473, 3.4716293e-05, -0.017517656, 0.023977743, 0.022549666, 0.04181301, 0.007500569, -0.0019229053, 0.023285722, -0.010899088, -0.004949611, -0.012531907, 0.041027624, -0.004620342, -0.013926477, -0.020054528, 0.026111232, -0.06232942, 0.09978252, -0.044156674, 0.061204664, 0.007044644, -0.0027112814, 0.04620226, 0.006134901, 0.03983195, -0.009853767, 0.0137631735, -0.07085734, 0.009606741, -0.008636412, 0.050337072, 0.045284208, -0.0032710661, -0.016086245, 0.008386805, -0.007903436, 0.0350885, 0.0025110857, 0.04684593, 0.12780859, -0.038998656, -0.029157333, -0.029113598, 0.0074333544, 0.05532698, -0.034412585, -0.00013683736, -0.020530468, 0.06506163, 0.0019480588, 0.0030335467, -0.018495142, -0.054084025, 0.023021378, -0.010500294, -0.007759436, -0.020039978, -0.017755102, 0.0006766737, 0.014525485, -0.026014434, 0.002474586, -0.027173916, 0.0093613025, 0.0058087856, 0.0006998545, 0.04791365, -0.04368597, -0.015235596, 0.0069595333, 0.009612967, -0.0009247106, 0.033619776, -0.00649697, -0.04766721, 0.0391879, -0.010284179, -0.006610166, -0.0020641836, -0.05440346, -0.007050968, -0.015853178, -0.031741284, -0.02172385, 0.03021658, -0.0012069787, 0.050265886, 0.04510601, -0.024716277, -0.05543306, -0.06419837, -0.014273427, -0.023703339, 0.0017521745, -0.056149185, 0.0069642677, 0.0065768356, 0.035255834, 0.039023213, 0.016403731, 0.025051782, 0.00695039, -0.05579997, 0.013183741, 0.08474835, -0.012680079, 0.0041794777, 0.02355896, -0.07197163, 0.024911461, -0.018766653, 0.025204346, 0.0048066434, 0.04904056, 0.016669538, -0.037882168, -0.021643393, 0.0053031743, -0.031009668, -0.016543044, -0.020345997, -0.005761681, -0.0743119, -0.02601627, -0.023271384, -0.07075993, -0.0029876109, 0.0066218525, -0.061091717, 0.032953493, 0.03662513, 0.010290128, 0.05418312, -0.03828874, 0.03312786, -0.014862627, -0.03720938, 0.018570531, -0.020742243, 0.048026983, 0.005438336, 0.020241424, -0.04405181, 0.030792728, 0.033958763, -0.023588262, 0.037658524, 0.010072951, 0.0064869304, 0.019048406, -0.06919818, -0.017083945, -0.016801478, 0.0027415873, 0.008172279, 0.0019755305, -0.057162683, -0.0053946367, 0.0014972482, -0.033361986, -0.0033606717, 0.03242665, 0.072544955, 0.02279949, -0.046871353, -0.06308129, 0.029209439, 0.011341486, 0.032790348, -0.020073028, -0.0044093695, 0.08292041, -0.03140556, 0.009308279, -0.004211382, -0.052444175, 0.0180874, 0.008575959, -0.0013550716, -0.07186043, 0.028372435, 0.024996122, 0.027749002, 0.016944503, -0.014632978, -0.06674174, -0.043031745, -0.044137582, 0.03530514, 0.030504197, 0.060496386, -0.06423886, 0.012235539, -0.05830343, -0.015868725, 0.041861057, 0.027080601, -0.014182999, -0.028095996, 0.0016349283, 0.010679886, 0.048808616, -0.058294244, -0.010633062, -0.056791265, -0.027161647, -0.030019993, -0.010299281, -0.03821823, -0.016588321, -0.0059704296, -0.053497788, 0.05661912, 0.005010262, -0.020186698, -0.03151958, -0.07490499, 0.045715272, -0.03747153, 0.02902543, 0.015007152, -0.01799195, 0.0079564275, -0.028715475, -0.018788284, -0.041037183, 0.012932907, -0.0072463937, -0.0046510296, 0.052094106, 0.047214568, -0.05604256, 0.006124289, -0.06112983, -0.028900363, -0.0033062366, -0.016411366, -0.03985708, -0.005927899, 0.027991273, -0.034023542, 0.0023991684, 0.020010024, 0.014298016, 0.017212953, 0.002652654, -0.08308305, 0.01726592, 0.013845524, 0.0065021385, 0.0364733, 0.020361774, 0.09685079, 0.04039578, 0.016480403, -0.08329836, -0.06590067, 0.00012861127, -0.055775307, 0.0065172235, -0.018937778, -0.021399701, 0.0004559998, -0.0097613875, -0.003239602, 0.0041429265, 0.059930306, -0.01656465, 0.018544743, -0.03232914, 0.006037772, -0.06402926, 0.05761484, -0.02093143, 0.018229362, 0.024098346, 0.025045564, -0.009451666, -0.010259512, 0.006660359, -0.029620942, -0.03495546, -0.06783166, -0.03193859, -0.04261954, 0.027878316, 0.023951625, 0.016354026, -0.0015310713, -0.05785183, -0.04868827, -0.06779814, -0.09212996, 0.04355289, 0.02634198, 0.045933742, -0.012108333, -0.017381534, 0.012251423, 0.035591044, 0.05024221, 0.056855064, 0.0101336455, -0.009532219, -0.054251555, 0.034745548, 0.020292252, 0.033525895, -0.040225316, -0.00015249893, -0.07806101, 0.0075722514, 0.015309747, 0.022623314, 0.06536824, 0.064232446, -0.01557734, -0.04813796, -0.013913105, 0.020742541, 0.060864896, -0.056623433, 0.057601452, -1.6570028e-05, 0.010925783, 0.0036125665, 0.032784764, -0.0801319, -0.048450164, 0.06296668, 0.02989288, -0.011754737, -0.0010066505, -0.05441974, -0.017106231, -0.04285682, -0.005424776, -0.028312048, -0.0022843084, -0.02028908, -0.007416978, 0.016722959, 0.03343588, -0.049168676, 0.003828647, 0.043084797, -0.011436926, -0.017679023, -0.012748326, -0.015104218, 0.008225339, -0.005965197, -0.010827806, -0.015990732, 0.031933613, 0.01862576, -0.013171726, 0.007987761, -0.018449496, 0.041906953, -0.020788714, 0.03404006, -0.00086082605, -0.007771558, 0.023855729, -0.00295711, -0.0085285455, -0.0556957, -0.005321175, -0.018151492, -0.011129989, -0.05183511, 0.0053123147, 0.009127998, -0.011530388, 0.009631709, 0.0041047884, -0.0353711, 0.052883077, -0.01532676, 0.03040235, 0.008731032, -0.00441319, 0.01950203, 0.014064995, 0.03141337, 0.018041868, 0.059427522, 0.048374873, -0.019928444, -0.004559623, 0.021962427, -0.08567552, -0.007796494, 0.033520035, 0.009779213, 0.05753526, 0.010492746, -0.039363436, -0.103733934, -0.024229618, 0.0062162466, -0.017748242, 0.005122951, -0.055344906, -0.010650967, 0.0309389, -0.073542334, -0.014872006, -0.003081951, 0.016437916, -0.0040901243, 0.0018574661, 0.03331834, 0.005815743, 0.022556618, 0.076257, -0.0065593896, -0.026774084, -0.016839791, 0.008689688, -0.015184644, 0.0073800148, -0.018499345, -0.036080927, 0.053406574, 0.015944907, -0.014478417, -0.021485219, -0.018035412, -0.038147416, 0.014293582, -0.021055873, 0.0314314, -0.07782329, 0.015536577, -0.031045694, 0.059434652, -0.020065695, 0.052754566, -0.08380041, 0.06855744, 0.012167185, -0.015827801, 0.04380172, 0.020258602, -0.058169313, -0.04435873, -0.013054301, -0.041333184, -0.02302342, 0.029140746, 0.00812361, 0.033690967, -0.0030892044, 0.052916355, -0.04835076, -0.0101818545, -0.05420185, -0.033779036, 0.02638142, -0.028346056, -0.02331669, -0.005781761, 0.012981267, -0.0055279816, 0.010089179, -0.04489518, -0.024379171, 0.007590703, -0.025511196, -0.06555892, 0.008145539, 0.021736145, -0.033178225, 0.026871512, -0.05637406, -0.030885229, 0.014512168, -0.008024667, 0.026689196, 0.004108927, -0.04103957, 0.0080031715, -0.0030232186, -0.036158007, 0.04256502, -0.0001681743, 0.0117336465, 0.025762333, -0.010921032, -0.0010622365, -0.07185124, 0.029530818, 0.009698986, 0.011916085, 0.0022654524, 0.07175238, 0.029233111, -0.020834876, -0.052442703, 0.011248308, 0.005422925, 0.018166017, 0.0472275, -0.013550265, 0.0350743, -0.010435109, 0.047774173, 0.021216916, -0.0026447468, -0.021085296, 0.013272342, -0.0133805, 0.02943836, -0.032338675, 0.0021435472, -0.016289461, -0.013629232, -0.03840216, 0.06655019, 0.009643849, 0.025085986, -0.018909356, -0.011246176, -0.05254555, -0.06776485, -0.02931862, 0.014850466, 0.029691922, -0.04090594, 0.0544204, 0.01552631, 0.02912549, -0.0020693596, 0.038805272, -0.009980787, 0.031122748, -0.05562063, 0.021108221, 0.0103203785, 0.044171233, 0.009732269, -0.0011330071]}
|
51 |
+
Now that you have created the embeddings, let's use the dot product to see how related close_to_x and different_from_x are to x. The dot product returns a value between -1 and 1, and represents how closely two vectors align in terms of what direction they point in. The closer the value is to 0, the less similar to objects (in this case, two strings) are. The closer the value is to 1, the more similar they are.
|
52 |
+
|
53 |
+
|
54 |
+
similar_measure = np.dot(embedding_x['embedding'], embedding_close_to_x['embedding'])
|
55 |
+
|
56 |
+
print(similar_measure)
|
57 |
+
|
58 |
+
0.7314063252924405
|
59 |
+
|
60 |
+
different_measure = np.dot(embedding_x['embedding'], embedding_different_from_x['embedding'])
|
61 |
+
|
62 |
+
print(different_measure)
|
63 |
+
|
64 |
+
0.43560702838194704
|
65 |
+
As shown here, the higher dot product value between the embeddings of x and close_to_x demonstrates more relatedness than the embeddings of x and different_from_x.
|
66 |
+
|
67 |
+
What can you do with embeddings?
|
68 |
+
You've generated your first set of embeddings with the PaLM API! But what can you do with this list of floating point values? Embeddings can be used for a wide variety of natural language processing (NLP) tasks, including:
|
69 |
+
|
70 |
+
Search (documents, web, etc.)
|
71 |
+
Recommendation systems
|
72 |
+
Clustering
|
73 |
+
Sentiment analysis/text classification
|
documents/guide.txt
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Generative AI for Developers
|
2 |
+
|
3 |
+
Large Language Models (LLMs) are a powerful, versatile type of machine learning model that enables computers to comprehend and generate natural language better than ever. They can be used to build all sorts of applications, from chat bots to virtual assistants to translation apps and much more. Plus, you don't have to be an AI expert or even write code to use them. All it takes are a few sentences or “prompts” to get started designing your own custom LLM app.
|
4 |
+
|
5 |
+
Further reading
|
6 |
+
To learn more about the models that power the PaLM API, see the models section.
|
7 |
+
To learn how to use LLMs safely and responsibly, see the safety settings and safety guidance documentation.
|
8 |
+
|
9 |
+
|
10 |
+
Models
|
11 |
+
|
12 |
+
bookmark_border
|
13 |
+
The PaLM API is based on Google’s next generation model, PaLM 2, which excels at a variety of capabilities. PaLM 2 has been optimized for ease of use on key developer use cases and the ability to follow instructions with precision and nuance. It has variations that are trained for text and chat generation as well as text embeddings. This guide provides information about each variation to help you decide which is the best fit for your use case.
|
14 |
+
|
15 |
+
Intended use
|
16 |
+
This model is intended to be used for a wide variety of natural language processing (NLP) applications such as chat bots, text summarization, and question and answer. The embedding service allows additional NLP use cases such as document search. It is only available to use through the PaLM API or the MakerSuite web app. Your use of PaLM API is also subject to the Generative AI Prohibited Use Policy and the Additional terms of service.
|
17 |
+
|
18 |
+
Limitations
|
19 |
+
Large language models are powerful tools, but they are not without their limitations. Their versatility and applicability can sometimes lead to unexpected outputs, such as outputs that are inaccurate, biased, or offensive. Post-processing, and rigorous manual evaluation are essential to limit the risk of harm from such outputs. See the safety guidance for additional safe use suggestions.
|
20 |
+
|
21 |
+
Model
|
22 |
+
This section provides more specific details about the model and each model variation.
|
23 |
+
|
24 |
+
Model attributes
|
25 |
+
The table below describes the attributes of the PaLM 2 model which are common to all the model variations.
|
26 |
+
|
27 |
+
Attribute Description
|
28 |
+
Training data PaLM 2's knowledge cutoff date is mid-2021. Knowledge about events past that date will be limited.
|
29 |
+
Supported language English
|
30 |
+
Configurable model parameters
|
31 |
+
Top p
|
32 |
+
Top k
|
33 |
+
Temperature
|
34 |
+
Stop sequence
|
35 |
+
Max output length
|
36 |
+
Number of response candidates
|
37 |
+
See the model parameters section of the Intro to LLMs guide for information about each of these parameters.
|
38 |
+
|
39 |
+
Model variations
|
40 |
+
The PaLM API offers different models optimized for specific use cases. The following table describes attributes of each.
|
41 |
+
|
42 |
+
Variation Attribute Description
|
43 |
+
text-bison-001 Model last updated May 2023
|
44 |
+
Model size Bison
|
45 |
+
Model capabilities
|
46 |
+
Generates text.
|
47 |
+
Optimized for language tasks such as:
|
48 |
+
Code generation
|
49 |
+
Text generation
|
50 |
+
Text editing
|
51 |
+
Problem solving
|
52 |
+
Recommendations generation
|
53 |
+
Information extraction
|
54 |
+
Data extraction or generation
|
55 |
+
AI agent
|
56 |
+
Can handle zero, one, and few-shot tasks.
|
57 |
+
Model safety Adjustable safety settings for 6 dimensions of harm available to developers. See the safety settings topic for details.
|
58 |
+
Rate limit during preview 30 requests per minute
|
59 |
+
chat-bison-001 Model last updated May 2023
|
60 |
+
Model size Bison
|
61 |
+
Model capabilities
|
62 |
+
Generates text in a conversational format.
|
63 |
+
Optimized for dialog language tasks such as implementation of chat bots or AI agents.
|
64 |
+
Can handle zero, one, and few-shot tasks.
|
65 |
+
Model safety No adjustable safety settings.
|
66 |
+
Rate limit during preview 30 requests per minute
|
67 |
+
embedding-gecko-001 Model last updated May 2023
|
68 |
+
Model size Gecko
|
69 |
+
Model capabilities
|
70 |
+
Generates text embeddings for the input text.
|
71 |
+
Optimized for creating embeddings for text of up to 1024 tokens.
|
72 |
+
Model safety No adjustable safety settings.
|
73 |
+
Rate limit during preview 300 requests per minute
|
74 |
+
See the prompt gallery and the examples to see the capabilities of these model variations in action.
|
75 |
+
|
76 |
+
Model sizes
|
77 |
+
The model sizes are described by an animal name. The following table shows the available sizes and what they mean relative to each other.
|
78 |
+
|
79 |
+
Model size Description Services
|
80 |
+
Bison PaLM API's most capable model size.
|
81 |
+
text
|
82 |
+
chat
|
83 |
+
Gecko PaLM API's smallest, most efficient model size.
|
84 |
+
embeddings
|
85 |
+
Model metadata
|
86 |
+
Use the ModelService API to get additional metadata about the latest models such as input and output token limits. The following table displays the metadata for the text-bison-001 model.
|
87 |
+
|
88 |
+
Note: For the PaLM 2 model, token is equivalent to about 4 characters. 100 tokens are about 60-80 English words.
|
89 |
+
Attribute Value
|
90 |
+
Display Name Text Bison
|
91 |
+
Name models/text-bison-001
|
92 |
+
Description Model targeted for text generation
|
93 |
+
Input token limit 8196
|
94 |
+
Output token limit 1024
|
95 |
+
Supported generation methods generateText
|
96 |
+
Temperature 0.7
|
97 |
+
top_p 0.95
|
98 |
+
top_k 40
|
99 |
+
|
100 |
+
|
101 |
+
Get an API key
|
102 |
+
|
103 |
+
bookmark_border
|
104 |
+
To use the API, you need an API key. Once you're through the waitlist you can create a key with one click in MakerSuite.
|
105 |
+
|
106 |
+
Get an API key
|
107 |
+
Note: Remember to use your API keys securely. Check out this support article to learn best practices.
|
108 |
+
Verify your API key with curl command
|
109 |
+
You can use a curl command to verify your setup. You can pass the API key either in the URL:
|
110 |
+
|
111 |
+
|
112 |
+
PALM_KEY="YOUR KEY HERE"
|
113 |
+
curl -H 'Content-Type: application/json' \
|
114 |
+
-d '{"prompt": {text: "Give me five subcategories of jazz"} }' \
|
115 |
+
"https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=${PALM_KEY}"
|
116 |
+
Or in the x-goog-api-key header:
|
117 |
+
|
118 |
+
|
119 |
+
PALM_KEY="YOUR KEY HERE"
|
120 |
+
curl -H 'Content-Type: application/json' \
|
121 |
+
-H "x-goog-api-key: ${PALM_KEY}" \
|
122 |
+
-d '{"prompt": {text: "Give me five subcategories of jazz"} }' \
|
123 |
+
"https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText"
|
documents/stable.txt
ADDED
@@ -0,0 +1,348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Index
|
2 |
+
Introduction
|
3 |
+
Google Colab
|
4 |
+
Local Installation (Windows + Nvidia)
|
5 |
+
Getting Started
|
6 |
+
Models
|
7 |
+
VAEs
|
8 |
+
Prompts
|
9 |
+
Generation parameters
|
10 |
+
Extensions
|
11 |
+
Loras
|
12 |
+
Lycoris
|
13 |
+
Upscaling
|
14 |
+
Scripts
|
15 |
+
X/Y/Z Plot
|
16 |
+
Prompt Matrix
|
17 |
+
Ultimate Upscaler
|
18 |
+
ControlNet
|
19 |
+
Lora Training for beginners
|
20 |
+
...vtubers?
|
21 |
+
|
22 |
+
|
23 |
+
Introduction ▲
|
24 |
+
Stable Diffusion is a very powerful AI image generation software you can run on your own home computer. It uses "models" which function like the brain of the AI, and can make almost anything, given that someone has trained it to do it. The biggest uses are anime art, photorealism, and NSFW content.
|
25 |
+
|
26 |
+
The images you create may be used for any purpose, depending on the used model's license. Whether they are "yours" in a legal sense varies by local laws and is often inconclusive. Neither I or any of the people involved in Stable Diffusion or its models are responsible for anything you make, and you are expressively forbidden from creating illegal or harmful content.
|
27 |
+
|
28 |
+
This guide is up to date with the best practices as of March 2023. One week is like a year in AI time, so hopefully it is still useful by the time you read it.
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
Google Colab ▲
|
33 |
+
The easiest way to use Stable Diffusion is through Google Colab. It borrows Google's computers to use AI, with variable time limitations, usually a few hours every day. You will need at least one Google account and we will be using Google Drive to store your settings and resulting images.
|
34 |
+
|
35 |
+
If you instead want to run it on your own computer, scroll down ▼.
|
36 |
+
|
37 |
+
Open THIS PAGE.
|
38 |
+
|
39 |
+
Near the top, click Copy to Drive. Wait for the new window to open and close the old one. This is now your personalized colab which will save your settings, and you should open it from your Google Drive from now on. If the original receives an update you'll have to replace yours to benefit from it.
|
40 |
+
|
41 |
+
Turn on the following options under Configurations: output_to_drive, configs_in_drive, no_custom_theme. Then, turn on the following options under Models, VAEs, etc: anything_vae, wd_vae, sd_vae.
|
42 |
+
|
43 |
+
If you're already familiar with Stable Diffusion, you may paste links to your desired resources in the custom_urls text box. We will add some links later in this guide. Links must be direct downloads to each file (ideally from civitai or huggingface), and must be separated by commas.
|
44 |
+
|
45 |
+
Press the play button to the left, anywhere in the first section of the page labeled Start 🚀. Wait a few minutes for it to finish, while a few progress messages appear near the bottom. Then, a public link will be created, which you can open in a new tab to start using Stable Diffusion. Keep the colab tab open! (On mobile try the trick at the bottom of the colab to keep the tab open)
|
46 |
+
|
47 |
+
You can now make some decent anime images thanks to the default Anything 4.5 model. But we can do more. Also, what are all of these options? Scroll down ▼ to get started.
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
Local Installation (Windows + Nvidia) ▲
|
52 |
+
To run Stable Diffusion on your own computer you'll need at least 16 GB of RAM and 4 GB of VRAM (preferably 8). I will only cover the case where you are running Windows 10/11 and using an NVIDIA graphics card series 16XX, 20XX or 30XX (though 10XX also work). My apologies to AMD, Linux, and Mac users, but their cases are harder to cover. If you don't meet the hardware requirements, you can just proceed with the Google Colab method above ▲.
|
53 |
+
|
54 |
+
Get the latest release from this page.
|
55 |
+
|
56 |
+
Run the installer, choose an easy and accessible location to install to, and wait for it to finish.
|
57 |
+
|
58 |
+
Run the program. You will see a few options. First, turn on medvram and xformers. You may skip medvram if you have 12 or more GB of VRAM.
|
59 |
+
|
60 |
+
Set your Additional Launch Options to: --opt-channelslast --no-half-vae --theme dark . Any extra options should be separated by spaces.
|
61 |
+
|
62 |
+
If your graphics card has 4-6 GB of VRAM, add --opt-split-attention-v1 as it may lower vram usage even further.
|
63 |
+
If you want to run the program from your computer but want to use it in another device, such as your phone, add --listen --enable-insecure-extension-access . After launching, use your computer's local IP in the same WiFi network to access the interface. You can also add a password like --gradio-auth name:1234 .
|
64 |
+
Full list of possible parameters here
|
65 |
+
Click Launch and wait for a browser window to open with the interface. It may take a while the first time.
|
66 |
+
|
67 |
+
The page is now open. It's your own private website. The starting page is where you can make your images. But first, we'll go to the Settings tab. There will be sections of settings on the left.
|
68 |
+
|
69 |
+
In the Stable Diffusion section, scroll down and increase Clip Skip from 1 to 2. This is said to produce better images, specially for anime.
|
70 |
+
In the User Interface section, scroll down to Quicksettings list and change it to sd_model_checkpoint, sd_vae
|
71 |
+
Scroll back up, click the big orange Apply settings button, then Reload UI next to it.
|
72 |
+
You are more than ready to generate some images, but you only have the basic model available. It's not great, at most it can make some paintings. Also, what are all of these options? See below ▼ to get started.
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
Getting Started ▲
|
77 |
+
Before or after generating your first few images, you will want to take a look at the information below to improve your experience and results.
|
78 |
+
If you followed the instructions above, the top of your page should look similar to this:
|
79 |
+
|
80 |
+
Top
|
81 |
+
|
82 |
+
Here you can select your checkpoint and VAE. We will go over what these are and how you can get some. The colab has additional settings here too, you should ignore them for now.
|
83 |
+
|
84 |
+
Models ▲
|
85 |
+
|
86 |
+
The model, also called checkpoint, is the brain of your AI, designed for the purpose of producing certain types of images. There are many options, most of which are on civitai. But which to choose? These are my recommendations:
|
87 |
+
|
88 |
+
For anime, 7th Heaven Mix has a nice aesthetic similar to anime movies, while Abyss Orange Mix 3 (Note: scroll down there and choose the AOM3 option) offers more realism in the form of advanced lighting and softer shading, as well as more lewdness. I merged these two options into Heaven Orange Mix.
|
89 |
+
While AOM3 is extremely capable for NSFW, the popular Grapefruit hentai model may also fit your needs.
|
90 |
+
For general art go with DreamShaper, there are few options quite like it in terms of creativity. An honorable mention goes to Pastel Mix, which has a beautiful and unique aesthetic with the addition of anime.
|
91 |
+
For photorealism go with Deliberate. It can do almost anything, but specially photographs. Very intricate results.
|
92 |
+
The Uber Realistic Porn Merge is self-explanatory.
|
93 |
+
If you're using the colab in this guide, copy the direct download link to the file and paste it in the text box labeled custom_urls. Multiple links are separated by commas.
|
94 |
+
|
95 |
+
If you're running the program locally, the models normally go into the stable-diffusion-webui/models/Stable-diffusion folder.
|
96 |
+
|
97 |
+
Please note that checkpoints in the format .safetensors are safe to use while .ckpt may contain viruses, so be careful. Additionally, when choosing models you may have a choice between fp32, fp16 and pruned. They all produce the same images within a tiny margin of error, so just go with the smallest file (pruned-fp16). If you want to use them for training or merging, go with the largest one instead.
|
98 |
+
|
99 |
+
Tip: Whenever you place a new file manually you can either restart the UI at the bottom of the page or press the small 🔃 button next to its dropdown.
|
100 |
+
|
101 |
+
VAEs ▲
|
102 |
+
|
103 |
+
Most checkpoints don't come with a VAE built in. The VAE is a small separate model, which "converts your image into human format". Without it, you'll get faded colors and ugly eyes, among other things.
|
104 |
+
|
105 |
+
If you're using the colab in this guide, you should already have the below VAEs, as I told you to select them before running.
|
106 |
+
|
107 |
+
There are practically only 3 different VAEs in circulation:
|
108 |
+
|
109 |
+
anything vae, also known as the orangemix vae. All anime models use this.
|
110 |
+
vae-ft-mse, the latest from Stable Diffusion itself. Used by photorealism models and such.
|
111 |
+
kl-f8-anime2, also known as the Waifu Diffusion VAE, it is older and produces more saturated results. Used by Pastel Mix.
|
112 |
+
The VAEs normally go into the stable-diffusion-webui/models/VAE folder.
|
113 |
+
|
114 |
+
If you did not follow this guide up to this point, you will have to go into the Settings tab, then the Stable Difussion section, to select your VAE.
|
115 |
+
|
116 |
+
Tip: Whenever you place a new file manually you can either restart the UI at the bottom of the page or press the small 🔃 button next to its dropdown.
|
117 |
+
|
118 |
+
Prompts ▲
|
119 |
+
|
120 |
+
On the first tab, txt2img, you'll be making most of your images. This is where you'll find your prompt and negative prompt.
|
121 |
+
Stable Diffusion is not like Midjourney or other popular image generation software, you can't just ask it what you want. You have to be specific. Very specific.
|
122 |
+
Most people have found a prompt that works for them and they swear by it, often recommended by other people. I will show you my own personal example of a prompt and negative prompt:
|
123 |
+
|
124 |
+
Anime
|
125 |
+
|
126 |
+
2d, masterpiece, best quality, anime, highly detailed face, highly detailed background, perfect lighting
|
127 |
+
EasyNegative, worst quality, low quality, 3d, realistic, photorealistic, (loli, child, teen, baby face), zombie, animal, multiple views, text, watermark, signature, artist name, artist logo, censored
|
128 |
+
Photorealism
|
129 |
+
|
130 |
+
best quality, 4k, 8k, ultra highres, raw photo in hdr, sharp focus, intricate texture, skin imperfections, photograph of
|
131 |
+
EasyNegative, worst quality, low quality, normal quality, child, painting, drawing, sketch, cartoon, anime, render, 3d, blurry, deformed, disfigured, morbid, mutated, bad anatomy, bad art
|
132 |
+
EasyNegative: The negative prompts above use EasyNegative, which is an embedding or "magic word" that encodes many bad things to make your images better. Otherwise you'd have to use a huge negative prompt.
|
133 |
+
|
134 |
+
If you're using the colab in this guide you already have this installed. Otherwise, you will have to download this tiny file, put it in your stable-diffusion-webui/embeddings folder, then go to the bottom of your WebUI page and click Reload UI. It will then work when you type that word.
|
135 |
+
A comparison with and without these negative prompts including EasyNegative can be seen further down ▼.
|
136 |
+
|
137 |
+
Prompts
|
138 |
+
|
139 |
+
After a "base prompt" like the above, you may then start typing what you want. For example young woman in a bikini in the beach, full body shot. Feel free to add other terms you don't like to your negatives such as old, ugly, futanari, furry, etc.
|
140 |
+
You can also save your prompts to reuse later with the buttons below Generate. Click the small 💾 Save style button and give it a name. Later, you can open your Styles dropdown to choose, then click 📋 Apply selected styles to the current prompt.
|
141 |
+
|
142 |
+
One important technique when writing prompts are emphasis and de-emphasis. When you surround something in (parentheses), it will get more emphasis or weight in your resulting image, basically telling the AI that part is more important. The normal weight for every word is 1, and each parentheses will multiply by 1.1 (you can use multiple). You can also specify the weight yourself, like this: (full body:1.4). You can also go below 1 to de-emphasize a word: [brackets] will multiply by 0.9, but you'll still need parentheses to go lower, like (this:0.5).
|
143 |
+
|
144 |
+
Also note that hands and feet are famously difficult for AI to generate. These methods improve your chances, but you may need to do photoshopping, inpainting, or advanced techniques with ControlNet ▼ to get it right.
|
145 |
+
|
146 |
+
Generation parameters ▲
|
147 |
+
|
148 |
+
The rest of the parameters in the starting page will look something like this:
|
149 |
+
|
150 |
+
Parameters
|
151 |
+
|
152 |
+
Sampling method: This is the algorithm that formulates your image, and each produce different results. The default of Euler a is often the best. There are also very good results for DPM++ 2M Karras and DPM++ SDE Karras. See below for a comparison.
|
153 |
+
Sampling steps: These are "calculated" beforehand, and so more steps doesn't always mean more detail. I always go with 30, you may go from 20-50 and find consistently good results. See below for a comparison.
|
154 |
+
Width and Height: 512x512 is the default, and you should almost never go above 768 in either direction as it may distort and deform your image. To produce bigger images see Hires fix.
|
155 |
+
Batch Count and Batch Size: Batch size is how many images your graphics card will generate at the same time, which is limited by its VRAM. Batch count is how many times to repeat those. Batches have consecutive seeds, more on seeds below.
|
156 |
+
CFG Scale: "Lower values produce more creative results". You should almost always stick to 7, but 4 to 10 is an acceptable range.
|
157 |
+
Seed: A number that guides the creation of your image. The same seed with the same prompt and parameters produces the same image every time, except for small details and under some circumstances.
|
158 |
+
Hires fix: Lets you create larger images without distortion. Often used at 2x scale. When selected, more options appear:
|
159 |
+
|
160 |
+
Upscaler: The algorithm to upscale with. Latent and its variations produce creative and detailed results, but you may also like R-ESRGAN 4x+ and its anime version. More explanation and some comparisons further down ▼.
|
161 |
+
Hires steps: I recommend at least half as many as your sampling steps. Higher values aren't always better, and they take a long time, so be conservative here.
|
162 |
+
Denoising strength: The most important parameter. Near 0.0, no detail will be added to the image. Near 1.0, the image will be changed completely. I recommend something between 0.2 and 0.6 depending on the image, to add enough detail as the image gets larger, without destroying any original details you like.
|
163 |
+
Others:
|
164 |
+
|
165 |
+
Restore faces: May improve realistic faces. I never need it with the models and prompts listed in this guide as well as hires fix.
|
166 |
+
Tiling: Used to produce repeating textures to put on a grid. Not very useful.
|
167 |
+
Script: Lets you access useful features and extensions, such as X/Y/Z Plot ▼ which lets you compare images with varying parameters on a grid. Very powerful.
|
168 |
+
Here is a comparison of a few popular samplers and various sampling steps:
|
169 |
+
|
170 |
+
(Click) Sampler comparison - Photography
|
171 |
+
(Click) Sampler comparison - Anime
|
172 |
+
An explanation of the samplers used above: Euler is a basic sampler. DDIM is a faster version, while DPM++ 2M Karras is an improved version. Meanwhile we have Euler a or "ancestral" which produces more creative results, and DPM++ 2S a Karras which is also ancestral and thus similar. Finally DPM++ SDE Karras is the slowest and quite unique. There are many other samplers not shown here but most of them are related.
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
Extensions ▲
|
177 |
+
Stable Diffusion WebUI supports extensions to add additional functionality and quality of life. These can be added by going into the Extensions tab, then Install from URL, and pasting the links found here or elsewhere. Then, click Install and wait for it to finish. Then, go to Installed and click Apply and restart UI.
|
178 |
+
|
179 |
+
Extensions
|
180 |
+
|
181 |
+
Here are some useful extensions. If you're using the colab in this guide you already have most of these, otherwise I hugely recommend you manually add the first 2:
|
182 |
+
|
183 |
+
Image Browser (updated) - This will let you browse your past generated images very efficiently, as well as directly sending their prompts and parameters back to txt2img, img2img, etc.
|
184 |
+
TagComplete - Absolutely essential for anime art. It will show you the matching booru tags as you type. Anime models work via booru tags, and prompts without them usually don't work, so knowing them is godmode. Not all tags will work well in all models though, specially if they're rare.
|
185 |
+
Locon - Lets you use LoCons and LoHas. More info below ▼.
|
186 |
+
ControlNet - A huge extension deserving of its own guide ▼. It lets you analyze any image and use it as an referene for your own image. Practically speaking, it can create any pose or environment you want.
|
187 |
+
Ultimate Upscale - A script usable from the img2img section to make really large images, where normally you can only go as high as your VRAM allows. See Ultimate Upscaler ▼.
|
188 |
+
Two-shot - Normally you can't create more than one distinct character in the same image without them blending together. This extension lets you divide the image into parts; full, left side, right side; allowing you to make nice 2-character images.
|
189 |
+
Dynamic Prompts - A script to let you generate randomly chosen elements in your image, among other things.
|
190 |
+
Model Converter - Lets you convert most 7GB/4GB models down to 2GB, by choosing safetensors, fp16, and no-ema. These pruned models work "almost the same" as the full models, which is to say, there is no appreciable difference due to math reasons. Most models come in 2 GB form nowadays regardless.
|
191 |
+
|
192 |
+
|
193 |
+
Loras ▲
|
194 |
+
LoRA or Low-Rank Adaptation is a form of Extra Network and the latest technology that lets you append a sort of smaller model to any of your full models. They are similar to embeddings, one of which you might've seen earlier ▲, but Loras are larger and often more capable. Technical details omitted.
|
195 |
+
|
196 |
+
Loras can represent a character, an artstyle, poses, clothes, or even a human face (though I do not endorse this). Checkpoints are usually capable enough for general work, but when it comes to specific details with little existing examples, you'll need a Lora. They can be downloaded from civitai or elsewhere (NSFW) and are 144 MB by default, but they can go as low as 1 MB. Bigger Loras are not always better. They come in .safetensors format, same as most checkpoints.
|
197 |
+
|
198 |
+
Place your Lora files in the stable-diffusion-webui/models/Lora folder, or if you're using the colab in this guide paste the direct download link into the custom_urls text box. Then, look for the 🎴 Show extra networks button below the big orange Generate button. It will open a new section either directly below or at the very bottom. Click on the Lora tab and press the Refresh button to scan for new Loras. When you click a Lora in that menu it will get added to your prompt, looking like this: <lora:filename:1>. The start is always the same. The filename will be the exact filename in your system without the .safetensors extension. Finally, the number is the weight, like we saw earlier ▲. Most Loras work between 0.5 and 1 weight, and too high values might "fry" your image, specially if using multiple Loras at the same time.
|
199 |
+
|
200 |
+
Extra Networks
|
201 |
+
|
202 |
+
An example of a Lora is Thicker Lines Anime Style, which is perfect if you want your images to look more like traditional anime.
|
203 |
+
|
204 |
+
Lycoris ▲
|
205 |
+
|
206 |
+
LyCORIS is a new development that lets LoRAs learn more layers. Learn about it here. You'll need this extension to use them.
|
207 |
+
|
208 |
+
As of now there are 2 new LoRA types:
|
209 |
+
|
210 |
+
LoCon - Said to be good with styles
|
211 |
+
LoHa - Said to be good with styles that also contain characters
|
212 |
+
You can make your own in the trainer further down ▼.
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
Upscaling ▲
|
217 |
+
As mentioned in Generation Parameters ▲, normally you shouldn't go above 768 width or height when generating an image. Instead you should use Hires fix with your choice of upscaler and an appropiate denoising level. Hires fix is limited by your VRAM however, so you may be interested in Ultimate Upscaler ▼ to go even larger.
|
218 |
+
|
219 |
+
You can download additional upscalers and put them in your stable-diffusion-webui/models/ESRGAN folder. They will then be available in Hires fix, Ultimate Upscaler, and Extras.
|
220 |
+
|
221 |
+
The colab in this guide comes with several of them, including Remacri, which is a great all-around upscaler for all sorts of images.
|
222 |
+
|
223 |
+
A few notable ones can be found here.
|
224 |
+
LDSR is an advanced yet slow upscaler, its model and config can be found here and both must be placed in stable-diffusion-webui/models/LDSR.
|
225 |
+
The Upscale Wiki contains dozens of historical choices.
|
226 |
+
Here are some comparisons. All of them were done at 0.4 denoising strength. Note that some of the differences may be completely up to random chance.
|
227 |
+
|
228 |
+
(Click) Comparison 1: Anime, stylized, fantasy
|
229 |
+
(Click) Comparison 2: Anime, detailed, soft lighting
|
230 |
+
(Click) Comparison 3: Photography, human, nature
|
231 |
+
|
232 |
+
|
233 |
+
Scripts ▲
|
234 |
+
Scripts can be found at the bottom of your generation parameters in txt2img or img2img.
|
235 |
+
|
236 |
+
X/Y/Z Plot ▲
|
237 |
+
|
238 |
+
Capable of generating a series of images, usually with the exact same seed, but varying parameters of your choice. Can compare almost anything you want, including different models, parts of your prompt, sampler, upscaler and much more. You can have 1, 2, or 3 variable parameters, hence the X, Y and Z.
|
239 |
+
|
240 |
+
Your parameters in X/Y/Z Plot are separated by commas, but anything else can go inbetween. The most common parameter to compare is S/R Prompt, where the first term is a phrase in your prompt and each term afterwards will replace the original. Knowing this, you can compare, say, Lora intensity, like this:
|
241 |
+
|
242 |
+
<lora:my lora:0.4>, <lora:my lora:0.6>, <lora:my lora:0.8>, <lora:my lora:1>
|
243 |
+
|
244 |
+
Here I made a comparison between different models (columns) and faces of different ethnicities via S/R Prompt (rows):
|
245 |
+
|
246 |
+
(Click) X/Y/Z Plot example
|
247 |
+
Tip: It appears possible to do S/R with commas by using quotes like this (note no spaces between the commas and quotes): "term 1, term 2","term 3, term 4","term 5, term 6"
|
248 |
+
|
249 |
+
Prompt Matrix ▲
|
250 |
+
|
251 |
+
Similar conceptually to S/R from before, but more in-depth. It works by showing each possible combination of terms listed between the | symbol in your prompt, for example: young man|tree|city will always contain "young man", but we'll see what happens when we add or remove "tree" and "city". You can use commas and spaces just fine between the |.
|
252 |
+
|
253 |
+
Inside the script, you will choose either your prompt or your negative prompt to make a matrix of, and whether the variable terms should be put at the start or the end.
|
254 |
+
|
255 |
+
Here is a comparison using the negative prompts I showed you in Prompts ▲. We can see how EasyNegative affects the image, as well as how the rest of the prompt affects the image, then both together:
|
256 |
+
|
257 |
+
(Click) Prompt matrix examples
|
258 |
+
Tip: When using prompt matrix, the Batch Size will let you generate multiple images or the whole grid all at once.
|
259 |
+
|
260 |
+
Ultimate Upscaler ▲
|
261 |
+
|
262 |
+
An improved version of a builtin script, it can be added as an extension ▲ and used from within img2img. Its purpose is to resize an image and add more detail way past the normal limits of your VRAM by splitting it into chunks, although slower. Here are the steps:
|
263 |
+
|
264 |
+
Generate your image normally up to 768 width and height, you can then apply hires fix if you are able to.
|
265 |
+
|
266 |
+
From txt2img or the Image Browser extension send it directly into img2img, carrying its prompt and parameters.
|
267 |
+
|
268 |
+
Set the Denoising somewhere between 0.1 and 0.4. If you go higher you most likely will experience mutations.
|
269 |
+
|
270 |
+
Go down to Scripts and choose Ultimate SD Upscale. Then, set your parameters like this, with your desired size and upscaler, and the "Chess" Type:
|
271 |
+
|
272 |
+
Ultimate upscale parameters
|
273 |
+
|
274 |
+
If you have enough VRAM, you may increase the Tile width as well as the Padding. For example, doubling both of them. Tile height can remain at 0 and it'll match the width.
|
275 |
+
|
276 |
+
It is not necessary to set the Seams fix unless you encounter visible seams between regions in the final image.
|
277 |
+
|
278 |
+
Generate your image and wait. You can watch the squares get sharper if you have image previews enabled.
|
279 |
+
|
280 |
+
|
281 |
+
|
282 |
+
ControlNet ▲
|
283 |
+
ControlNet is an extremely powerful recent technology for Stable Diffusion. It lets you analyze information about any previously existing image and use it to guide the generation of your AI images. We'll see what this means in a moment.
|
284 |
+
|
285 |
+
If you're using the colab in this guide, you should enable the all_control_models option. Otherwise, you should first install the ControlNet extension ▲, then go here to download some models which you'll need to place in stable-diffusion-webui/extensions/sd-webui-controlnet/models. I recommend at least Canny, Depth, Openpose and Scribble, which I will show here.
|
286 |
+
|
287 |
+
I will demonstrate how ControlNet may be used. For this I chose a popular image online as our "sample image". It's not necessary for you to follow along, but you can download the images and put them in the PNG Info tab to view their generation data.
|
288 |
+
|
289 |
+
First, you must scroll down in the txt2img page and click on ControlNet to open the menu. Then, click Enable, and pick a matching preprocessor and model. To start with, I chose Canny for both. Finally I upload my sample image. Make sure not to click over the sample image or it will start drawing. We can ignore the other settings.
|
290 |
+
|
291 |
+
Control Net
|
292 |
+
|
293 |
+
Canny
|
294 |
+
|
295 |
+
The Canny method extracts the hard edges of the sample image. It is useful for many different types of images, specially where you want to preserve small details and the general look of an image. Observe:
|
296 |
+
|
297 |
+
(Click) Canny example
|
298 |
+
Depth
|
299 |
+
|
300 |
+
The Depth method extracts the 3D elements of the sample image. It is best suited for complex environments and general composition. Observe:
|
301 |
+
|
302 |
+
(Click) Depth example
|
303 |
+
Openpose
|
304 |
+
|
305 |
+
The Openpose method extracts the human poses of the sample image. It helps tremendously to get the desired shot and composition of your generated characters. Observe:
|
306 |
+
|
307 |
+
(Click) Openpose example
|
308 |
+
Scribble
|
309 |
+
|
310 |
+
Lets you make a simple sketch and convert it into a finished piece with the help of your prompt. This is the only example not using the sample image above.
|
311 |
+
|
312 |
+
(Click) Scribble example
|
313 |
+
You will notice that there are 2 results for each method except Scribble. The first is an intermediate step called the preprocessed image, which is then used to produce the final image. You can supply the preprocessed image yourself, in which case you should set the preprocessor to None. This is extremely powerful with external tools such as Blender and Photoshop.
|
314 |
+
|
315 |
+
In the Settings tab there is a ControlNet section where you can enable multiple controlnets at once. One particularly good use is when one of them is Openpose, to get a specific character pose in a specific environment, or with specific hand gestures or details. Observe:
|
316 |
+
|
317 |
+
(Click) Openpose+Canny example
|
318 |
+
You can also use ControlNet in img2img, in which the input image and sample image both will have a certain effect on the result. I do not have much experience with this method.
|
319 |
+
|
320 |
+
There are also alternative diff versions of each ControlNet model, which produce slightly different results. You can try them if you want, but I personally haven't.
|
321 |
+
|
322 |
+
|
323 |
+
|
324 |
+
Lora Training for beginners ▲
|
325 |
+
To train a Lora ▲ is regarded as a difficult task. However, my new guide covers everything you need to know to get started for free, thanks to Google Colab:
|
326 |
+
|
327 |
+
🎴 Read my Lora making guide here
|
328 |
+
|
329 |
+
You can also train a Lora on your own computer if you have at least 8 GB of VRAM. For that, I will list a few resources below:
|
330 |
+
|
331 |
+
For training, use bmaltais' Kohya GUI. It has all the same settings as my trainer colab and more, so you can follow my guide too. Also there are youtube tutorials available in this link.
|
332 |
+
Also, here's an angry Lora training guide by ao
|
333 |
+
To collect your images from Gelbooru like in my guide, install Grabber.
|
334 |
+
To tag your dataset use the WD1.4 Tagger extension for webui. First add and enable the extension, and restart your entire webui. Then go to the new Tagger tab, then Batch from directory, and select the folder with your images. Set the output name to [name].txt and the threshold at or above 0.35 (this is how closely each tag must match an image to be included). Then Interrogate and it will start generating your text files.
|
335 |
+
To curate your tags like in my guide use the Tag Editor extension for webui. It has all the features you need like sorting, pruning, replacing and merging tags. To add an activation tag it's as follows: After adding the extension and restarting your webui, go to the new Dataset Tag Editor tab then Batch Edit Captions. Turn off "Show only the tags...", turn on "Prepend additional tags", then add your activation tag inside the Edit Tags text box. Then apply your changes, scroll up and save your changes. Only then will it modify your files and add a new tag at the beginning of every text file.
|
336 |
+
|
337 |
+
|
338 |
+
...vtubers? ▲
|
339 |
+
That's it, that's the end of this guide for now. I'd be grateful if you want to contribute on missing topics like:
|
340 |
+
|
341 |
+
img2img
|
342 |
+
Inpainting
|
343 |
+
Controlnet t2i adapters
|
344 |
+
Thank you for reading!
|
345 |
+
|
346 |
+
I have a separate repo that aggregates vtuber Loras, specially Hololive. If you're interested in that.
|
347 |
+
|
348 |
+
Cheers.
|
gen-lang-client-0582614236-c66336e67562.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "gen-lang-client-0582614236",
|
4 |
+
"private_key_id": "c66336e6756253b59968a5f2a47aeb60d62894a2",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCt/6XG+oaFYY34\nWMjY5GTZgDMJ0C3FTegbEV/XD/nRmWEgzWn6Y2sOa1DlWZyj4h8XhoXTYzrXRu+Q\nGDUAVJHDdUVKKJ4ZjM2hVqL1H1rnnTyhlOYBzdi4DNBRPqeBxa2U8UCWtamrm8dq\nQ3Ro8/gdScbIgsDPMHe/xaCwPtIJXRpIpvJNHpro48CuKLAnXdNcT99dCH4lqXD8\ngNaqIXQ3mxf60VBRn1TSh8gnK4zkmhd0rT1y2V/Cu5bJ8dRQGBI0T7iB7xRzfc2t\n4SX//P/Y6YLZ2mGCmUHK0o6ZwDt7wftKr6aj/THmFYfT/AAmg9BVOIqaCSS+zKkx\nGoNu1Ux7AgMBAAECggEABMf/ih4ixV7ZfYZn14xQuFMvcbennHV8c+SgZV+fejo+\nJASty04QMvjmwbSBA96CvwJp6LwgLXcmzwTpIpnJ6gEhBMPtXVBe2inFfk78utPs\nzzF1YvUJssR+TkbSRg/kmP3kIVVj7mDwVhLa+qV12sz6H5cyV/JbmzvF3zY3gdjZ\nyLwotYQusO4lfLn/bABovBOpANzcq7LhF22Zx9DRO8PxTnNQZsmWzw9BDjGQ/eev\nXnmXht7OPGlk5ei7Wowuq6km4tfMzA4n0EdpF/RilRHZ98YVo4bNDlO4vDwplSJf\nrmeE1FxcWM2nPbDnc5+bGKAKfXHJmRxfNlwDXiF7OQKBgQDmylqsXHWtOs6nYWAP\nNl8hL59+gU+Tczm4N+s77g+fOD9r/psrCbxxyc9axg8rDIi6Li9Aiso7EjKCRjsl\neWX4Tr+ZAtiX7XHN2+vppXFEr/fwoHRS6MsI89b7J4ZJb15DexRF3fEoPxNhW30J\np9lO9Xu/9Qmds+Va6/59g8VUVwKBgQDBATXwd214HL+fv4I49fJp2nymwvMpcwO2\n9iUhkeXOA/UyeR+01QvbKUsJrOUKAHT+gJwzOCYSYx+r9b6/lSJOJ+OEmz2yOx6f\nAi68ctyk/r9itA95YCu23VXfZn+9kgeefeMwdVym6t9h9KubeEU6PF562KB6Oa0o\nJQRZxLsSfQKBgQDKhewHsKM7Po216AdJkibskkPd2U+hTs3p0HhGgOOIju1osWYs\n4EBTggrJ11/cIQWii4C/nItWRuO32jVw7xUCTuhnN04mFCF3ynM89wdshRaIO+WY\nE9yHoKKYhtiafHY1tT3XCanj7WXuW0BgRsC1P0o26qpjj0fG/6y1K/rvxQKBgH9L\nJSAsJG8Lxva9gE3BKNjX2U7DVU0JJnINdf/mEgMGaTmY2WiS+vbZDTycY5NtKbyR\nZnQBnFqUNddchSomWjHh7RUQrmhWxaNp9RklSJEp02BuEaaj3zEEWha1q5qELbJC\npQzY2vrb/ejFTkmD0WTPXCD5rx3t3F6Vy4DJV/ihAoGACAigorEIK3fwRWzXWr0z\nW+KTGxerQJpZNVErNxGF8NwEz7+zmlBAYEHSpm77sazJbp8a/+rA/YSeIWgN+ZNL\nn5Qpe6oyON+10dy3NphN148BQO1hPMBAkMmh3pbAnwqX3ZWkz6aeEFfXldQG7lJm\nShD/nYOKSzlNP++tt2V4nlU=\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "palm2docgenie-webui@gen-lang-client-0582614236.iam.gserviceaccount.com",
|
7 |
+
"client_id": "116013782022183245155",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/palm2docgenie-webui%40gen-lang-client-0582614236.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|
key.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
google palm 2 key = AIzaSyCMyiuFNaTTrkZU8HSIOszpZh2Z8Vq3Rdc
|
modules/__pycache__/document_loader.cpython-311.pyc
ADDED
Binary file (1.81 kB). View file
|
|
modules/__pycache__/embedding_generator.cpython-311.pyc
ADDED
Binary file (3.89 kB). View file
|
|
modules/__pycache__/forms.cpython-311.pyc
ADDED
Binary file (825 Bytes). View file
|
|
modules/__pycache__/text_generator.cpython-311.pyc
ADDED
Binary file (1.3 kB). View file
|
|
modules/document_loader.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import glob
|
3 |
+
from pypdf import PdfReader
|
4 |
+
|
5 |
+
DOCUMENT_DIR = "./documents/"
|
6 |
+
|
7 |
+
# handle_loading of pdf files
|
8 |
+
|
9 |
+
def load_pdf_text(filename):
|
10 |
+
reader = PdfReader(filename)
|
11 |
+
text = ""
|
12 |
+
for page in reader.pages:
|
13 |
+
text += page.extract_text()
|
14 |
+
return text
|
15 |
+
|
16 |
+
# create the document loader function
|
17 |
+
def load_documents(document_dir=DOCUMENT_DIR, file_extensions=['.txt', '.pdf']):
|
18 |
+
documents = []
|
19 |
+
for file_extension in file_extensions:
|
20 |
+
for filename in glob.glob(os.path.join(document_dir, f"*{file_extension}")):
|
21 |
+
if file_extension == '.pdf':
|
22 |
+
doc_text = load_pdf_text(filename)
|
23 |
+
else:
|
24 |
+
with open(filename, 'r', encoding="utf-8", errors="replace") as f:
|
25 |
+
doc_text = f.read()
|
26 |
+
if doc_text.strip():
|
27 |
+
documents.append((doc_text))
|
28 |
+
else:
|
29 |
+
print(f"Skipping empty document: {filename}")
|
30 |
+
return documents
|
modules/embedding_generator.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import google.generativeai as palm
|
4 |
+
import chromadb
|
5 |
+
from chromadb.api.types import Documents, Embeddings, EmbeddingFunction
|
6 |
+
from chromadb.utils import embedding_functions
|
7 |
+
|
8 |
+
|
9 |
+
class MyEmbeddingFunction(EmbeddingFunction):
|
10 |
+
def __call__(self, input: Documents) -> Embeddings:
|
11 |
+
sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction(
|
12 |
+
model_name="BAAI/bge-large-en-v1.5")
|
13 |
+
embeddings = sentence_transformer_ef(input)
|
14 |
+
return embeddings
|
15 |
+
|
16 |
+
|
17 |
+
custom = MyEmbeddingFunction()
|
18 |
+
|
19 |
+
|
20 |
+
def configure_palm(api_key):
|
21 |
+
palm.configure(api_key=api_key)
|
22 |
+
|
23 |
+
|
24 |
+
def get_text_model():
|
25 |
+
models = [m for m in palm.list_models(
|
26 |
+
) if 'embedText' in m.supported_generation_methods]
|
27 |
+
if not models:
|
28 |
+
raise ValueError(
|
29 |
+
"No models found that support 'embedText' generation method.")
|
30 |
+
return models[0]
|
31 |
+
|
32 |
+
|
33 |
+
def embed_function(texts: Documents, text_model) -> MyEmbeddingFunction:
|
34 |
+
return [palm.generate_embeddings(model=text_model, text=text)['embedding'] for text in texts]
|
35 |
+
|
36 |
+
|
37 |
+
def create_chroma_db(documents, name, text_model):
|
38 |
+
chroma_client = chromadb.Client()
|
39 |
+
db = chroma_client.create_collection(
|
40 |
+
name=name, embedding_function=custom)
|
41 |
+
for i, d in enumerate(documents):
|
42 |
+
db.add(documents=d, ids=str(i))
|
43 |
+
return db
|
44 |
+
|
45 |
+
|
46 |
+
def generate_embeddings_df(documents, text_model):
|
47 |
+
# Set up the ChromaDB database
|
48 |
+
db = create_chroma_db(documents, "embeddings_db", text_model)
|
49 |
+
|
50 |
+
# Retrieve the embeddings and metadata from the database
|
51 |
+
embeddings_data = db.peek(len(documents))
|
52 |
+
|
53 |
+
# Create a DataFrame from the embeddings data
|
54 |
+
df = pd.DataFrame(embeddings_data)
|
55 |
+
df.columns = ['Text', 'Embeddings', 'Document_ID',
|
56 |
+
'Embedding_ID']
|
57 |
+
df['Embeddings'] = df['Embeddings'].apply(np.array)
|
58 |
+
|
59 |
+
return df
|
modules/forms.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask_wtf import FlaskForm
|
2 |
+
from wtforms import StringField, SubmitField
|
3 |
+
from wtforms.validators import DataRequired
|
4 |
+
|
5 |
+
class ChatForm(FlaskForm):
|
6 |
+
message = StringField('Ask a Question', validators=[DataRequired()])
|
7 |
+
submit = SubmitField('Submit')
|
modules/text_generator.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as palm
|
2 |
+
|
3 |
+
def get_text_generation_model():
|
4 |
+
text_models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
|
5 |
+
if not text_models:
|
6 |
+
raise ValueError("No models found that support 'generateText' generation method.")
|
7 |
+
return text_models[0]
|
8 |
+
|
9 |
+
def generate_text(prompt, text_model, temperature=0.5, candidate_count=3, max_output_tokens=5000):
|
10 |
+
section = palm.generate_text(
|
11 |
+
prompt=prompt,
|
12 |
+
model=text_model,
|
13 |
+
candidate_count=candidate_count,
|
14 |
+
temperature=temperature,
|
15 |
+
max_output_tokens=max_output_tokens
|
16 |
+
)
|
17 |
+
return section
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
numpy
|
3 |
+
pandas
|
4 |
+
google.generativeai
|
5 |
+
scikit-learn
|
6 |
+
flask_wtf
|
7 |
+
wtforms
|
8 |
+
pypdf
|
9 |
+
chromadb==0.3.29
|
10 |
+
gunicorn
|
11 |
+
sentence_transformers
|
routes/__pycache__/main_routes.cpython-311.pyc
ADDED
Binary file (9.97 kB). View file
|
|
routes/main_routes.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from modules import text_generator
|
2 |
+
from modules import embedding_generator
|
3 |
+
from modules import document_loader
|
4 |
+
from flask import request, render_template, jsonify
|
5 |
+
import textwrap
|
6 |
+
import os
|
7 |
+
import numpy as np
|
8 |
+
import pandas as pd
|
9 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
10 |
+
import google.generativeai as palm
|
11 |
+
from modules.forms import ChatForm
|
12 |
+
from sklearn.decomposition import TruncatedSVD
|
13 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
14 |
+
|
15 |
+
|
16 |
+
print("Setting environment variable: ", os.path.join(
|
17 |
+
os.getcwd(), "gen-lang-client-0582614236-c66336e67562.json"))
|
18 |
+
|
19 |
+
path = os.path.join(
|
20 |
+
os.getcwd(), "gen-lang-client-0582614236-c66336e67562.json")
|
21 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = path
|
22 |
+
|
23 |
+
|
24 |
+
DOCUMENT_DIR = "./documents/"
|
25 |
+
RESPONSE_FILENAME = 'conversation.txt'
|
26 |
+
|
27 |
+
# Load documents
|
28 |
+
documents = document_loader.load_documents()
|
29 |
+
|
30 |
+
print(f"Loaded {len(documents)} documents.")
|
31 |
+
|
32 |
+
# Configure palm and get text model
|
33 |
+
API_KEY = os.getenv('API_KEY')
|
34 |
+
embedding_generator.configure_palm(API_KEY)
|
35 |
+
text_model = embedding_generator.get_text_model()
|
36 |
+
|
37 |
+
# Generate embeddings DataFrame
|
38 |
+
df = embedding_generator.generate_embeddings_df(documents, text_model)
|
39 |
+
|
40 |
+
# Get text generation model
|
41 |
+
text_model2 = text_generator.get_text_generation_model()
|
42 |
+
|
43 |
+
# The existing find_best_passage, make_prompt, and other functions remain the same
|
44 |
+
|
45 |
+
|
46 |
+
def ensure_same_dimensions(X, Y):
|
47 |
+
# Determine the smaller number of features
|
48 |
+
n_components = min(X.shape[1], Y.shape[1])
|
49 |
+
|
50 |
+
svd = TruncatedSVD(n_components=n_components)
|
51 |
+
|
52 |
+
# Fit the SVD transformer with the larger dimension data
|
53 |
+
if X.shape[1] > Y.shape[1]:
|
54 |
+
svd.fit(X)
|
55 |
+
else:
|
56 |
+
svd.fit(Y)
|
57 |
+
|
58 |
+
X_transformed = svd.transform(X)
|
59 |
+
Y_transformed = svd.transform(Y)
|
60 |
+
|
61 |
+
return X_transformed, Y_transformed
|
62 |
+
|
63 |
+
|
64 |
+
def find_best_passage(query, dataframe):
|
65 |
+
# Generate embeddings for the query!
|
66 |
+
vectorizer = TfidfVectorizer()
|
67 |
+
query_embedding = vectorizer.fit_transform([query])
|
68 |
+
|
69 |
+
# Stack the embeddings into a matrix.
|
70 |
+
embeddings_matrix = vectorizer.transform(dataframe['Text'].to_numpy())
|
71 |
+
|
72 |
+
# Ensure the query_embedding and embeddings_matrix have the same dimensions
|
73 |
+
query_embedding_transformed, embeddings_matrix_transformed = ensure_same_dimensions(
|
74 |
+
query_embedding, embeddings_matrix)
|
75 |
+
|
76 |
+
# Calculate the cosine similarity between the query and each passage in the document.
|
77 |
+
cosine_similarities = cosine_similarity(
|
78 |
+
embeddings_matrix_transformed, query_embedding_transformed)
|
79 |
+
|
80 |
+
# Find the index of the passage with the highest cosine similarity to the query.
|
81 |
+
idx = np.argmax(cosine_similarities)
|
82 |
+
|
83 |
+
# Return the passage with the highest cosine similarity and its cosine similarity value.
|
84 |
+
return dataframe.iloc[idx]['Text'], cosine_similarities[idx][0]
|
85 |
+
|
86 |
+
# Next we need to create the make_prompt function. This function will take the query and the best passage and return the prompt.
|
87 |
+
|
88 |
+
|
89 |
+
def make_prompt(query, relevant_passage):
|
90 |
+
escaped = relevant_passage.replace(
|
91 |
+
"'", "").replace('"', "").replace("\n", " ")
|
92 |
+
prompt = textwrap.dedent("""\
|
93 |
+
AI assistant, answer the query: '{query}'. Use the following relevant passage: '{escaped}'.
|
94 |
+
Provide a comprehensive response with necessary context or background information. Feel free to use your own knowledge to fill in any gaps or provide additional insights.
|
95 |
+
""").format(query=query, escaped=escaped)
|
96 |
+
|
97 |
+
return prompt
|
98 |
+
|
99 |
+
|
100 |
+
# create register route app function
|
101 |
+
def register_routes(app):
|
102 |
+
# Now we need to create the app route for the home page.
|
103 |
+
@app.route('/', methods=['GET', 'POST'])
|
104 |
+
def index():
|
105 |
+
loaded_files = os.listdir(DOCUMENT_DIR)
|
106 |
+
if request.method == 'POST':
|
107 |
+
query = request.form['topic']
|
108 |
+
passage = find_best_passage(query, df)
|
109 |
+
prompt = make_prompt(query, passage)
|
110 |
+
temperature = 0.2
|
111 |
+
section = palm.generate_text(
|
112 |
+
prompt=prompt,
|
113 |
+
model=text_model2,
|
114 |
+
candidate_count=3,
|
115 |
+
temperature=temperature,
|
116 |
+
max_output_tokens=8000
|
117 |
+
)
|
118 |
+
|
119 |
+
if not section.candidates:
|
120 |
+
return "No text was generated for this topic."
|
121 |
+
|
122 |
+
section = section.candidates[0]['output']
|
123 |
+
with open(RESPONSE_FILENAME, 'a') as response_file:
|
124 |
+
response_file.write(f"Query: {query}\nResponse: {section}\n")
|
125 |
+
|
126 |
+
return f"Response to '{query}' has been added to the response file."
|
127 |
+
|
128 |
+
# assuming you have a template named index.html
|
129 |
+
return render_template('index.html', loaded_files=loaded_files)
|
130 |
+
|
131 |
+
@app.route('/get-models', methods=['GET'])
|
132 |
+
def get_models():
|
133 |
+
embedding_model_name = text_model.name if text_model else None
|
134 |
+
generation_model_name = text_model2.name if text_model2 else None
|
135 |
+
print(f"Embedding Model In Use: {embedding_model_name}")
|
136 |
+
print(f"Text Generation Model In Use: {generation_model_name}")
|
137 |
+
return jsonify({'embedding_model': embedding_model_name, 'generation_model': generation_model_name})
|
138 |
+
|
139 |
+
@app.route('/chat', methods=['GET', 'POST'])
|
140 |
+
def chat():
|
141 |
+
form = ChatForm()
|
142 |
+
if request.method == 'POST':
|
143 |
+
user_message = request.form['message']
|
144 |
+
response = palm.chat(messages=user_message)
|
145 |
+
model_response = response.last
|
146 |
+
return jsonify({'model_response': model_response})
|
147 |
+
return render_template('chat.html', form=form)
|
148 |
+
|
149 |
+
@app.route('/generate-section', methods=['POST'])
|
150 |
+
def generate_section():
|
151 |
+
topic = request.form['topic']
|
152 |
+
print("topic: ", topic)
|
153 |
+
# print("['Embeddings']: ", df['Embeddings'][0])
|
154 |
+
print("\n\ndataframe: ", df)
|
155 |
+
|
156 |
+
passage, cosine_similarity = find_best_passage(topic, df)
|
157 |
+
prompt = make_prompt(topic, passage)
|
158 |
+
|
159 |
+
temperature = 0.3
|
160 |
+
section = palm.generate_text(
|
161 |
+
prompt=prompt,
|
162 |
+
model=text_model2,
|
163 |
+
candidate_count=3,
|
164 |
+
temperature=temperature,
|
165 |
+
max_output_tokens=8000
|
166 |
+
)
|
167 |
+
if not section.candidates:
|
168 |
+
return jsonify({'error': 'No text was generated for this topic.'})
|
169 |
+
|
170 |
+
section = section.candidates[0]['output']
|
171 |
+
with open(RESPONSE_FILENAME, 'a', encoding='utf-8') as response_file:
|
172 |
+
# Write the user's query to the file
|
173 |
+
response_file.write(f"User Query: {topic}\n")
|
174 |
+
# Write the generated
|
175 |
+
response_file.write(f"Document Genie Says:\n{section}\n")
|
176 |
+
return jsonify({'section': section, 'cosine_similarity': cosine_similarity})
|
177 |
+
|
178 |
+
@app.route('/clear-document', methods=['POST'])
|
179 |
+
def clear_document():
|
180 |
+
clear_output = request.form.get('clear_output')
|
181 |
+
if clear_output == 'true':
|
182 |
+
# Clear the output screen
|
183 |
+
with open(RESPONSE_FILENAME, 'w') as response_file:
|
184 |
+
response_file.write('')
|
185 |
+
|
186 |
+
# Get the updated list of loaded files
|
187 |
+
loaded_files = os.listdir(DOCUMENT_DIR)
|
188 |
+
# Return a JSON response indicating the success of clearing the document
|
189 |
+
return jsonify({'success': 'Document cleared', 'loaded_files': loaded_files})
|
static/css/styles.css
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
|
3 |
+
"Liberation Mono", "Courier New", monospace;
|
4 |
+
height: 100vh;
|
5 |
+
display: flex;
|
6 |
+
flex-direction: column;
|
7 |
+
background-color: #f2f2f2;
|
8 |
+
color: black;
|
9 |
+
}
|
10 |
+
|
11 |
+
h3.models {
|
12 |
+
text-align: left;
|
13 |
+
padding-top: 20px;
|
14 |
+
color: #000;
|
15 |
+
}
|
16 |
+
|
17 |
+
.container-fluid {
|
18 |
+
flex: 1;
|
19 |
+
display: flex;
|
20 |
+
flex-direction: column;
|
21 |
+
height: 100%;
|
22 |
+
}
|
23 |
+
|
24 |
+
.sidebar {
|
25 |
+
display: flex;
|
26 |
+
flex-direction: column;
|
27 |
+
background-color: #fff;
|
28 |
+
color: #fff;
|
29 |
+
padding: 1rem;
|
30 |
+
position: relative;
|
31 |
+
/* border: 1px solid lightgray; */
|
32 |
+
border-radius: 15px;
|
33 |
+
flex: 0.5 1 30px;
|
34 |
+
height: 100%;
|
35 |
+
}
|
36 |
+
|
37 |
+
.input-container {
|
38 |
+
display: flex;
|
39 |
+
justify-content: center;
|
40 |
+
align-items: center;
|
41 |
+
width: 100%;
|
42 |
+
}
|
43 |
+
|
44 |
+
.sidebar-footer {
|
45 |
+
text-align: center;
|
46 |
+
color: #000;
|
47 |
+
}
|
48 |
+
|
49 |
+
.sidebar-content {
|
50 |
+
flex: 1 1 auto;
|
51 |
+
text-align: center;
|
52 |
+
}
|
53 |
+
.sidebar-content p {
|
54 |
+
text-align: left;
|
55 |
+
}
|
56 |
+
|
57 |
+
.chat-window {
|
58 |
+
display: flex;
|
59 |
+
flex-direction: column;
|
60 |
+
background-color: #fff;
|
61 |
+
color: #000;
|
62 |
+
/* border: 1px solid lightgray; */
|
63 |
+
padding: 1rem;
|
64 |
+
position: relative;
|
65 |
+
border-radius: 15px;
|
66 |
+
flex: 2;
|
67 |
+
height: 550px;
|
68 |
+
}
|
69 |
+
|
70 |
+
.chat-window-2 {
|
71 |
+
display: flex;
|
72 |
+
flex-direction: column;
|
73 |
+
background-color: #fff;
|
74 |
+
color: #000;
|
75 |
+
/* border: 1px solid lightgray; */
|
76 |
+
padding: 1rem;
|
77 |
+
position: relative;
|
78 |
+
border-radius: 15px;
|
79 |
+
flex: 2;
|
80 |
+
height: 600px;
|
81 |
+
}
|
82 |
+
|
83 |
+
.document-view {
|
84 |
+
background-color: #fff;
|
85 |
+
border-radius: 15px;
|
86 |
+
padding: 1rem;
|
87 |
+
flex: 0 0 250px;
|
88 |
+
/* border: 1px solid lightgray; */
|
89 |
+
height: 550px;
|
90 |
+
margin-right: 30px;
|
91 |
+
}
|
92 |
+
|
93 |
+
.mac-btns {
|
94 |
+
display: flex;
|
95 |
+
justify-content: flex-start;
|
96 |
+
align-items: center;
|
97 |
+
gap: 4px;
|
98 |
+
}
|
99 |
+
|
100 |
+
.mac-btn {
|
101 |
+
width: 12px;
|
102 |
+
height: 12px;
|
103 |
+
border-radius: 150%;
|
104 |
+
}
|
105 |
+
|
106 |
+
.red {
|
107 |
+
background-color: #ff5f56;
|
108 |
+
}
|
109 |
+
|
110 |
+
.yellow {
|
111 |
+
background-color: #ffbd2e;
|
112 |
+
}
|
113 |
+
|
114 |
+
.green {
|
115 |
+
background-color: #27c93f;
|
116 |
+
}
|
117 |
+
|
118 |
+
.title {
|
119 |
+
text-align: center;
|
120 |
+
margin-bottom: 1rem;
|
121 |
+
color: #156ba6;
|
122 |
+
font-weight: bold;
|
123 |
+
}
|
124 |
+
|
125 |
+
.input-field {
|
126 |
+
width: 100%;
|
127 |
+
max-width: calc(100% - 20px);
|
128 |
+
margin-top: 1rem;
|
129 |
+
}
|
130 |
+
|
131 |
+
.chat-content {
|
132 |
+
flex: 1;
|
133 |
+
overflow-y: auto;
|
134 |
+
}
|
135 |
+
|
136 |
+
.chat-input {
|
137 |
+
margin-top: auto;
|
138 |
+
}
|
139 |
+
|
140 |
+
.thicky {
|
141 |
+
/* border: 2px solid #ccc; */
|
142 |
+
margin: 1rem 0;
|
143 |
+
}
|
144 |
+
|
145 |
+
#output {
|
146 |
+
font-family: "Roboto", sans-serif;
|
147 |
+
font-size: 16px;
|
148 |
+
line-height: 1.6;
|
149 |
+
display: none;
|
150 |
+
white-space: pre-wrap;
|
151 |
+
/* border: 1px solid lightgray; */
|
152 |
+
padding: 15px;
|
153 |
+
border-radius: 15px;
|
154 |
+
background-color: rgb(30, 30, 30);
|
155 |
+
color: #f6f6f6;
|
156 |
+
box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
|
157 |
+
width: 100%;
|
158 |
+
margin-top: 10px;
|
159 |
+
height: auto;
|
160 |
+
}
|
161 |
+
|
162 |
+
.user-message {
|
163 |
+
color: #27c93f;
|
164 |
+
}
|
165 |
+
.model-response {
|
166 |
+
color: #fbbd00;
|
167 |
+
}
|
168 |
+
|
169 |
+
.text-center {
|
170 |
+
margin-top: 10px;
|
171 |
+
}
|
172 |
+
|
173 |
+
.d-flex {
|
174 |
+
display: flex;
|
175 |
+
gap: 20px;
|
176 |
+
justify-content: center;
|
177 |
+
align-items: center;
|
178 |
+
height: 100%;
|
179 |
+
}
|
180 |
+
.title {
|
181 |
+
margin-top: 10px;
|
182 |
+
}
|
static/img/00006-2189979373-removebg-preview.png
ADDED
![]() |
static/img/PaLM2_Doc_Genie.png
ADDED
![]() |
static/img/cosign_sims.png
ADDED
![]() |
static/img/doc_view.png
ADDED
![]() |
static/img/document-genie.png
ADDED
![]() |
static/img/genie.png
ADDED
![]() |
static/img/logo.png
ADDED
![]() |
static/img/optimization_visual.png
ADDED
![]() |
static/main.js
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
var topicForm = document.getElementById('topic-form');
|
2 |
+
if (topicForm) {
|
3 |
+
topicForm.addEventListener('submit', function(event) {
|
4 |
+
event.preventDefault();
|
5 |
+
|
6 |
+
var topic = document.getElementById('topic').value;
|
7 |
+
|
8 |
+
fetch('/generate-section', {
|
9 |
+
method: 'POST',
|
10 |
+
headers: {
|
11 |
+
'Content-Type': 'application/x-www-form-urlencoded'
|
12 |
+
},
|
13 |
+
body: 'topic=' + encodeURIComponent(topic)
|
14 |
+
})
|
15 |
+
.then(response => response.json())
|
16 |
+
.then(data => {
|
17 |
+
var output = document.getElementById('output');
|
18 |
+
if (data.error) {
|
19 |
+
output.textContent = data.error;
|
20 |
+
} else {
|
21 |
+
output.innerHTML = '<span style="color: #27c93f; font-weight: bold;">Your Question:</span><br>'
|
22 |
+
+ topic +
|
23 |
+
'<br><br><span style="color: #FBBD00; font-weight: bold;">Genie Says:</span><br>'
|
24 |
+
+ data.section +
|
25 |
+
'<br><span style="color: red; font-weight: bold;">Cosine Similarity:</span><br>'
|
26 |
+
+ data.cosine_similarity.toFixed(2)
|
27 |
+
}
|
28 |
+
// Show the output div after getting the response
|
29 |
+
output.style.display = 'block';
|
30 |
+
});
|
31 |
+
});
|
32 |
+
}
|
33 |
+
function submitChatForm(event) {
|
34 |
+
event.preventDefault();
|
35 |
+
var message = document.getElementById('message').value;
|
36 |
+
|
37 |
+
console.log('Submitting message:', message); // Add this line for debugging
|
38 |
+
|
39 |
+
fetch('/chat', {
|
40 |
+
method: 'POST',
|
41 |
+
headers: {
|
42 |
+
'Content-Type': 'application/x-www-form-urlencoded'
|
43 |
+
},
|
44 |
+
body: 'message=' + encodeURIComponent(message)
|
45 |
+
})
|
46 |
+
.then(response => {
|
47 |
+
console.log('Response received:', response); // Add this line for debugging
|
48 |
+
return response.json();
|
49 |
+
})
|
50 |
+
.then(data => {
|
51 |
+
console.log('Data received:', data); // Add this line for debugging
|
52 |
+
|
53 |
+
var chatContainer = document.getElementById('chat-output');
|
54 |
+
if (data.error) {
|
55 |
+
chatContainer.innerHTML += '<p class="error">' + data.error + '</p>';
|
56 |
+
} else {
|
57 |
+
chatContainer.innerHTML += '<p style="font-weight:bold">Your Query: </p>' + message + '\n'; // Add newline character here
|
58 |
+
chatContainer.innerHTML += '<p style="font-weight:bold">Genie Says: </p>' + data.model_response + '\n'; // Add newline character here
|
59 |
+
}
|
60 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
61 |
+
});
|
62 |
+
}
|
63 |
+
function saveChat() {
|
64 |
+
const chatContent = document.getElementById('chat-output').textContent;
|
65 |
+
const blob = new Blob([chatContent], { type: "text/plain;charset=utf-8" });
|
66 |
+
const link = document.createElement('a');
|
67 |
+
link.href = URL.createObjectURL(blob);
|
68 |
+
link.download = 'chat-session.txt';
|
69 |
+
link.click();
|
70 |
+
}
|
71 |
+
document.getElementById('save-button').addEventListener('click', saveChat);
|
72 |
+
|
73 |
+
var clearDocument = document.getElementById('clear-document-btn');
|
74 |
+
if (clearDocument) {
|
75 |
+
clearDocument.addEventListener('click', function(event) {
|
76 |
+
event.preventDefault();
|
77 |
+
|
78 |
+
fetch('/clear-document', {
|
79 |
+
method: 'POST',
|
80 |
+
headers: {
|
81 |
+
'Content-Type': 'application/x-www-form-urlencoded'
|
82 |
+
},
|
83 |
+
body: 'clear_output=true'
|
84 |
+
})
|
85 |
+
.then(response => response.text())
|
86 |
+
.then(data => {
|
87 |
+
var output = document.getElementById('output');
|
88 |
+
output.textContent = ''; // Clear the output section
|
89 |
+
// Hide the output div after clearing the output
|
90 |
+
output.style.display = 'none';
|
91 |
+
});
|
92 |
+
});
|
93 |
+
}
|
94 |
+
|
templates/chat.html
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<meta charset="UTF-8">
|
6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.0/css/all.min.css">
|
9 |
+
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism-tomorrow.min.css" rel="stylesheet" />
|
10 |
+
<link rel="stylesheet" href="/static/css/styles.css">
|
11 |
+
<title>Bluechip Technologies Asia</title>
|
12 |
+
<style>
|
13 |
+
/* Add the following CSS */
|
14 |
+
#chat-output {
|
15 |
+
font-family: 'Roboto', sans-serif;
|
16 |
+
font-size: 16px;
|
17 |
+
line-height: 1.6;
|
18 |
+
white-space: pre-wrap;
|
19 |
+
border: 1px solid #156BA6;
|
20 |
+
padding: 15px;
|
21 |
+
border-radius: 5px;
|
22 |
+
background-color: #F2F2F2;
|
23 |
+
color: #000;
|
24 |
+
box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
|
25 |
+
width: 100%;
|
26 |
+
margin-top: 10px;
|
27 |
+
height: auto;
|
28 |
+
}
|
29 |
+
|
30 |
+
.user-message {
|
31 |
+
color: #27c93f;
|
32 |
+
}
|
33 |
+
|
34 |
+
.model-response {
|
35 |
+
color: #FBBD00;
|
36 |
+
}
|
37 |
+
</style>
|
38 |
+
</head>
|
39 |
+
|
40 |
+
<body>
|
41 |
+
<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: #156BA6;">
|
42 |
+
<div class="container d-flex">
|
43 |
+
<a class="navbar-brand" href="/chat">🧞 PaLM2 Chat Genie</a>
|
44 |
+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
|
45 |
+
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
46 |
+
<span class="navbar-toggler-icon"></span>
|
47 |
+
</button>
|
48 |
+
<div class="collapse navbar-collapse" id="navbarNav">
|
49 |
+
<ul class="navbar-nav w-100 d-flex justify-content-between">
|
50 |
+
<li class="nav-item">
|
51 |
+
<a class="nav-link" href="/">Document Genie</a>
|
52 |
+
</li>
|
53 |
+
<div style="float: right;">
|
54 |
+
<a href="https://github.com/Nick-Williamz/Palm-DocumentGenie-Webui" target="_blank"><svg
|
55 |
+
xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="white"
|
56 |
+
class="bi bi-github" viewBox="0 0 16 16">
|
57 |
+
<path
|
58 |
+
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
|
59 |
+
</svg></a>
|
60 |
+
</div>
|
61 |
+
</ul>
|
62 |
+
</div>
|
63 |
+
</div>
|
64 |
+
</nav>
|
65 |
+
<div class="container-fluid">
|
66 |
+
<h1 class="title">Bluechip Technologies Asia</h1>
|
67 |
+
<div class="title">
|
68 |
+
<img src="/static/img/logo.png" class="genie rounded" height="150px" width="150px" alt="Document Genie" />
|
69 |
+
</div>
|
70 |
+
<p class="text-center small">Chat With Googles Largest LLMs</p>
|
71 |
+
<div class="d-flex" style="gap: 20px; height: 100%;">
|
72 |
+
<div class="chat-window-2">
|
73 |
+
<div class="mac-btns">
|
74 |
+
<div class="mac-btn red"></div>
|
75 |
+
<div class="mac-btn yellow"></div>
|
76 |
+
<div class="mac-btn green"></div>
|
77 |
+
<span class="ms-2">Chat Genie V 1.0</span>
|
78 |
+
</div>
|
79 |
+
<pre id="chat-output"></pre>
|
80 |
+
<div class="chat-input">
|
81 |
+
<hr>
|
82 |
+
<form id="chat-form" class="mt-4" onsubmit="submitChatForm(event)">
|
83 |
+
{{ form.csrf_token }}
|
84 |
+
<div class="form=group">
|
85 |
+
<label for="message">Chat With Googles Largest LLMs:</label>
|
86 |
+
<input type="text" id="message" name="message" class="form-control"
|
87 |
+
placeholder="Ask a Question" required>
|
88 |
+
</div>
|
89 |
+
<div class="text-center">
|
90 |
+
<button type="submit" class="btn btn-primary mt-3">Submit</button>
|
91 |
+
<button id="save-button" class="btn btn-secondary mt-3">Save Chat</button>
|
92 |
+
</div>
|
93 |
+
</form>
|
94 |
+
</div>
|
95 |
+
</div>
|
96 |
+
|
97 |
+
</div>
|
98 |
+
</div>
|
99 |
+
<script src="/static/main.js"></script>
|
100 |
+
<script>
|
101 |
+
|
102 |
+
</script>
|
103 |
+
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
|
104 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
|
105 |
+
<!-- Bootstrap JS and jQuery -->
|
106 |
+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
|
107 |
+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
|
108 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
|
109 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/components/prism-python.min.js"></script>
|
110 |
+
</body>
|
111 |
+
|
112 |
+
</html>
|
templates/index.html
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<meta charset="UTF-8">
|
6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.0/css/all.min.css">
|
9 |
+
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism-tomorrow.min.css" rel="stylesheet" />
|
10 |
+
<link rel="stylesheet" href="/static/css/styles.css">
|
11 |
+
<title>Bluechip Technologies Asia</title>
|
12 |
+
</head>
|
13 |
+
|
14 |
+
<body>
|
15 |
+
<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: #156BA6;">
|
16 |
+
<div class="container d-flex">
|
17 |
+
<a class="navbar-brand" href="/">🧞 PaLM2 Document Genie</a>
|
18 |
+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
|
19 |
+
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
20 |
+
<span class="navbar-toggler-icon"></span>
|
21 |
+
</button>
|
22 |
+
<div class="collapse navbar-collapse" id="navbarNav">
|
23 |
+
<ul class="navbar-nav w-50 d-flex justify-content-between">
|
24 |
+
<li class="nav-item">
|
25 |
+
<a class="nav-link" href="/chat">Chat Genie</a>
|
26 |
+
</li>
|
27 |
+
</ul>
|
28 |
+
</div>
|
29 |
+
<a href="https://github.com/Nick-Williamz/Palm-DocumentGenie-Webui" target="_blank"><svg
|
30 |
+
xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="white" class="bi bi-github"
|
31 |
+
viewBox="0 0 16 16">
|
32 |
+
<path
|
33 |
+
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
|
34 |
+
</svg></a>
|
35 |
+
</div>
|
36 |
+
</nav>
|
37 |
+
<div class="container-fluid">
|
38 |
+
<h1 class="title">Bluechip Technologies Asia</h1>
|
39 |
+
<p class="text-center large">Upload Your Documents, Ask Questions, & Store Converstations</p>
|
40 |
+
<div class="d-flex" style="gap: 20px; height: 100%;">
|
41 |
+
<div class="sidebar">
|
42 |
+
<div class="sidebar-content">
|
43 |
+
<img src="/static/img/logo.png" class="genie rounded" alt="Document Genie" />
|
44 |
+
<hr>
|
45 |
+
<h3 class="models"><strong>Embedding Model:</strong><br><span id="embedding-model"></span></h3>
|
46 |
+
<h3 class="models"><strong>Text Generation:</strong><br><span id="generation-model"></span></h3>
|
47 |
+
</div>
|
48 |
+
<div class="sidebar-footer">
|
49 |
+
<h5>Powered By:PaLM 2</h5>
|
50 |
+
|
51 |
+
<hr>
|
52 |
+
<b>
|
53 |
+
<p class="large" style="font-size: 20px;">Bluechip Technologies Asia</p>
|
54 |
+
</b>
|
55 |
+
<p class="small">Your Trusted Technology Partner</p>
|
56 |
+
|
57 |
+
<a href="https://bluechiptech.asia" target="_blank">Contact us</a>
|
58 |
+
|
59 |
+
</div>
|
60 |
+
</div>
|
61 |
+
<div class="chat-window">
|
62 |
+
<div class="mac-btns">
|
63 |
+
<div class="mac-btn red"></div>
|
64 |
+
<div class="mac-btn yellow"></div>
|
65 |
+
<div class="mac-btn green"></div>
|
66 |
+
<span class="ms-2" style="color: black;">Document Genie V 1.0</span>
|
67 |
+
</div>
|
68 |
+
<pre id="output"></pre>
|
69 |
+
<div class="chat-input">
|
70 |
+
<hr>
|
71 |
+
<form id="topic-form" class="mt-4">
|
72 |
+
<div class="form-group">
|
73 |
+
<label for="topic">Enter a Question About Your Document(s):</label>
|
74 |
+
<input type="text" class="form-control" id="topic" name="topic" required>
|
75 |
+
</div>
|
76 |
+
<div class="text-center">
|
77 |
+
<button type="submit" class="btn btn-primary">Generate Section</button>
|
78 |
+
<button id="clear-document-btn" class="btn btn-danger ml-2">Clear Document</button>
|
79 |
+
</div>
|
80 |
+
</form>
|
81 |
+
</div>
|
82 |
+
</div>
|
83 |
+
|
84 |
+
<div class="document-view">
|
85 |
+
<p><strong>📄 Document View</strong></p>
|
86 |
+
<hr>
|
87 |
+
<ul>
|
88 |
+
{% for file in loaded_files %}
|
89 |
+
<li>{{ file }}</li>
|
90 |
+
{% endfor %}
|
91 |
+
</ul>
|
92 |
+
</div>
|
93 |
+
</div>
|
94 |
+
</div>
|
95 |
+
|
96 |
+
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
|
97 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
|
98 |
+
<!-- Bootstrap JS and jQuery -->
|
99 |
+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
|
100 |
+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
|
101 |
+
<!-- Our custom JavaScript -->
|
102 |
+
<script src="/static/main.js"></script>
|
103 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
|
104 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/components/prism-python.min.js"></script>
|
105 |
+
<script>
|
106 |
+
// Fetch the models in use
|
107 |
+
fetch('/get-models')
|
108 |
+
.then(response => response.json())
|
109 |
+
.then(data => {
|
110 |
+
var embeddingModelElement = document.getElementById('embedding-model');
|
111 |
+
var generationModelElement = document.getElementById('generation-model');
|
112 |
+
embeddingModelElement.textContent = data.embedding_model;
|
113 |
+
generationModelElement.textContent = data.generation_model;
|
114 |
+
});
|
115 |
+
</script>
|
116 |
+
<script>
|
117 |
+
|
118 |
+
</script>
|
119 |
+
</body>
|
120 |
+
|
121 |
+
</html>
|
webui_app.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This is the app file which will be run to start the web app. It is responsible for creating the Flask app and registering the routes.
|
2 |
+
# It also contains the main function which will be run when the app is started. The main function will load the documents and models and then start the app.
|
3 |
+
# The app will be run in debug mode so that it will automatically reload when changes are made to the code. This is useful for development, but should not be used in production.
|
4 |
+
|
5 |
+
from routes import main_routes
|
6 |
+
import google.generativeai as palm
|
7 |
+
from flask import Flask, request, render_template, jsonify
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
app.secret_key = 'c6beb68cf8cda747fc0e6b13812b1c65'
|
11 |
+
|
12 |
+
main_routes.register_routes(app)
|
13 |
+
|
14 |
+
if __name__ == "__main__":
|
15 |
+
app.run(debug=True)
|