salemamassi commited on
Commit
94232b5
·
1 Parent(s): a8ada2a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import tempfile
4
+ from langchain.document_loaders import UnstructuredPDFLoader
5
+ from langchain.indexes import VectorstoreIndexCreator
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.schema import AIMessage, HumanMessage
8
+ from langchain.vectorstores import FAISS
9
+ from langchain.embeddings import HuggingFaceEmbeddings
10
+ from langchain.text_splitter import CharacterTextSplitter
11
+ from langchain import HuggingFaceHub
12
+ import time
13
+
14
+ # Set your API keys
15
+ API_KEY = os.environ["HUGGINGFACEHUB_API_TOKEN"]
16
+
17
+ # Create a temporary upload directory
18
+ upload_dir = tempfile.mkdtemp()
19
+
20
+ # Define global variables for loaders and index
21
+ index = None
22
+
23
+ def load_file(pdf_file, progress=gr.Progress()):
24
+ global index
25
+ uploaded_pdf_path = os.path.join(upload_dir, pdf_file.name)
26
+ pdf_loader = UnstructuredPDFLoader(uploaded_pdf_path)
27
+ index = VectorstoreIndexCreator(
28
+ embedding=HuggingFaceEmbeddings(),
29
+ text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
30
+ ).from_loaders([pdf_loader])
31
+
32
+ def chat(message,history):
33
+ global index
34
+ history_langchain_format = []
35
+ for human, ai in history:
36
+ history_langchain_format.append(HumanMessage(content=human))
37
+ history_langchain_format.append(AIMessage(content=ai))
38
+ history_langchain_format.append(HumanMessage(content=message))
39
+ history_langchain_format.append(HumanMessage(content=message))
40
+ # Create the index (update index)
41
+ llm2 = HuggingFaceHub(repo_id="declare-lab/flan-alpaca-large", model_kwargs={"temperature": 0, "max_length": 512},API_KEY )
42
+ chain = RetrievalQA.from_chain_type(llm=llm2,
43
+ chain_type="stuff",
44
+ retriever=index.vectorstore.as_retriever(),
45
+ input_key="question")
46
+ # Perform question-answering on the uploaded PDF with the user's question
47
+ gpt_response = chain.run(message)
48
+ return gpt_response
49
+
50
+
51
+ # Create a Gradio interface for chat
52
+ chat_interface = gr.ChatInterface(
53
+ chat,
54
+ theme=gr.themes.Soft()
55
+ )
56
+
57
+
58
+ upload_interface = gr.Interface(
59
+ fn=load_file,
60
+ inputs=[
61
+ gr.File(label="Upload a PDF",file_types=["pdf"]),
62
+ ],
63
+ outputs="text",
64
+ title="PDF Question Answering",
65
+ description="Upload a PDF, enter a question, and get an answer from the model.",
66
+ theme=gr.themes.Soft()
67
+ )
68
+
69
+
70
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
71
+ with gr.Row():
72
+ with gr.Column(scale=1):
73
+ with gr.Row():
74
+ upload_file = gr.File(label="Upload a PDF",file_types=["pdf"])
75
+ with gr.Row():
76
+ upload_button = gr.Button(label="Upload a PDF")
77
+ with gr.Row():
78
+ text = gr.Textbox(label="Status")
79
+ def load_file(pdf_file):
80
+ global index
81
+ uploaded_pdf_path = os.path.join(upload_dir, pdf_file.name)
82
+ pdf_loader = UnstructuredPDFLoader(uploaded_pdf_path)
83
+ index = VectorstoreIndexCreator(
84
+ embedding=HuggingFaceEmbeddings(),
85
+ text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
86
+ ).from_loaders([pdf_loader])
87
+ return "DONE ✅"
88
+ upload_button.click(load_file, [upload_file], text)
89
+ with gr.Column(scale=2):
90
+ chat_interface = gr.ChatInterface(
91
+ chat,
92
+ theme=gr.themes.Soft()
93
+ )
94
+
95
+ demo.queue().launch(inline=False)