Spaces:
Runtime error
Runtime error
deepozzzie
commited on
Commit
·
6ec818e
1
Parent(s):
b63876a
fixed pdf
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ from langchain.chains import ConversationalRetrievalChain
|
|
19 |
def loading_pdf():
|
20 |
return "Loading..."
|
21 |
|
22 |
-
def pdf_changes(
|
23 |
if openai_key is not None:
|
24 |
os.environ['OPENAI_API_KEY'] = open_ai_key
|
25 |
loader = OnlinePDFLoader(pdf_doc.name)
|
@@ -29,10 +29,10 @@ def pdf_changes(pdf_doc, open_ai_key):
|
|
29 |
embeddings = OpenAIEmbeddings()
|
30 |
db = Chroma.from_documents(texts, embeddings)
|
31 |
retriever = db.as_retriever()
|
32 |
-
global qa
|
33 |
qa = ConversationalRetrievalChain.from_llm(
|
34 |
-
llm=OpenAI(temperature=0.5),
|
35 |
-
retriever=retriever,
|
36 |
return_source_documents=False)
|
37 |
return "Ready"
|
38 |
else:
|
@@ -45,20 +45,20 @@ def add_text(history, text):
|
|
45 |
def bot(history):
|
46 |
response = infer(history[-1][0], history)
|
47 |
history[-1][1] = ""
|
48 |
-
|
49 |
-
for character in response:
|
50 |
history[-1][1] += character
|
51 |
time.sleep(0.05)
|
52 |
yield history
|
53 |
-
|
54 |
|
55 |
def infer(question, history):
|
56 |
-
|
57 |
res = []
|
58 |
for human, ai in history[:-1]:
|
59 |
pair = (human, ai)
|
60 |
res.append(pair)
|
61 |
-
|
62 |
chat_history = res
|
63 |
#print(chat_history)
|
64 |
query = question
|
@@ -83,18 +83,18 @@ title = """
|
|
83 |
with gr.Blocks(css=css) as demo:
|
84 |
with gr.Column(elem_id="col-container"):
|
85 |
gr.HTML(title)
|
86 |
-
|
87 |
with gr.Column():
|
88 |
openai_key = gr.Textbox(label="You OpenAI API key", type="password")
|
89 |
pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
|
90 |
with gr.Row():
|
91 |
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
92 |
load_pdf = gr.Button("Load pdf")
|
93 |
-
|
94 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
95 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
96 |
submit_btn = gr.Button("Send Message")
|
97 |
-
load_pdf.click(loading_pdf, None, langchain_status, queue=False)
|
98 |
load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
|
99 |
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
100 |
bot, chatbot, chatbot
|
@@ -102,4 +102,4 @@ with gr.Blocks(css=css) as demo:
|
|
102 |
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
|
103 |
bot, chatbot, chatbot)
|
104 |
|
105 |
-
demo.launch()
|
|
|
19 |
def loading_pdf():
|
20 |
return "Loading..."
|
21 |
|
22 |
+
def pdf_changes("https://drive.google.com/file/d/1rXzpCHuwofyvTPDwciSn_x4J2b3-q-HB/view?usp=share_link", open_ai_key):
|
23 |
if openai_key is not None:
|
24 |
os.environ['OPENAI_API_KEY'] = open_ai_key
|
25 |
loader = OnlinePDFLoader(pdf_doc.name)
|
|
|
29 |
embeddings = OpenAIEmbeddings()
|
30 |
db = Chroma.from_documents(texts, embeddings)
|
31 |
retriever = db.as_retriever()
|
32 |
+
global qa
|
33 |
qa = ConversationalRetrievalChain.from_llm(
|
34 |
+
llm=OpenAI(temperature=0.5),
|
35 |
+
retriever=retriever,
|
36 |
return_source_documents=False)
|
37 |
return "Ready"
|
38 |
else:
|
|
|
45 |
def bot(history):
|
46 |
response = infer(history[-1][0], history)
|
47 |
history[-1][1] = ""
|
48 |
+
|
49 |
+
for character in response:
|
50 |
history[-1][1] += character
|
51 |
time.sleep(0.05)
|
52 |
yield history
|
53 |
+
|
54 |
|
55 |
def infer(question, history):
|
56 |
+
|
57 |
res = []
|
58 |
for human, ai in history[:-1]:
|
59 |
pair = (human, ai)
|
60 |
res.append(pair)
|
61 |
+
|
62 |
chat_history = res
|
63 |
#print(chat_history)
|
64 |
query = question
|
|
|
83 |
with gr.Blocks(css=css) as demo:
|
84 |
with gr.Column(elem_id="col-container"):
|
85 |
gr.HTML(title)
|
86 |
+
|
87 |
with gr.Column():
|
88 |
openai_key = gr.Textbox(label="You OpenAI API key", type="password")
|
89 |
pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
|
90 |
with gr.Row():
|
91 |
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
92 |
load_pdf = gr.Button("Load pdf")
|
93 |
+
|
94 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
95 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
96 |
submit_btn = gr.Button("Send Message")
|
97 |
+
load_pdf.click(loading_pdf, None, langchain_status, queue=False)
|
98 |
load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
|
99 |
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
100 |
bot, chatbot, chatbot
|
|
|
102 |
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
|
103 |
bot, chatbot, chatbot)
|
104 |
|
105 |
+
demo.launch()
|