vikramvasudevan commited on
Commit
949133f
·
verified ·
1 Parent(s): 89c441f

Upload folder using huggingface_hub

Browse files
Files changed (10) hide show
  1. .github/workflows/update_space.yml +28 -0
  2. .gitignore +11 -0
  3. .python-version +1 -0
  4. README.md +2 -8
  5. data/gita_data.csv +0 -0
  6. db.py +78 -0
  7. main.py +94 -0
  8. pyproject.toml +18 -0
  9. requirements.txt +10 -0
  10. uv.lock +0 -0
.github/workflows/update_space.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Run Python script
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout
14
+ uses: actions/checkout@v2
15
+
16
+ - name: Set up Python
17
+ uses: actions/setup-python@v2
18
+ with:
19
+ python-version: '3.9'
20
+
21
+ - name: Install Gradio
22
+ run: python -m pip install gradio
23
+
24
+ - name: Log in to Hugging Face
25
+ run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
26
+
27
+ - name: Deploy to Spaces
28
+ run: gradio deploy
.gitignore ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+ .env
9
+
10
+ # Virtual environments
11
+ .venv
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.12
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Bhagavat Gita Chat
3
- emoji: 😻
4
- colorFrom: pink
5
- colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.38.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Bhagavat_Gita_Chat
3
+ app_file: main.py
 
 
4
  sdk: gradio
5
  sdk_version: 5.38.0
 
 
6
  ---
 
 
data/gita_data.csv ADDED
The diff for this file is too large to render. See raw diff
 
db.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chromadb
2
+ from chromadb.config import Settings
3
+ import json
4
+ import csv
5
+ from langchain_community.document_loaders import PyPDFLoader
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain_community.embeddings import OpenAIEmbeddings
8
+
9
+
10
+ class MyDatabase:
11
+ def __init__(self):
12
+ # Settings(persist_directory="./chroma_db")
13
+ self.chroma_client = chromadb.Client()
14
+ self.initialize()
15
+
16
+ def get_collection(self):
17
+ return self.chroma_client.get_or_create_collection(name="bhagavat_gita")
18
+
19
+ def initialize(self):
20
+ print("Adding Data ...")
21
+ collection = self.get_collection()
22
+ # Read CSV data into a list of dictionaries
23
+ print("Loading Bhagavat Gita ...")
24
+ with open(
25
+ "./data/gita_data.csv", mode="r", newline="", encoding="utf-8"
26
+ ) as csvfile:
27
+ documents = list(csv.DictReader(csvfile))
28
+ # with open("./gita_data.json", "r") as f:
29
+ # documents = json.load(f)
30
+ with open("./data/gita_data_new.json", "w") as f:
31
+ json.dump(documents, f, indent=1)
32
+ collection.add(
33
+ documents=[document["translation"] for document in documents],
34
+ metadatas=[
35
+ {
36
+ "source": "bhagavat_gita",
37
+ "chapter_number": document["chapter_number"],
38
+ "verse_number": document["chapter_verse"],
39
+ }
40
+ for document in documents
41
+ ],
42
+ # [
43
+ # {"source": "article1"},
44
+ # {"source": "article2"},
45
+ # {"source": "article3"},
46
+ # ],
47
+ # ids=["doc1", "doc2", "doc3"],
48
+ ids=[f"doc{i}" for i, document in enumerate(documents)],
49
+ )
50
+
51
+ # print("Loading Vishnu Puranam ...")
52
+ # loader = PyPDFLoader("./data/vishnu_puranam.pdf")
53
+ # pdfDocument = loader.load()
54
+ # print("pdfDocument", pdfDocument)
55
+ # with open("./data/vishnu_puranam.json","w") as f:
56
+ # json.dump([doc.model_dump_json() for doc in pdfDocument], f, indent=1)
57
+
58
+ # text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
59
+ # chunked_documents = text_splitter.split_documents([pdfDocument])
60
+ # print(chunked_documents)
61
+ print("Added data ...")
62
+
63
+ def get_data(self, query: str = "is knowledge superior to action?"):
64
+ print("Querying data ...")
65
+ collection = self.get_collection()
66
+ results = collection.query(
67
+ query_texts=[
68
+ query,
69
+ ], # Chroma will embed this for you
70
+ n_results=5, # how many results to return
71
+ )
72
+ print(json.dumps(results, indent=2))
73
+ return results
74
+
75
+
76
+ # mydb = MyDatabase()
77
+ # mydb.initialize()
78
+ # mydb.get_data("What is karma?")
main.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import TypedDict, override
2
+ from langgraph.constants import END, START
3
+ from langgraph.graph.state import StateGraph
4
+ from typing_extensions import Annotated
5
+ from pydantic import BaseModel
6
+ from langgraph.graph.message import add_messages
7
+ import gradio as gr
8
+ from langchain_openai import ChatOpenAI
9
+ from dotenv import load_dotenv
10
+
11
+ from db import MyDatabase
12
+
13
+ load_dotenv(override=True)
14
+ mydb = MyDatabase()
15
+
16
+
17
+ class State(TypedDict):
18
+ messages: Annotated[list, add_messages]
19
+
20
+
21
+ graph_builder = StateGraph(State)
22
+
23
+ llm = ChatOpenAI(model="gpt-4o-mini")
24
+
25
+
26
+ def chatNode(state: State):
27
+ messages = state["messages"]
28
+ print("messages = ", messages)
29
+ responseMessage = llm.invoke(messages)
30
+ newState = State(messages=[responseMessage])
31
+ return newState
32
+
33
+
34
+ def encryptNode(state: State):
35
+ messages = state["messages"]
36
+ messages[-1].content += "\n--------- \n with love, \n##### Krishna"
37
+ newState = State(messages=messages)
38
+ return newState
39
+
40
+
41
+ graph_builder.add_node("MyChatNode", chatNode)
42
+ graph_builder.add_node("MyEncryptNode", encryptNode)
43
+ graph_builder.add_edge(START, "MyChatNode")
44
+ graph_builder.add_edge("MyChatNode", "MyEncryptNode")
45
+ graph_builder.add_edge("MyEncryptNode", END)
46
+ graph = graph_builder.compile()
47
+
48
+
49
+ def chat(message, history):
50
+ # Ensure history is a list of message dicts
51
+ relevant_sections = mydb.get_data(message)
52
+ if not history:
53
+ history = [
54
+ {
55
+ "role": "system",
56
+ "content": f"""You are a religious researcher, expert in Hindu literature like Bhagavat Gita.
57
+ User asks questions and you will answer from the context given below. it is important that you answer ONLY from the context given below and nowhere else.
58
+ In your response, mention which chapter and verses from which you came up with this explanation.
59
+ DO NOT talk about other spiritual traditions. Limit yourself to the context at all times.
60
+ organize your response under subheadings for clarity and keep it simple in terms of language and brief. Do not add your interpretation or additional commentary.
61
+ Answer any question in the context of Bhagavat Gita (particularly from the context given below). If you dont know the answer, just say so.
62
+
63
+ here is the context:
64
+ {relevant_sections}
65
+ """,
66
+ },
67
+ {
68
+ "role" : "assistant",
69
+ "content" : "Namaste, Ask me any questions on Bhagavat Gita!"
70
+ }
71
+ ]
72
+ initial_state = State(messages=history + [{"role": "user", "content": message}])
73
+ print("initial_state = ", initial_state)
74
+ response = graph.invoke(initial_state)
75
+ return response["messages"][-1].content
76
+
77
+
78
+ def main():
79
+ print("Hello from langgraph-demo!")
80
+ gr.ChatInterface(
81
+ chat,
82
+ type="messages",
83
+ title="Let's chat on Bhagavat Gita",
84
+ examples=[
85
+ "What does Gita say about Karma?",
86
+ "Why did God create this world?",
87
+ "What is the relationship between knowledge and action?",
88
+ "Who are friends and enemies per Gita?"
89
+ ],
90
+ ).launch()
91
+
92
+
93
+ if __name__ == "__main__":
94
+ main()
pyproject.toml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "langgraph-demo"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "chromadb>=1.0.15",
9
+ "dotenv>=0.9.9",
10
+ "gradio>=5.38.0",
11
+ "ipython>=9.4.0",
12
+ "langchain>=0.3.26",
13
+ "langchain-community>=0.3.27",
14
+ "langchain-openai>=0.3.28",
15
+ "langgraph>=0.5.3",
16
+ "pydantic>=2.11.7",
17
+ "pypdf>=5.8.0",
18
+ ]
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ chromadb
2
+ dotenv
3
+ gradio
4
+ ipython
5
+ langchain
6
+ langchain-community
7
+ langchain-openai
8
+ langgraph
9
+ pydantic
10
+ pypdf
uv.lock ADDED
The diff for this file is too large to render. See raw diff