Dharma20 commited on
Commit
8d76cc2
·
verified ·
1 Parent(s): 60ae3d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -75
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
- if isinstance(files, dict):
10
- files = [files]
11
- if not files:
12
- return 'No file uploaded!'
13
-
14
- preprocessing_pipeline.run({'file_type_router': {'sources': files}})
15
-
16
- return "Database created🤗🤗"
17
-
18
-
19
- def rag(history,question):
20
-
21
- if history is None:
22
- history=[]
23
- messages = [system_message, user_message]
24
- res = conversational_rag.run(
25
- data = {'query_rephrase_prompt_builder' : {'query': question},
26
- 'prompt_builder': {'template': messages, 'query': question},
27
- 'memory_joiner': {'values': [ChatMessage.from_user(question)]}},
28
- include_outputs_from=['llm','query_rephrase_llm'])
29
-
30
- bot_message = res['llm']['replies'][0].content
31
-
32
- streamed_message = ""
33
- for token in bot_message.split():
34
- streamed_message += f"{token} "
35
- yield history + [(question, streamed_message.strip())], " "
36
- time.sleep(0.05)
37
-
38
- history.append((question,bot_message))
39
-
40
- yield history, " "
 
41
 
42
  EXAMPLE_FILE = "RAG Survey.pdf"
43
 
44
- with gr.Blocks(theme=gr.themes.Soft())as demo:
45
-
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
- with gr.Row():
50
- with gr.Column(scale=86):
51
- gr.Markdown("""#### ***Step 1 - Upload Documents and Initialize RAG pipeline***</br>
52
- Can upload Multiple documents""")
53
- with gr.Row():
54
- file_input = gr.File(label='Upload Files', file_count='multiple',file_types=['.pdf', '.txt', '.md'],interactive=True)
55
- with gr.Row():
56
- process_files = gr.Button('Create Document store')
57
- with gr.Row():
58
- result = gr.Textbox(label="Document store", value='Document store not initialized')
59
- #Pre-processing Events
60
- process_files.click(fn=process_files_into_docs, inputs=file_input, outputs=result ,show_progress=True)
61
-
62
- def load_example():
63
- return [EXAMPLE_FILE]
64
-
65
- with gr.Row():
66
- gr.Examples(
67
- examples=[[EXAMPLE_FILE]],
 
 
68
  inputs=file_input,
69
- examples_per_page=1,
70
- label="Click to upload an example"
71
- ).dataset.click(fn=load_example, inputs=[], outputs=file_input)
72
-
73
-
74
-
75
- with gr.Column(scale=200):
76
- gr.Markdown("""#### ***Step 2 - Chat with your docs*** """)
77
- chatbot = gr.Chatbot(label='ChatBot')
78
- user_input = gr.Textbox(label='Enter your query', placeholder='Type here...')
79
-
80
- with gr.Row():
81
- submit_button = gr.Button("Submit")
82
- clear_btn = gr.ClearButton([user_input, chatbot], value='Clear')
83
- submit_button.click(rag, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
84
-
85
-
86
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)