Spaces:
Runtime error
Runtime error
iabualhaol
commited on
Commit
•
167d3d4
1
Parent(s):
946259b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.document_loaders import PyMuPDFLoader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.vectorstores import Chroma
|
6 |
+
from langchain.embeddings import OpenAIEmbeddings
|
7 |
+
from langchain.chat_models import ChatOpenAI
|
8 |
+
from langchain.chains import RetrievalQA
|
9 |
+
|
10 |
+
import os
|
11 |
+
os.environ['CURL_CA_BUNDLE'] = ''
|
12 |
+
# Initialize conversation history
|
13 |
+
conversation_history = ""
|
14 |
+
|
15 |
+
def main(api_key, pdf_path, user_input):
|
16 |
+
global conversation_history # Declare as global to update it
|
17 |
+
|
18 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
19 |
+
|
20 |
+
persist_directory = "./storage"
|
21 |
+
|
22 |
+
loader = PyMuPDFLoader(pdf_path.name)
|
23 |
+
documents = loader.load()
|
24 |
+
|
25 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=10)
|
26 |
+
texts = text_splitter.split_documents(documents)
|
27 |
+
|
28 |
+
embeddings = OpenAIEmbeddings()
|
29 |
+
vectordb = Chroma.from_documents(documents=texts,
|
30 |
+
embedding=embeddings,
|
31 |
+
persist_directory=persist_directory)
|
32 |
+
vectordb.persist()
|
33 |
+
|
34 |
+
retriever = vectordb.as_retriever(search_kwargs={"k": 3})
|
35 |
+
llm = ChatOpenAI(model_name='gpt-4')
|
36 |
+
|
37 |
+
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
|
38 |
+
|
39 |
+
# Update conversation history with the user's latest question
|
40 |
+
conversation_history += f"User: {user_input}\n"
|
41 |
+
|
42 |
+
query = f"{conversation_history}###Prompt {user_input}"
|
43 |
+
try:
|
44 |
+
llm_response = qa(query)
|
45 |
+
response_text = llm_response["result"]
|
46 |
+
|
47 |
+
# Update conversation history with the model's latest answer
|
48 |
+
conversation_history += f"Model: {response_text}\n"
|
49 |
+
|
50 |
+
return conversation_history # Return the entire conversation history
|
51 |
+
except Exception as err:
|
52 |
+
return f'Exception occurred. Please try again: {str(err)}'
|
53 |
+
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=main,
|
56 |
+
inputs=[
|
57 |
+
gr.inputs.Textbox(label="OpenAI API Key", type="password"),
|
58 |
+
gr.inputs.File(label="Upload PDF"),
|
59 |
+
gr.inputs.Textbox(label="Enter Query")
|
60 |
+
],
|
61 |
+
outputs="text",
|
62 |
+
live=False,
|
63 |
+
show_submit_button=True,
|
64 |
+
description="Enter your OpenAI API Key, upload a PDF, and enter a query to get a response."
|
65 |
+
)
|
66 |
+
iface.launch(share=True)
|