Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,82 +5,99 @@ from haystack.dataclasses import ChatMessage
|
|
5 |
import time
|
6 |
import os
|
7 |
|
8 |
-
def process_files_into_docs(files,progress=gr.Progress()):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
EXAMPLE_FILE = "RAG Survey.pdf"
|
43 |
|
44 |
-
with gr.Blocks(theme=gr.themes.Soft())as demo:
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
inputs=file_input,
|
69 |
-
|
70 |
-
|
71 |
-
)
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import time
|
6 |
import os
|
7 |
|
8 |
+
def process_files_into_docs(files, progress=gr.Progress()):
|
9 |
+
if isinstance(files, dict):
|
10 |
+
files = [files]
|
11 |
+
|
12 |
+
if not files:
|
13 |
+
return 'No file uploaded!'
|
14 |
+
|
15 |
+
preprocessing_pipeline.run({'file_type_router': {'sources': files}})
|
16 |
+
return "Database created🤗🤗"
|
17 |
+
|
18 |
+
def rag(history, question):
|
19 |
+
if history is None:
|
20 |
+
history = []
|
21 |
+
|
22 |
+
messages = [system_message, user_message]
|
23 |
+
res = conversational_rag.run(
|
24 |
+
data = {
|
25 |
+
'query_rephrase_prompt_builder' : {'query': question},
|
26 |
+
'prompt_builder': {'template': messages, 'query': question},
|
27 |
+
'memory_joiner': {'values': [ChatMessage.from_user(question)]}
|
28 |
+
},
|
29 |
+
include_outputs_from=['llm', 'query_rephrase_llm']
|
30 |
+
)
|
31 |
+
|
32 |
+
bot_message = res['llm']['replies'][0].content
|
33 |
+
streamed_message = ""
|
34 |
+
|
35 |
+
for token in bot_message.split():
|
36 |
+
streamed_message += f"{token} "
|
37 |
+
yield history + [(question, streamed_message.strip())], " "
|
38 |
+
time.sleep(0.05)
|
39 |
+
|
40 |
+
history.append((question, bot_message))
|
41 |
+
yield history, " "
|
42 |
|
43 |
EXAMPLE_FILE = "RAG Survey.pdf"
|
44 |
|
45 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
46 |
+
gr.HTML("<center><h1>TalkToFiles - Query your documents! 📂📄</h1></center>")
|
47 |
+
gr.Markdown("""##### This AI chatbot🤖 can help you chat with your documents. Can upload <b>Text(.txt), PDF(.pdf) and Markdown(.md)</b> files.
|
48 |
+
<b>Please do not upload confidential documents.</b>""")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column(scale=86):
|
52 |
+
gr.Markdown("""#### ***Step 1 - Upload Documents and Initialize RAG pipeline***</br> Can upload Multiple documents""")
|
53 |
+
|
54 |
+
with gr.Row():
|
55 |
+
file_input = gr.File(
|
56 |
+
label='Upload Files',
|
57 |
+
file_count='multiple',
|
58 |
+
file_types=['.pdf', '.txt', '.md'],
|
59 |
+
interactive=True
|
60 |
+
)
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
process_files = gr.Button('Create Document store')
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
result = gr.Textbox(label="Document store", value='Document store not initialized')
|
67 |
+
|
68 |
+
# Pre-processing Events
|
69 |
+
process_files.click(
|
70 |
+
fn=process_files_into_docs,
|
71 |
inputs=file_input,
|
72 |
+
outputs=result,
|
73 |
+
show_progress=True
|
74 |
+
)
|
75 |
+
|
76 |
+
def load_example():
|
77 |
+
return [EXAMPLE_FILE]
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
gr.Examples(
|
81 |
+
examples=[[EXAMPLE_FILE]],
|
82 |
+
inputs=file_input,
|
83 |
+
examples_per_page=1,
|
84 |
+
label="Click to upload an example"
|
85 |
+
).dataset.click(fn=load_example, inputs=[], outputs=file_input)
|
86 |
+
|
87 |
+
with gr.Column(scale=200):
|
88 |
+
gr.Markdown("""#### ***Step 2 - Chat with your docs*** """)
|
89 |
+
chatbot = gr.Chatbot(label='ChatBot', type="messages") # <-- Added type="messages" to fix deprecation
|
90 |
+
user_input = gr.Textbox(label='Enter your query', placeholder='Type here...')
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
submit_button = gr.Button("Submit")
|
94 |
+
clear_btn = gr.ClearButton([user_input, chatbot], value='Clear')
|
95 |
+
|
96 |
+
submit_button.click(
|
97 |
+
rag,
|
98 |
+
inputs=[chatbot, user_input],
|
99 |
+
outputs=[chatbot, user_input]
|
100 |
+
)
|
101 |
+
|
102 |
+
# Use api_name=None to avoid API generation issues
|
103 |
+
demo.launch(api_name=None)
|