arnavagrawal commited on
Commit
f7efcdd
1 Parent(s): 7c29546

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings.openai import OpenAIEmbeddings
2
+ from langchain.vectorstores import Chroma
3
+ from langchain.text_splitter import CharacterTextSplitter
4
+ from langchain.chains.question_answering import load_qa_chain
5
+ from langchain.llms import OpenAI
6
+ import os
7
+ from glob import glob
8
+ import shutil
9
+ import gradio as gr
10
+
11
+ git.Clone("https://github.com/TheMITTech/shakespeare", "shakespeare")
12
+ files = glob("./shakespeare/**/*.html")
13
+ os.mkdir('./data')
14
+ destination_folder = './data/'
15
+
16
+ for html_file in files:
17
+ shutil.move(html_file, destination_folder + html_file.split("/")[-1])
18
+
19
+ from langchain.document_loaders import BSHTMLLoader, DirectoryLoader
20
+
21
+ bshtml_dir_loader = DirectoryLoader('./data/', loader_cls=BSHTMLLoader)
22
+
23
+ data = bshtml_dir_loader.load()
24
+
25
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
26
+
27
+ text_splitter = RecursiveCharacterTextSplitter(
28
+ chunk_size = 1000,
29
+ chunk_overlap = 20,
30
+ length_function = len,
31
+ )
32
+
33
+ documents = text_splitter.split_documents(data)
34
+
35
+ from langchain.embeddings.openai import OpenAIEmbeddings
36
+
37
+ embeddings = OpenAIEmbeddings()
38
+ from chromadb.segment.impl.vector.local_hnsw import HnswParams
39
+ from langchain.vectorstores import Chroma
40
+
41
+ persist_directory = "vector_db"
42
+
43
+ vectordb = Chroma.from_documents(documents=documents, embedding=embeddings, persist_directory=persist_directory)
44
+
45
+ vectordb.persist()
46
+ vectordb = None
47
+
48
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
49
+
50
+ from langchain.chat_models import ChatOpenAI
51
+
52
+ llm = ChatOpenAI(temperature=0, model="gpt-4")
53
+
54
+ doc_retriever = vectordb.as_retriever()
55
+ from langchain.chains import RetrievalQA
56
+
57
+ shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever)
58
+
59
+ from langchain.utilities import SerpAPIWrapper
60
+
61
+ search = SerpAPIWrapper()
62
+
63
+ from langchain.agents import initialize_agent, Tool
64
+ from langchain.agents import AgentType
65
+ from langchain.tools import BaseTool
66
+ from langchain.llms import OpenAI
67
+ from langchain import LLMMathChain, SerpAPIWrapper
68
+
69
+ tools = [
70
+ Tool(
71
+ name = "Shakespeare QA System",
72
+ func=shakespeare_qa.run,
73
+ description="useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question."
74
+ ),
75
+ Tool(
76
+ name = "SERP API Search",
77
+ func=search.run,
78
+ description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
79
+ ),
80
+ ]
81
+
82
+ agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
83
+
84
+
85
+ def make_inference(query):
86
+ return(agent.run(query))
87
+
88
+ if __name__ == "__main__":
89
+ gr.Interface(
90
+ make_inference,
91
+ [
92
+ gr.inputs.Textbox(lines=2, label="Query"),
93
+ ],
94
+ gr.outputs.Textbox(label="Response"),
95
+ title="Shakespeare",
96
+ description="Shakespeare is a tool that allows you to ask questions about Shakespeare.",
97
+ ).launch()