jazzysnake commited on
Commit
eddd3f1
1 Parent(s): 90a828c

proper session data and refractor

Browse files
Files changed (1) hide show
  1. app.py +96 -82
app.py CHANGED
@@ -1,10 +1,7 @@
1
  import json
2
  import gradio as gr
3
- import json
4
- from langchain_legal import run_pipeline
5
 
6
- global data
7
- data = {}
8
 
9
 
10
  def load_json(path: str) -> dict:
@@ -12,10 +9,9 @@ def load_json(path: str) -> dict:
12
  return json.load(f)
13
 
14
 
15
- def get_ai_final_summary(text_input, req: gr.Request):
16
- global data
17
- data[req.client.host] = run_pipeline(text_input)
18
- return data[req.client.host]['ai_final_summary']
19
 
20
 
21
  STEP_NAMES = [
@@ -26,73 +22,73 @@ STEP_NAMES = [
26
  ]
27
 
28
 
29
- def get_inital_assesment(req: gr.Request) -> str:
30
- client_data = data.get(req.client.host)
31
- if client_data is None:
32
- return 'No llm answer yet'
33
- initial = client_data.get('ai_first_5', None)
34
- print(data)
 
 
 
35
  if initial is None:
36
- return 'No llm answer yet'
37
- return initial
38
 
39
 
40
- def get_applicable_eu_regs(summary_tb, json_output, req: gr.Request):
41
- client_data = data.get(req.client.host)
42
- if client_data is None:
43
- return summary_tb, json_output
44
- eval = client_data.get('ai_eur_lex_eval', None)
45
  if eval is None:
46
- return summary_tb, json_output
47
- sources = client_data.get('eur_lex_lookup')
48
- summary_tb = gr.Textbox(value=eval, show_label=False)
49
- if sources is not None:
50
- json_output = gr.Json(value=[json.loads(j) for j in sources])
51
- return summary_tb, json_output
52
-
53
-
54
- def get_applicable_national_regs(summary_tb, json_output, req: gr.Request):
55
- client_data = data.get(req.client.host)
56
- if client_data is None:
57
- return summary_tb, json_output
58
- eval = client_data.get('ai_aus_eval', None)
59
- nat = 'aus'
60
  if eval is None:
61
- eval = client_data.get('ai_ger_eval', None)
62
- nat = 'ger'
63
  if eval is None:
64
- return summary_tb, json_output
65
- if nat == 'aus':
66
- sources = client_data.get('aus_lookup')
67
- else:
68
- sources = client_data.get('ger_lookup')
69
- summary_tb = gr.Textbox(eval, show_label=False)
70
- if sources is not None:
71
- json_output = gr.Json([json.loads(j) for j in sources])
72
- return summary_tb, sources
73
-
74
-
75
- def get_ai_challange(req: gr.Request) -> str:
76
- client_data = data.get(req.client.host)
77
- if client_data is None:
78
- return 'No llm answer yet'
79
- challange = client_data.get('ai_challange', None)
80
- print(data)
81
  if challange is None:
82
- return 'No llm answer yet'
83
- return challange
84
 
85
 
86
- CALLBACKS = [
87
- get_inital_assesment,
88
- get_applicable_eu_regs,
89
- get_applicable_national_regs,
90
- get_ai_challange,
91
- ]
 
 
 
92
 
93
 
94
  def main():
95
  with gr.Blocks() as demo:
 
96
  gr.Markdown("# Lawgarithm's LLM legal research tool")
97
 
98
  with gr.Column():
@@ -101,28 +97,46 @@ def main():
101
  placeholder='Write your legal case here',
102
  )
103
  submit_btn = gr.Button('Submit')
104
-
105
- text_output = gr.Textbox(label='Final Summary')
106
-
107
- submit_btn.click(
108
- get_ai_final_summary, inputs=text_input, outputs=text_output
109
- )
 
 
110
 
111
  with gr.Tabs():
112
- for name, cb in zip(STEP_NAMES, CALLBACKS):
113
- with gr.Tab(f'{name}') as t:
114
- tab_text = gr.Textbox(
115
- placeholder='No answer yet', show_label=False
116
- )
117
- if 'regs' not in name:
118
- t.select(cb, outputs=tab_text)
119
- else:
120
- tab_sources = gr.Json({})
121
- t.select(
122
- cb,
123
- inputs=[tab_text, tab_sources],
124
- outputs=[tab_text, tab_sources],
125
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  demo.launch()
128
 
 
1
  import json
2
  import gradio as gr
 
 
3
 
4
+ from langchain_legal import run_pipeline
 
5
 
6
 
7
  def load_json(path: str) -> dict:
 
9
  return json.load(f)
10
 
11
 
12
+ def get_ai_final_summary(text_input):
13
+ data = run_pipeline(text_input)
14
+ return data, data.get('ai_final_summary', 'No answer yet')
 
15
 
16
 
17
  STEP_NAMES = [
 
22
  ]
23
 
24
 
25
+ def get_final_summary(data):
26
+ summ = data.get('ai_final_summary')
27
+ if summ is None:
28
+ return gr.Textbox('No answer yet', label='Final summary')
29
+ return gr.Textbox(summ, label='Final summary', show_copy_button=True)
30
+
31
+
32
+ def get_inital_assesment(data):
33
+ initial = data.get('ai_first_5', None)
34
  if initial is None:
35
+ return gr.Textbox('No llm answer yet', show_label=False)
36
+ return gr.Textbox(initial, show_label=False, lines=25)
37
 
38
 
39
+ def get_eu_regs_eval(data):
40
+ eval = data.get('ai_eur_lex_eval', None)
 
 
 
41
  if eval is None:
42
+ return gr.Textbox('No llm answer yet', show_label=False)
43
+ return gr.Textbox(eval, show_label=False, lines=50)
44
+
45
+
46
+ def get_eu_regs_sources(data):
47
+ lookup = data.get('eur_lex_lookup', None)
48
+ if lookup is None:
49
+ return gr.Json({}, show_label=False)
50
+ return gr.Json(lookup)
51
+
52
+
53
+ def get_national_regs_eval(data):
54
+ eval = data.get('ai_aus_eval', None)
 
55
  if eval is None:
56
+ eval = data.get('ai_ger_eval')
 
57
  if eval is None:
58
+ return gr.Textbox('No llm answer yet', show_label=False)
59
+ return gr.Textbox(eval, show_label=False, lines=50)
60
+
61
+
62
+ def get_national_regs_sources(data):
63
+ lookup = data.get('aus_lookup', None)
64
+ if lookup is None:
65
+ lookup = data.get('ger_lookup')
66
+ if lookup is None:
67
+ return gr.Json({}, show_label=False)
68
+ return gr.Json(lookup)
69
+
70
+
71
+ def get_ai_challange(data):
72
+ challange = data.get('ai_challange', None)
 
 
73
  if challange is None:
74
+ return gr.Textbox('No llm answer yet', show_label=False)
75
+ return gr.Textbox(challange, show_label=False, lines=50)
76
 
77
 
78
+ def update_ui(data):
79
+ return (
80
+ get_inital_assesment(data),
81
+ get_eu_regs_eval(data),
82
+ get_eu_regs_sources(data),
83
+ get_national_regs_eval(data),
84
+ get_national_regs_sources(data),
85
+ get_ai_challange(data),
86
+ )
87
 
88
 
89
  def main():
90
  with gr.Blocks() as demo:
91
+ data = gr.State({})
92
  gr.Markdown("# Lawgarithm's LLM legal research tool")
93
 
94
  with gr.Column():
 
97
  placeholder='Write your legal case here',
98
  )
99
  submit_btn = gr.Button('Submit')
100
+ text_output = gr.Textbox(label='Final Summary')
101
+ submit_btn.click(
102
+ get_ai_final_summary,
103
+ inputs=[text_input],
104
+ outputs=[data, text_output],
105
+ show_progress='full',
106
+ queue=True,
107
+ )
108
 
109
  with gr.Tabs():
110
+ with gr.Tab(STEP_NAMES[0]):
111
+ inital_tab_text = gr.Textbox(
112
+ placeholder='No answer yet', show_label=False
113
+ )
114
+ with gr.Tab(STEP_NAMES[1]):
115
+ eu_regs_tab_text = gr.Textbox(
116
+ placeholder='No answer yet', show_label=False
117
+ )
118
+ eu_regs_tab_sources = gr.Json({})
119
+ with gr.Tab(STEP_NAMES[2]):
120
+ nat_regs_tab_text = gr.Textbox(
121
+ placeholder='No answer yet', show_label=False
122
+ )
123
+ nat_regs_tab_sources = gr.Json({})
124
+ with gr.Tab(STEP_NAMES[2]):
125
+ comp_assesment_tab_text = gr.Textbox(
126
+ placeholder='No answer yet', show_label=False
127
+ )
128
+ data.change(
129
+ update_ui,
130
+ inputs=data,
131
+ outputs=[
132
+ inital_tab_text,
133
+ eu_regs_tab_text,
134
+ eu_regs_tab_sources,
135
+ nat_regs_tab_text,
136
+ nat_regs_tab_sources,
137
+ comp_assesment_tab_text,
138
+ ],
139
+ )
140
 
141
  demo.launch()
142