Spaces:
Runtime error
Runtime error
Commit
β’
eae3ee1
1
Parent(s):
c04acd2
π initial commit
Browse files- README.md +2 -2
- app.py +49 -0
- requirements.txt +7 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
-
title: Langchain
|
3 |
-
emoji:
|
4 |
colorFrom: blue
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
+
title: Langchain PFF QA
|
3 |
+
emoji: π
|
4 |
colorFrom: blue
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.document_loaders import PyPDFLoader
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from langchain.chains import RetrievalQA
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
+
|
9 |
+
embeddings = HuggingFaceEmbeddings()
|
10 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
11 |
+
|
12 |
+
def chat(openai_key,message, history,file):
|
13 |
+
history = history or []
|
14 |
+
|
15 |
+
loader = PyPDFLoader(file.name)
|
16 |
+
documents = loader.load()
|
17 |
+
|
18 |
+
texts = text_splitter.split_documents(documents)
|
19 |
+
vectordb = Chroma.from_documents(texts, embeddings)
|
20 |
+
|
21 |
+
qa = RetrievalQA.from_chain_type(llm=ChatOpenAI(temperature=0,model_name="gpt-3.5-turbo",openai_api_key=openai_key), chain_type="stuff", retriever=vectordb.as_retriever())
|
22 |
+
answer = qa.run(message)
|
23 |
+
|
24 |
+
history.append((message, answer))
|
25 |
+
|
26 |
+
return history, history
|
27 |
+
|
28 |
+
|
29 |
+
inputs = [
|
30 |
+
gr.Textbox(lines=1, label="OpenAI API Key",placeholder="openai api here...", type="password"),
|
31 |
+
gr.Textbox(lines=2,label="prompt"),
|
32 |
+
gr.State([]),
|
33 |
+
gr.File(label="PDF File")
|
34 |
+
]
|
35 |
+
|
36 |
+
outputs = [
|
37 |
+
gr.Chatbot(label="Chatbot"),
|
38 |
+
gr.State([])
|
39 |
+
]
|
40 |
+
|
41 |
+
app = gr.Interface(
|
42 |
+
fn=chat,
|
43 |
+
inputs=inputs,
|
44 |
+
outputs=outputs,
|
45 |
+
allow_flagging='never',
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__ == '__main__':
|
49 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.0.235
|
2 |
+
openai==0.27.8
|
3 |
+
transformers==4.25.1
|
4 |
+
sentence-transformers==2.2.2
|
5 |
+
pypdf==3.12.2
|
6 |
+
pycryptodome==3.18.0
|
7 |
+
chromadb==0.4.0
|