rairo commited on
Commit
5f2f34f
·
verified ·
1 Parent(s): 5669c2a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -83
main.py CHANGED
@@ -3,101 +3,41 @@ import pandas as pd
3
 
4
  import io
5
  from flask import Flask, request
6
- from twilio.twiml.messaging_response import MessagingResponse
7
- from langchain.llms import GooglePalm
8
- import pandas as pd
9
- #from yolopandas import pd
10
- import os
11
- from langchain.embeddings import GooglePalmEmbeddings
12
- # a class to create a question answering system based on information retrieval
13
- from langchain.chains import RetrievalQA
14
- # a class for splitting text into fixed-sized chunks with an optional overlay
15
- from langchain.text_splitter import RecursiveCharacterTextSplitter
16
- # a class to create a vector index using FAISS, a library for approximate nearest neighbor search
17
- from langchain.vectorstores import FAISS
18
- # a class for loading PDF documents from a directory
19
- from langchain.document_loaders import PyPDFDirectoryLoader
20
  from langchain.chains.question_answering import load_qa_chain
21
- from langchain.chains import ConversationalRetrievalChain
22
- from langchain.schema.vectorstore import VectorStoreRetriever
23
- import google.generativeai
24
-
 
 
 
25
  from dotenv import load_dotenv
 
 
 
26
 
27
- load_dotenv()
28
-
29
-
30
-
31
-
32
- def get_pdf_text(pdf_docs):
33
- text=""
34
- for pdf in pdf_docs:
35
- pdf_reader= PdfReader(pdf)
36
- for page in pdf_reader.pages:
37
- text+= page.extract_text()
38
- return text
39
-
40
- # load PDF files from a directory
41
- loader = PyPDFDirectoryLoader("documents/")
42
- data = loader.load()
43
-
44
- # print the loaded data, which is a list of tuples (file name, text extracted from the PDF)
45
- #print(data)
46
-
47
- # split the extracted data into text chunks using the text_splitter, which splits the text based on the specified number of characters and overlap
48
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=20)
49
-
50
- text_chunks = text_splitter.split_documents(data)
51
-
52
- # print the number of chunks obtained
53
- #print(len(text_chunks))
54
-
55
- embeddings = GooglePalmEmbeddings(google_api_key=os.environ['PALM'])
56
 
57
- # create embeddings for each text chunk using the FAISS class, which creates a vector index using FAISS and allows efficient searches between vectors
58
- vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)
59
 
60
- #print(type(vector_store))
61
 
62
 
 
63
 
64
- def ask_pdfs(user_question):
 
65
  load_dotenv()
66
-
67
-
68
-
 
 
69
  llm = GooglePalm(temperature=0, google_api_key=os.environ['PALM'])
70
-
71
- # Create a question answering system based on information retrieval using the RetrievalQA class, which takes as input a neural language model, a chain type and a retriever (an object that allows you to retrieve the most relevant chunks of text for a query)
72
- retriever = VectorStoreRetriever(vectorstore=vector_store)
73
- qa = RetrievalQA.from_llm(llm=llm, retriever=retriever)
74
- response =qa.run(user_question)
75
- #print("Response:",response)
76
-
77
  return response
78
 
79
- app = Flask(__name__)
80
-
81
- @app.route("/", methods=["POST"])
82
- def whatsapp():
83
-
84
- # user input
85
- user_msg = request.values.get('Body', '').lower()
86
-
87
- # creating object of MessagingResponse
88
- response = MessagingResponse()
89
-
90
- print(user_msg)
91
-
92
- # User Query
93
- q = user_msg
94
-
95
- answer = ask_pdfs(q)
96
-
97
- response.message(answer)
98
-
99
- return str(response)
100
-
101
 
102
  if __name__ == "__main__":
103
  app.run(debug=True,host="0.0.0.0", port=7860)
 
3
 
4
  import io
5
  from flask import Flask, request
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from langchain.chains.question_answering import load_qa_chain
7
+ from langchain import PromptTemplate, LLMChain
8
+ from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
9
+ from langchain.chat_models import ChatOpenAI
10
+ from langchain.agents.agent_types import AgentType
11
+ from langchain.chat_models import ChatOpenAI
12
+ import pandas as pd
13
+ from langchain.llms import OpenAI
14
  from dotenv import load_dotenv
15
+ import google.generativeai as palm
16
+ from langchain.llms import GooglePalm
17
+ import json
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ from dotenv import load_dotenv
 
21
 
22
+ load_dotenv()
23
 
24
 
25
+ app = Flask(__name__)
26
 
27
+ @app.route("/predict", methods=["POST"])
28
+ def bot(json_table, user_question):
29
  load_dotenv()
30
+ #
31
+ df = pd.DataFrame(json_table)
32
+ #df['Profit'] = df['Profit'].apply(lambda x: "R{:.1f}".format((x)))
33
+ #df['Revenue'] = df['Revenue'].apply(lambda x: "R{:.1f}".format((x)))
34
+ #llm = ChatOpenAI(model_name='gpt-3.5-turbo-0613', temperature=0, openai_api_key=os.getenv('OPENAI_API_KEY'))
35
  llm = GooglePalm(temperature=0, google_api_key=os.environ['PALM'])
36
+ #agent = create_pandas_dataframe_agent(llm, df, verbose=True, agent_type=AgentType.OPENAI_FUNCTIONS)
37
+ agent = create_pandas_dataframe_agent(llm, df, agent="structured_chat-zero-shot-react-description", verbose=True)
38
+ response = agent.run(user_question)
 
 
 
 
39
  return response
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == "__main__":
43
  app.run(debug=True,host="0.0.0.0", port=7860)