Skip to content

Anakintano/Knowledge-Graph-with-OpenAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

streamlit Knowledge Graph Generator

A Colab-based Streamlit application that extracts graph data (entities and relationships) from text input using LangChain and OpenAI's GPT-4o models, then visualizes it interactively as a dynamic knowledge graph.

CleanShot 2025-05-28 at 13 11 46


πŸš€ Features

  • πŸ“‚ Input support for both .txt uploads and direct text entry
  • 🧠 Automatic entity and relationship extraction using GPT-4o
  • πŸ•ΈοΈ Interactive knowledge graph with physics-based layout
  • πŸ”„ Real-time drag, zoom, and hover interactivity with PyVis
  • 🌐 Fully deployable via Streamlit within Colab using %%writefile

βš™οΈ Installation

βœ… Prerequisites

  • Python 3.8+
  • OpenAI API key

πŸ“¦ Dependencies

The following Python packages are required:

  • langchain >= 0.1.0
  • langchain-experimental >= 0.0.45
  • langchain-openai >= 0.1.0
  • python-dotenv >= 1.0.0
  • pyvis >= 0.3.2
  • streamlit >= 1.32.0

Install all dependencies using:

pip install -r requirements.txt

In Colab, you can also run:

!pip install -r requirements.txt

πŸ” API Key Setup

Create a .env file in the notebook with:

%%writefile .env
OPENAI_API_KEY=your_openai_api_key_here

Or set it dynamically in the notebook:

import os
os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"

▢️ Running the App in Colab

Since this app uses %%writefile to generate scripts:

  1. Run each notebook cell to write required files like app.py
  2. Launch the Streamlit app using:
!streamlit run app.py

Colab will give you a public URL to access the interface.


🧠 How to Use

  1. Select input method in the sidebar:

    • πŸ“„ Upload a .txt file, or
    • πŸ“ Enter text directly
  2. Click "Generate Knowledge Graph"

  3. Interact with the graph:

    • Drag nodes
    • Zoom in/out with mouse wheel
    • Hover to see details
    • Apply graph filters and layouts

πŸ§ͺ How It Works

  1. Your input text is sent through a LangChain GraphTransformer
  2. GPT-4o identifies (entity) - [relationship] β†’ (entity) triplets
  3. A NetworkX graph is built from those triplets
  4. PyVis visualizes the graph inside Streamlit

Example triplets:

(Isaac Newton) - [discovered] β†’ (Laws of Motion)  
(Laws of Motion) - [published in] β†’ (1687)

πŸ“ Project Structure

All scripts are generated via Colab using %%writefile:

β”œβ”€β”€ KnowledgeGraph.ipynb    # Main notebook
β”œβ”€β”€ app.py                  # Streamlit app
β”œβ”€β”€ utils.py                # Helper logic (if needed) (not in the project yet)
β”œβ”€β”€ .env                    # API key
└── requirements.txt        # Dependencies

πŸ§‘β€πŸ’» Author

Β© Aditya Saxena, 2025 Licensed under the MIT License. See: MIT License


πŸ“š Resources

About

πŸ” Turn raw text into structured, visual knowledge graphs using large language models like OpenAI GPT-4o. 🧠 The app extracts entities and their relationships, then renders them interactively using PyVis πŸ•ΈοΈ and Streamlit 🌐.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors