Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -47,8 +47,8 @@ def infer(question):
|
|
47 |
|
48 |
return result
|
49 |
|
50 |
-
|
51 |
-
|
52 |
#col-container {
|
53 |
max-width: 700px;
|
54 |
margin-left: auto;
|
@@ -81,19 +81,14 @@ css="""
|
|
81 |
height: 350px;
|
82 |
border: 1px solid #ccc;
|
83 |
padding: 10px;
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
}
|
89 |
-
|
90 |
-
.submit-btn {
|
91 |
-
margin-bottom: 10px;
|
92 |
}
|
93 |
|
94 |
.chatbot .message {
|
95 |
color: #000;
|
96 |
-
font-size: 16px;
|
97 |
}
|
98 |
|
99 |
.chatbot .user-message {
|
@@ -105,37 +100,70 @@ css="""
|
|
105 |
}
|
106 |
"""
|
107 |
|
108 |
-
|
|
|
109 |
<div style="text-align: center;max-width: 700px;">
|
110 |
<h1>Chat with PDF</h1>
|
111 |
<p style="text-align: center;">Upload a .pdf from local machine, click the "Load PDF🚀" button, <br />
|
112 |
When ready, you are all set to start asking questions from the pdf</p>
|
113 |
-
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
|
|
|
|
115 |
|
|
|
|
|
116 |
|
|
|
117 |
with gr.Blocks(css=css) as demo:
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
)
|
140 |
-
|
141 |
-
demo.launch(True)
|
|
|
47 |
|
48 |
return result
|
49 |
|
50 |
+
# CSS
|
51 |
+
css = """
|
52 |
#col-container {
|
53 |
max-width: 700px;
|
54 |
margin-left: auto;
|
|
|
81 |
height: 350px;
|
82 |
border: 1px solid #ccc;
|
83 |
padding: 10px;
|
84 |
+
background-color: #fff;
|
85 |
+
font-family: sans-serif;
|
86 |
+
font-size: 16px;
|
87 |
+
line-height: 24px;
|
|
|
|
|
|
|
|
|
88 |
}
|
89 |
|
90 |
.chatbot .message {
|
91 |
color: #000;
|
|
|
92 |
}
|
93 |
|
94 |
.chatbot .user-message {
|
|
|
100 |
}
|
101 |
"""
|
102 |
|
103 |
+
# HTML
|
104 |
+
title = """
|
105 |
<div style="text-align: center;max-width: 700px;">
|
106 |
<h1>Chat with PDF</h1>
|
107 |
<p style="text-align: center;">Upload a .pdf from local machine, click the "Load PDF🚀" button, <br />
|
108 |
When ready, you are all set to start asking questions from the pdf</p>
|
109 |
+
</div>
|
110 |
+
"""
|
111 |
+
|
112 |
+
|
113 |
+
def pdf_changes(inputs, outputs):
|
114 |
+
"""Updates the text of the langchain status textbox."""
|
115 |
+
pdf_doc = inputs["pdf_doc"]
|
116 |
+
repo_id = inputs["repo_id"]
|
117 |
+
|
118 |
+
langchain_status.update_text(f"Loading PDF to langchain...")
|
119 |
+
|
120 |
+
# Load the PDF to langchain
|
121 |
+
langchain.load_pdf(pdf_doc, repo_id)
|
122 |
+
|
123 |
+
langchain_status.update_text(f"PDF loaded to langchain.")
|
124 |
+
|
125 |
+
|
126 |
+
def add_text(inputs, outputs):
|
127 |
+
"""Adds text to the chatbot."""
|
128 |
+
chatbot = inputs["chatbot"]
|
129 |
+
question = inputs["question"]
|
130 |
+
|
131 |
+
# Add the user question to the chatbot
|
132 |
+
chatbot.add_user_message(question)
|
133 |
+
|
134 |
+
# Get the response from the langchain chatbot
|
135 |
+
response = langchain.get_response(question)
|
136 |
+
|
137 |
+
# Add the bot response to the chatbot
|
138 |
+
chatbot.add_bot_message(response)
|
139 |
+
|
140 |
|
141 |
+
# Create the chatbot
|
142 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
143 |
|
144 |
+
# Create the langchain object
|
145 |
+
langchain = gr.Langchain()
|
146 |
|
147 |
+
# Create the UI
|
148 |
with gr.Blocks(css=css) as demo:
|
149 |
+
with gr.Column(elem_id="col-container"):
|
150 |
+
gr.HTML(title)
|
151 |
+
|
152 |
+
with gr.Column():
|
153 |
+
pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
|
154 |
+
repo_id = gr.Dropdown(label="LLM", choices=["google/flan-ul2", "OpenAssistant/oasst-sft-1-pythia-12b", "bigscience/bloomz"], value="google/flan-ul2")
|
155 |
+
with gr.Row():
|
156 |
+
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
157 |
+
load_pdf = gr.Button("Load pdf to langchain")
|
158 |
+
chatbot.style(height=350)
|
159 |
+
|
160 |
+
question = gr.Textbox(label="Question", placeholder="Type your Question and hit Enter ")
|
161 |
+
submit_btn = gr.Button("Send message")
|
162 |
+
|
163 |
+
# Respond to events
|
164 |
+
load_pdf.click(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
165 |
+
question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
|
166 |
+
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
|
167 |
+
|
168 |
+
# Start the demo
|
169 |
+
demo.show(True)
|
|
|
|
|
|