svsaurav95 commited on
Commit
a46bece
·
verified ·
1 Parent(s): 16227d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -1,5 +1,5 @@
1
- import pandas as pd
2
  import os
 
3
  import gradio as gr
4
  from langchain.document_loaders import CSVLoader
5
  from langchain.vectorstores import FAISS
@@ -7,36 +7,34 @@ from langchain.embeddings import HuggingFaceEmbeddings
7
  from langchain.chains import RetrievalQA
8
  from langchain_groq import ChatGroq
9
 
10
- # Set up your API key for ChatGroq
11
- os.environ["GROQ_API_KEY"] = "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" # Replace with your actual API key
12
 
13
- # Initialize the HuggingFace embeddings
14
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") # Lightweight embedding model
 
 
 
 
 
15
 
16
- # Instantiate the ChatGroq model
17
  llm = ChatGroq(
18
- model="mixtral-8x7b-32768", # Replace with your desired model
19
  temperature=0,
20
  max_tokens=None,
21
  timeout=None,
22
  max_retries=2
23
  )
24
 
25
- # Define the function to process the query and CSV
26
  def process_query(file, query):
27
  try:
28
- # Load the CSV as documents for retrieval
29
  loader = CSVLoader(file_path=file.name)
30
  documents = loader.load()
31
 
32
- # Create a FAISS vector store
33
  vector_store = FAISS.from_documents(documents, embeddings)
34
-
35
- # Create a retriever from the vector store
36
  retriever = vector_store.as_retriever()
37
-
38
- # Create a RetrievalQA pipeline
39
- qa_chain = RetrievalQA.from_chain_type(
40
  llm=llm,
41
  retriever=retriever,
42
  return_source_documents=True
@@ -52,20 +50,19 @@ def process_query(file, query):
52
  except Exception as e:
53
  return f"An error occurred: {str(e)}", ""
54
 
55
- # Create a Gradio interface
56
  interface = gr.Interface(
57
  fn=process_query,
58
  inputs=[
59
- gr.File(label="Upload CSV File"), # File input for the CSV
60
- gr.Textbox(label="Enter your query") # Text input for the query
61
  ],
62
  outputs=[
63
- gr.Textbox(label="Answer"), # Text output for the answer
64
- gr.Textbox(label="Source Documents") # Text output for the source documents
65
  ],
66
- title="CSV Query Assistant",
67
- description="Upload a CSV file and enter a query to retrieve relevant information."
68
  )
69
 
70
- # Launch the Gradio app
71
  interface.launch(share=True)
 
 
1
  import os
2
+ import pandas as pd
3
  import gradio as gr
4
  from langchain.document_loaders import CSVLoader
5
  from langchain.vectorstores import FAISS
 
7
  from langchain.chains import RetrievalQA
8
  from langchain_groq import ChatGroq
9
 
 
 
10
 
11
+ api_key = os.environ.get("GROQ_API_KEY")
12
+ if not api_key:
13
+ raise ValueError("Api key not found")
14
+
15
+ os.environ["GROQ_API_KEY"] = api_key
16
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
17
+
18
 
 
19
  llm = ChatGroq(
20
+ model="mixtral-8x7b-32768",
21
  temperature=0,
22
  max_tokens=None,
23
  timeout=None,
24
  max_retries=2
25
  )
26
 
27
+ # function to process query and CSV
28
  def process_query(file, query):
29
  try:
 
30
  loader = CSVLoader(file_path=file.name)
31
  documents = loader.load()
32
 
33
+ # FAISS vector store
34
  vector_store = FAISS.from_documents(documents, embeddings)
 
 
35
  retriever = vector_store.as_retriever()
36
+
37
+ qa_chain = RetrievalQA.from_chain_type( #RetrievalQA pipeline
 
38
  llm=llm,
39
  retriever=retriever,
40
  return_source_documents=True
 
50
  except Exception as e:
51
  return f"An error occurred: {str(e)}", ""
52
 
53
+ # Gradio interface
54
  interface = gr.Interface(
55
  fn=process_query,
56
  inputs=[
57
+ gr.File(label="Upload CSV File"),
58
+ gr.Textbox(label="Enter your query")
59
  ],
60
  outputs=[
61
+ gr.Textbox(label="Answer"),
62
+ gr.Textbox(label="Source Documents") #
63
  ],
64
+ title="DataScope.ai",
65
+ description="Upload & Unlock Insights from Your Data Ask, Query, Discover!"
66
  )
67
 
 
68
  interface.launch(share=True)