Dharma20 commited on
Commit
b474636
·
verified ·
1 Parent(s): dd64543

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from module import preprocessing_pipeline, conversational_rag
3
+ from module import system_message, user_message
4
+ from haystack.dataclasses import ChatMessage
5
+ import time
6
+
7
+
8
+ def process_files_into_docs(pdf_files,progress=gr.Progress()):
9
+ preprocessing_pipeline.run({'file_type_router': {'sources': pdf_files}})
10
+ return "Database created🤗🤗"
11
+
12
+
13
+ def rag(history,question):
14
+
15
+ if history is None:
16
+ history=[]
17
+ messages = [system_message, user_message]
18
+ res = conversational_rag.run(
19
+ data = {'query_rephrase_prompt_builder' : {'query': question},
20
+ 'prompt_builder': {'template': messages, 'query': question},
21
+ 'memory_joiner': {'values': [ChatMessage.from_user(question)]}},
22
+ include_outputs_from=['llm','query_rephrase_llm'])
23
+
24
+ bot_message = res['llm']['replies'][0].content
25
+
26
+ streamed_message = ""
27
+ for token in bot_message.split():
28
+ streamed_message += f"{token} "
29
+ yield history + [(question, streamed_message.strip())], " "
30
+ time.sleep(0.05)
31
+
32
+ history.append((question,bot_message))
33
+
34
+ yield history, " "
35
+
36
+
37
+ with gr.Blocks(theme=gr.themes.Soft(font=gr.themes.GoogleFont('Open Sans')))as demo:
38
+
39
+ gr.HTML("<center><h1>TalkToFiles - Query your documents! 📂📄</h1><center>")
40
+ gr.Markdown("""##### This AI chatbot🤖 can help you chat with your documents. Can upload <b>Text(.txt), PDF(.pdf) and Markdown(.md)</b> files.\
41
+ <b>Please do not upload confidential documents.</b>""")
42
+ with gr.Row():
43
+ with gr.Column(scale=86):
44
+ gr.Markdown("""#### ***Step 1 - Upload Documents and Initialize RAG pipeline***</br>
45
+ Can upload Multiple documents""")
46
+ with gr.Row():
47
+ file_input = gr.File(label='Upload Files', file_count='multiple',file_types=['.pdf', '.txt', '.md'],interactive=True)
48
+ with gr.Row():
49
+ process_files = gr.Button('Create Document store')
50
+ with gr.Row():
51
+ result = gr.Textbox(label="Document store", value='Document store not initialized')
52
+ #Pre-processing Events
53
+ process_files.click(fn=process_files_into_docs, inputs=file_input, outputs=result ,show_progress=True)
54
+
55
+
56
+ with gr.Column(scale=200):
57
+ gr.Markdown("""#### ***Step 2 - Chat with your docs*** """)
58
+ chatbot = gr.Chatbot(label='ChatBot')
59
+ user_input = gr.Textbox(label='Enter your query', placeholder='Type here...')
60
+
61
+ with gr.Row():
62
+ submit_button = gr.Button("Submit")
63
+ clear_btn = gr.ClearButton([user_input, chatbot], value='Clear')
64
+ submit_button.click(rag, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
65
+
66
+
67
+ demo.launch()
68
+
69
+
70
+
71
+