Upload 4 files
Browse files
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GOOGLE_API_KEY = "AIzaSyBMWuP_369qtz_9d9IIe09MLf0z1O9hSxM"
|
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 3 |
+
import tempfile
|
| 4 |
+
from bd import get_response
|
| 5 |
+
|
| 6 |
+
def main():
|
| 7 |
+
st.title("Chat with CSV")
|
| 8 |
+
|
| 9 |
+
uploaded_file = st.sidebar.file_uploader("choose your csv file",type='csv')
|
| 10 |
+
|
| 11 |
+
if uploaded_file is not None:
|
| 12 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 13 |
+
temp_file.write(uploaded_file.getvalue())
|
| 14 |
+
temp_file_path = temp_file.name
|
| 15 |
+
|
| 16 |
+
#Intializing CSVLoader
|
| 17 |
+
csv_loader = CSVLoader(file_path = temp_file_path, encoding = 'latin-1', csv_args={'delimiter':','})
|
| 18 |
+
|
| 19 |
+
#load csv data
|
| 20 |
+
data = csv_loader.load()
|
| 21 |
+
|
| 22 |
+
#input
|
| 23 |
+
user_ip = st.text_input("Enter Your Query/Message")
|
| 24 |
+
print(user_ip)
|
| 25 |
+
|
| 26 |
+
if user_ip:
|
| 27 |
+
response=get_response(data,user_ip)
|
| 28 |
+
st.write(response)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__" :
|
| 32 |
+
main()
|
bd.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 2 |
+
from langchain_community.vectorstores import Chroma
|
| 3 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 4 |
+
from langchain.prompts import PromptTemplate
|
| 5 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def list_available_models():
|
| 13 |
+
models = genai.models.list_models()
|
| 14 |
+
|
| 15 |
+
print("Available models:")
|
| 16 |
+
for model in models:
|
| 17 |
+
print(f"Name: {model.name}")
|
| 18 |
+
print(f"Description: {model.description}")
|
| 19 |
+
print(f"Supported methods: {', '.join(model.supported_methods)}")
|
| 20 |
+
print("\n")
|
| 21 |
+
|
| 22 |
+
def get_response(file, query):
|
| 23 |
+
# Load environment variables
|
| 24 |
+
load_dotenv()
|
| 25 |
+
|
| 26 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=400)
|
| 27 |
+
context = '\n\n'.join(str(p.page_content) for p in file)
|
| 28 |
+
data = text_splitter.split_text(context)
|
| 29 |
+
|
| 30 |
+
# Specify the correct model name based on your requirements
|
| 31 |
+
model_name = 'models/chat-bison-001'
|
| 32 |
+
|
| 33 |
+
# Specify the API key directly in the code
|
| 34 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 35 |
+
|
| 36 |
+
embeddings = GoogleGenerativeAIEmbeddings(model=model_name, google_api_key=google_api_key)
|
| 37 |
+
|
| 38 |
+
searcher = Chroma.from_texts(data, embeddings).as_retriever()
|
| 39 |
+
|
| 40 |
+
ques = 'Which country has maximum GDP?'
|
| 41 |
+
records = searcher.get_relevent_documents(ques)
|
| 42 |
+
|
| 43 |
+
prompt_template = """
|
| 44 |
+
You have to give the correct answer to the question from the provided context and make sure you give all details\n
|
| 45 |
+
Context: {context}\n
|
| 46 |
+
Question: {question}\n
|
| 47 |
+
|
| 48 |
+
Answer:
|
| 49 |
+
"""
|
| 50 |
+
prompt = PromptTemplate(template=prompt_template, input_variable=['context', 'question'])
|
| 51 |
+
|
| 52 |
+
model = ChatGoogleGenerativeAI(model=model_name, temperature=0.5)
|
| 53 |
+
|
| 54 |
+
chain = load_qa_chain(model, chain_type='stuff', prompt=prompt)
|
| 55 |
+
|
| 56 |
+
response = chain(
|
| 57 |
+
{
|
| 58 |
+
'input_document': records,
|
| 59 |
+
'question': query
|
| 60 |
+
},
|
| 61 |
+
return_only_output=True
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return response['output_text']
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain_google_genai
|
| 3 |
+
langchain-google-bigtable
|
| 4 |
+
streamlit
|
| 5 |
+
chromadb
|
| 6 |
+
langchain-community
|
| 7 |
+
python-dotenv
|
| 8 |
+
|