DialogueRobust commited on
Commit
292920a
·
1 Parent(s): 3735c7a

First commit

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from backend import BackEnd
3
+ import argparse
4
+ import configparser
5
+
6
+
7
+
8
+
9
+ class Demo:
10
+ def __init__(self, config):
11
+ self.config = config
12
+ self.backend = BackEnd(self.config)
13
+ self.lang = self.config['General']['language'].lower()[:2]
14
+
15
+
16
+ def process_query(self,history, query):
17
+ res,context = self.backend.process_query(query)
18
+ documents = '\n\n'.join(context)
19
+
20
+ if self.lang == 'fr':
21
+ response = res['réponse']
22
+ rationale = res['justification']
23
+ elif lang == 'en':
24
+ response = res['answer']
25
+ rationale = res['rationale']
26
+
27
+ source = res['source']
28
+ history.append((query,response))#, "See details below))"))
29
+ return history, "",response, rationale, source, documents
30
+
31
+
32
+ def run_demo(self):
33
+ if self.lang == 'fr':
34
+ with gr.Blocks(theme=gr.themes.Glass()) as demo:
35
+ gr.Image(value = 'crim_logo.png')
36
+ gr.Markdown("## Démonstration d'IA générative")
37
+
38
+
39
+ chatbot = gr.Chatbot(label="Conversation", height=400)
40
+ gr.Markdown("Saisissez une requête ci-dessous et voyez la réponse et le raisonnement du système.")
41
+ with gr.Row():
42
+ query_input = gr.Textbox(
43
+ show_label=False,
44
+ placeholder="Tapez quelque chose ...",
45
+ lines=1
46
+ )
47
+
48
+ send_btn = gr.Button("Envoyer", scale = 0)
49
+ gr.Markdown("### Dernière réponse")
50
+ with gr.Row():
51
+ answer_output = gr.Textbox(label="Réponse", lines = 3, interactive=False)
52
+ with gr.Row():
53
+ reasoning_output = gr.Textbox(label="Raisonnement du système", lines = 5,interactive=False)
54
+ with gr.Row():
55
+ source_output = gr.Textbox(label="Source", interactive=False)
56
+
57
+
58
+ with gr.Accordion("Documents récupérés", open=False):
59
+ docs_output = gr.Textbox(label="Documents justificatifs", interactive=False, lines=30)
60
+
61
+ inputs = [chatbot, query_input]
62
+ outputs = [chatbot, query_input, answer_output, reasoning_output, source_output,docs_output]
63
+ query_input.submit(fn=self.process_query, inputs=inputs, outputs=outputs)
64
+ send_btn.click(fn=self.process_query, inputs=inputs, outputs=outputs)
65
+
66
+ elif self.lang == 'en':
67
+ with gr.Blocks(theme=gr.themes.Glass()) as demo:
68
+ gr.Image(value = 'crim_logo.png')
69
+ gr.Markdown("## Generative AI Chat Demo with Structured Outputs")
70
+
71
+ chatbot = gr.Chatbot(label="Conversation", height=400)
72
+
73
+ with gr.Row():
74
+ query_input = gr.Textbox(
75
+ show_label=False,
76
+ placeholder="Type your query here and press Enter...",
77
+ lines=1
78
+ )
79
+
80
+ send_btn = gr.Button("Send", scale = 0)
81
+ gr.Markdown("### Latest Response Details")
82
+ with gr.Row():
83
+ answer_output = gr.Textbox(label="Answer", interactive=False)
84
+ with gr.Row():
85
+ reasoning_output = gr.Textbox(label="System Reasoning", interactive=False)
86
+ with gr.Row():
87
+ source_output = gr.Textbox(label="Source", interactive=False)
88
+
89
+
90
+ with gr.Accordion("Retrieved Documents", open=False):
91
+ docs_output = gr.Textbox(label="Supporting Documents", interactive=False, lines=30)
92
+
93
+ inputs = [chatbot, query_input]
94
+ outputs = [chatbot, query_input, answer_output, reasoning_output, source_output,docs_output]
95
+ query_input.submit(fn=self.process_query, inputs=inputs, outputs=outputs)
96
+ send_btn.click(fn=self.process_query, inputs=inputs, outputs=outputs)
97
+
98
+
99
+
100
+
101
+ demo.launch()
102
+
103
+
104
+
105
+
106
+
107
+
108
+ def main():
109
+
110
+ parser = argparse.ArgumentParser()
111
+ # parser.add_argument('--config_file', type=str, required=True, help='File containing the configuration for the backend (in .ini format)')
112
+ # args = parser.parse_args()
113
+
114
+ config = configparser.ConfigParser()
115
+ config.read('config.ini')
116
+ demo = Demo(config)
117
+ demo.run_demo()
118
+
119
+
120
+ if __name__ == "__main__":
121
+ main()