Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
from langchain import vectorstores
|
6 |
+
from langchain import chains
|
7 |
+
from langchain import llms
|
8 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
9 |
import gradio as gr
|
|
|
10 |
|
11 |
+
llm = llms.AI21(ai21_api_key='diNNQzvL40ZnBnEQkIBwNESWjtj792NG')
|
12 |
+
|
13 |
+
def pdf_qa(pdf, query):
|
14 |
+
if pdf is not None:
|
15 |
+
pdf_reader = PdfReader(pdf)
|
16 |
+
texts = ""
|
17 |
+
for page in pdf_reader.pages:
|
18 |
+
texts += page.extract_text()
|
19 |
+
text_splitter = CharacterTextSplitter(
|
20 |
+
separator="\n",
|
21 |
+
chunk_size=1000,
|
22 |
+
chunk_overlap=0
|
23 |
+
)
|
24 |
+
chunks = text_splitter.split_text(texts)
|
25 |
+
embeddings = HuggingFaceEmbeddings()
|
26 |
+
db = vectorstores.Chroma.from_texts(chunks, embeddings)
|
27 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 10})
|
28 |
+
qa = chains.ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever)
|
29 |
+
chat_history = []
|
30 |
+
if query:
|
31 |
+
result = qa({"question": query, "chat_history": chat_history})
|
32 |
+
return result["answer"]
|
33 |
+
return "Please upload a PDF and enter a query."
|
34 |
+
|
35 |
+
pdf_input = gr.inputs.File(label="Upload your PDF", type="file", file_count="single")
|
36 |
+
query_input = gr.inputs.Textbox(label="Ask a question in PDF")
|
37 |
+
output = gr.outputs.Textbox(label="Answer")
|
38 |
+
|
39 |
+
gr.Interface(fn=pdf_qa, inputs=[pdf_input, query_input], outputs=output, title="PDF QA").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|