# Directions to use KGE | DSG

## Access the KGE-CLIP GitHub Repo [Here](https://github.com/dsgiitr/kge-clip/tree/main)!  :blue\_heart: | DSG, IITR

The usable apps are:

1. Text-Knowledge Graph Generation.
2. Image-Knowledge Graph Generation
3. Text & Image Vector Embedding & Knowledge Graph Embedding with TensorBoard

The guide on using the Text-Based Knowledge Graph Visualization App!  The app is built using Flask, Python libraries, and the REBEL model, it efficiently extracts knowledge from text and visualizes it as interactive graphs. Let's dive into how you can make the most of this tool.

<figure><img src="https://3558521670-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8mI5ieugJlfU4ng7eyP9%2Fuploads%2FEXl0nfvmX6cwy6g6TtdX%2Fimage.png?alt=media&#x26;token=ce6dfe71-d67e-4272-b2d8-13e6f854dec5" alt="" width="563"><figcaption><p>Flask Text-KG app</p></figcaption></figure>

### Getting Started

**Prerequisites**

Before you begin, make sure your environment is ready:

* **Python 3.7 or higher**
* **pip** (Python package installer)
* **Graphviz** for advanced graph visualization

**Installation Steps**

1. **Clone the Repository:** Fork the project repository and clone it to your local machine:

```shell
git clone https://github.com/dsgiitr/kge-clip.git
cd your-repo-name
```

1. **Set Up a Virtual Environment:** Activate a virtual environment to manage dependencies:
   * On Windows:

     ```shell
     python -m venv venv
     venv\Scripts\activate
     ```
   * On macOS/Linux:

     ```shell
     python3 -m venv venv
     source venv/bin/activate
     ```
2. **Install Dependencies:** Install the required Python packages:

```shell
pip install flask transformers torch pandas networkx matplotlib plotly graphviz
```

1. **Install Graphviz:** Ensure Graphviz is installed and added to your system path:
   * **Windows**: Download from the [Graphviz website](https://graphviz.org/download/).
   * **macOS/Linux**: Use Homebrew or your package manager:

     ```shell
     brew install graphviz
     ```

#### Running the Flask App

Once installed, follow these steps to run the app:

1. **Activate Virtual Environment** and set the Flask app environment variable:
   * On Windows:

     ```shell
     set FLASK_APP=app.py
     ```
   * On macOS/Linux:

     ```shell
     export FLASK_APP=app.py
     ```
2. **Start the Flask Server:**

   ```shell
   flask run
   ```
3. **Access the App:** Open `http://127.0.0.1:5000/` in your browser to start using the app.

#### How to Use the App

1. **Input Text:** Paste the text you want to analyze on the homepage.

<figure><img src="https://3558521670-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8mI5ieugJlfU4ng7eyP9%2Fuploads%2FeuyTXywucI3zHJJ9GFGV%2Fimage.png?alt=media&#x26;token=ae6527c8-546a-42cd-a71e-a14f46e80405" alt="" width="563"><figcaption><p>Enter the text to begin</p></figcaption></figure>

2. **Generate the Knowledge Graph:** Click "Generate Knowledge Graph" to process the text and generate visualizations.

<figure><img src="https://3558521670-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8mI5ieugJlfU4ng7eyP9%2Fuploads%2F3M3QBOp0pqKiB5Ff7Gjz%2Fimage.png?alt=media&#x26;token=6d6b6f47-384e-4767-aba2-c3cd22380952" alt="" width="563"><figcaption></figcaption></figure>

3. **View the Results:** Explore the generated knowledge graph on the result page.

<figure><img src="https://3558521670-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8mI5ieugJlfU4ng7eyP9%2Fuploads%2FWb3RcPDY7uB8jSxOQ7zZ%2Fimage.png?alt=media&#x26;token=a9a12873-4333-4034-b942-4d651ca6047f" alt="" width="563"><figcaption><p>Knowledge graph using Plotly</p></figcaption></figure>

#### How It Works

* **Text Processing:** The app extracts triplets (subject-predicate-object) from the text using the REBEL model.
* **Graph Construction:** These triplets are used to build a knowledge graph with NetworkX.
* **Visualization:** The graph can be visualized in several ways:
  * **Static Graph:** Created with Matplotlib and NetworkX.
  * **Interactive Graph:** Built using Plotly, offering dynamic exploration.
  * **Hierarchical Layout:** Structured using Graphviz.

#### Troubleshooting Tips

Having trouble? Here are some quick fixes:

1. **Python Compatibility:** Ensure you're using Python 3.7+.
2. **Virtual Environment:** Verify your virtual environment is active.
3. **Graphviz Installation:** Make sure Graphviz is installed correctly and added to your PATH.
4. **Memory Issues:** For large texts, consider using a machine with more RAM.

#### <mark style="color:blue;">Moving on in the Gitbook, the project is explained in detailed from Problems in Traditional Vector Embeddings to Knowledge Graph Embeddings and its use in RAG-LLMs.</mark>
