giridharnair01 commited on
Commit
acfb108
·
verified ·
1 Parent(s): 12c6df7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -5,38 +5,38 @@ from langchain.llms import HuggingFacePipeline
5
  from langchain.chains import RetrievalQA
6
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
7
  import json
8
- import os
9
 
10
- # Define JSON file path
11
- dataset_path = "/mnt/data/constitution_qa.json"
12
 
13
- # Load JSON dataset
14
- with open(dataset_path, "r", encoding="utf-8") as f:
15
- data = json.load(f)
16
 
17
- # Extract questions and answers
18
- texts = [f"Q: {item['question']}\nA: {item['answer']}" for item in data]
 
19
 
20
- # Load the embedding model
21
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
22
 
23
- # Create FAISS vector database
24
- vector_db = FAISS.from_texts(texts, embeddings)
25
 
26
- # Load Open-Source LLM (LLaMA-2 7B Open Chat Model)
27
- tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
28
- model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
29
- text_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
30
- llm = HuggingFacePipeline(pipeline=text_pipeline)
31
 
32
- # Create RAG pipeline
33
- qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vector_db.as_retriever())
 
 
 
34
 
35
- # Streamlit UI
36
- st.title("Indian Constitution Q&A RAG App")
37
- query = st.text_input("Enter your legal query:")
 
38
 
39
- if query:
40
- response = qa_chain.run(query)
41
- st.write("### AI-Generated Answer:")
42
- st.write(response)
 
5
  from langchain.chains import RetrievalQA
6
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
7
  import json
 
8
 
9
+ # Streamlit UI
10
+ st.title("Indian Constitution Q&A RAG App")
11
 
12
+ # Upload JSON File
13
+ uploaded_file = st.file_uploader("Upload Constitution JSON", type="json")
 
14
 
15
+ if uploaded_file is not None:
16
+ # Load JSON dataset
17
+ data = json.load(uploaded_file)
18
 
19
+ # Extract questions and answers
20
+ texts = [f"Q: {item['question']}\nA: {item['answer']}" for item in data]
21
 
22
+ # Load the embedding model
23
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
24
 
25
+ # Create FAISS vector database
26
+ vector_db = FAISS.from_texts(texts, embeddings)
 
 
 
27
 
28
+ # Load Open-Source LLM (LLaMA-2 7B Open Chat Model)
29
+ tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
30
+ model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
31
+ text_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
32
+ llm = HuggingFacePipeline(pipeline=text_pipeline)
33
 
34
+ # Create RAG pipeline
35
+ qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vector_db.as_retriever())
36
+
37
+ query = st.text_input("Enter your legal query:")
38
 
39
+ if query:
40
+ response = qa_chain.run(query)
41
+ st.write("### AI-Generated Answer:")
42
+ st.write(response)