Commit
·
a7aa0eb
1
Parent(s):
d64fc58
update summary rev
Browse files
app.py
CHANGED
|
@@ -29,6 +29,9 @@ def summary(self):
|
|
| 29 |
avg_doc_length = sum(len(doc) for doc in self.documents) / num_documents
|
| 30 |
return f"Number of documents: {num_documents}, Average document length: {avg_doc_length}"
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
# PDF summary and query using stuffing
|
| 33 |
def pdf_changes(pdf_doc):
|
| 34 |
try:
|
|
@@ -54,6 +57,8 @@ def pdf_changes(pdf_doc):
|
|
| 54 |
# Generate summary using StuffDocumentsChain
|
| 55 |
global full_summary
|
| 56 |
full_summary = stuff_chain.run(documents)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Other existing logic for Chroma, embeddings, and retrieval
|
| 59 |
embeddings = OpenAIEmbeddings()
|
|
@@ -116,23 +121,23 @@ def infer(question, history):
|
|
| 116 |
return f"Error querying chatbot: {str(e)}"
|
| 117 |
|
| 118 |
def auto_clear_data():
|
| 119 |
-
|
| 120 |
-
|
| 121 |
qa = None
|
| 122 |
db = None
|
| 123 |
print("Data cleared successfully.") # Logging
|
| 124 |
-
|
| 125 |
def periodic_clear():
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
threading.Thread(target=periodic_clear).start()
|
| 131 |
-
|
| 132 |
css = """
|
| 133 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
| 134 |
"""
|
| 135 |
-
|
| 136 |
title = """
|
| 137 |
<div style="text-align: center;max-width: 700px;">
|
| 138 |
<h1>CauseWriter Chat with PDF • OpenAI</h1>
|
|
@@ -141,52 +146,39 @@ title = """
|
|
| 141 |
This version is set to erase chat history automatically after page timeout and uses OpenAI.</p>
|
| 142 |
</div>
|
| 143 |
"""
|
| 144 |
-
|
| 145 |
-
last_interaction_time = 0
|
| 146 |
-
full_summary = "" # Added global full_summary
|
| 147 |
-
|
| 148 |
-
def update_summary_box():
|
| 149 |
-
global full_summary
|
| 150 |
-
return {"summary_box": full_summary}
|
| 151 |
-
|
| 152 |
with gr.Blocks(css=css) as demo:
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
summary_box = gr.Textbox(
|
| 165 |
-
label="Document Summary",
|
| 166 |
-
placeholder="Summary will appear here.",
|
| 167 |
-
interactive=False,
|
| 168 |
-
rows=5,
|
| 169 |
-
elem_id="summary_box" # Set the elem_id to match the state key
|
| 170 |
)
|
| 171 |
-
|
| 172 |
|
| 173 |
-
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=450)
|
| 174 |
-
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
| 175 |
-
submit_btn = gr.Button("Send Message")
|
| 176 |
-
|
| 177 |
-
load_pdf.click(loading_pdf, None, langchain_status, queue=False)
|
| 178 |
-
load_pdf.click(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False).then(
|
| 179 |
-
update_summary_box
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
# Then update the summary_box
|
| 184 |
-
clear_btn.click(clear_data, outputs=[langchain_status], queue=False)
|
| 185 |
-
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
| 186 |
-
bot, chatbot, chatbot
|
| 187 |
-
)
|
| 188 |
-
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
|
| 189 |
-
bot, chatbot, chatbot
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
demo.launch()
|
|
|
|
| 29 |
avg_doc_length = sum(len(doc) for doc in self.documents) / num_documents
|
| 30 |
return f"Number of documents: {num_documents}, Average document length: {avg_doc_length}"
|
| 31 |
|
| 32 |
+
# Gradio state
|
| 33 |
+
summary_state = gr.State(initial_value="pending")
|
| 34 |
+
|
| 35 |
# PDF summary and query using stuffing
|
| 36 |
def pdf_changes(pdf_doc):
|
| 37 |
try:
|
|
|
|
| 57 |
# Generate summary using StuffDocumentsChain
|
| 58 |
global full_summary
|
| 59 |
full_summary = stuff_chain.run(documents)
|
| 60 |
+
# Update the state variable
|
| 61 |
+
return {summary_state: full_summary}
|
| 62 |
|
| 63 |
# Other existing logic for Chroma, embeddings, and retrieval
|
| 64 |
embeddings = OpenAIEmbeddings()
|
|
|
|
| 121 |
return f"Error querying chatbot: {str(e)}"
|
| 122 |
|
| 123 |
def auto_clear_data():
|
| 124 |
+
global qa, db, last_interaction_time
|
| 125 |
+
if time.time() - last_interaction_time > 1000:
|
| 126 |
qa = None
|
| 127 |
db = None
|
| 128 |
print("Data cleared successfully.") # Logging
|
| 129 |
+
|
| 130 |
def periodic_clear():
|
| 131 |
+
while True:
|
| 132 |
+
auto_clear_data()
|
| 133 |
+
time.sleep(1000)
|
| 134 |
+
|
| 135 |
threading.Thread(target=periodic_clear).start()
|
| 136 |
+
|
| 137 |
css = """
|
| 138 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
| 139 |
"""
|
| 140 |
+
|
| 141 |
title = """
|
| 142 |
<div style="text-align: center;max-width: 700px;">
|
| 143 |
<h1>CauseWriter Chat with PDF • OpenAI</h1>
|
|
|
|
| 146 |
This version is set to erase chat history automatically after page timeout and uses OpenAI.</p>
|
| 147 |
</div>
|
| 148 |
"""
|
| 149 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
with gr.Blocks(css=css) as demo:
|
| 151 |
+
with gr.Column(elem_id="col-container"):
|
| 152 |
+
gr.HTML(title)
|
| 153 |
+
|
| 154 |
+
with gr.Column():
|
| 155 |
+
pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
|
| 156 |
+
with gr.Row():
|
| 157 |
+
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
| 158 |
+
load_pdf = gr.Button("Convert PDF to Magic AI language")
|
| 159 |
+
clear_btn = gr.Button("Clear Data")
|
| 160 |
+
|
| 161 |
+
# New Textbox to display summary
|
| 162 |
+
summary_box = gr.Textbox(
|
| 163 |
+
label="Document Summary",
|
| 164 |
+
placeholder="Summary will appear here.",
|
| 165 |
+
interactive=False,
|
| 166 |
+
rows=5,
|
| 167 |
+
elem_id="summary_box" # Set the elem_id to match the state key
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=450)
|
| 171 |
+
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
| 172 |
+
submit_btn = gr.Button("Send Message")
|
| 173 |
|
| 174 |
+
# Step 2 and 3: Put the State object as an input and output
|
| 175 |
+
load_pdf.click(pdf_changes, inputs=[pdf_doc, summary_state], outputs=[langchain_status, summary_state])
|
| 176 |
+
clear_btn.click(clear_data, outputs=[langchain_status])
|
| 177 |
+
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
| 178 |
+
bot, chatbot, chatbot
|
| 179 |
+
)
|
| 180 |
+
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
|
| 181 |
+
bot, chatbot, chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
)
|
|
|
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
demo.launch()
|