DocUA commited on
Commit
98a589f
·
1 Parent(s): 45fc0a6

Working app.py

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. run.py → app.py +0 -0
  3. interface.py +67 -21
  4. main.py +1 -8
README.md CHANGED
@@ -5,6 +5,6 @@ colorFrom: "blue"
5
  colorTo: "green"
6
  sdk: "gradio"
7
  sdk_version: "5.8.0"
8
- app_file: main.py
9
  pinned: false
10
  ---
 
5
  colorTo: "green"
6
  sdk: "gradio"
7
  sdk_version: "5.8.0"
8
+ app_file: app.py
9
  pinned: false
10
  ---
run.py → app.py RENAMED
File without changes
interface.py CHANGED
@@ -3,46 +3,92 @@ import re
3
  from typing import Callable, Any, Tuple
4
  import asyncio
5
 
 
6
  def create_gradio_interface(search_action: Callable) -> gr.Blocks:
7
- with gr.Blocks() as app:
 
 
8
  gr.Markdown("# Знаходьте правові позиції Верховного Суду")
9
 
10
- input_field = gr.Textbox(
11
- label="Введіть текст або посилання на судове рішення",
12
- lines=1
13
- )
14
- search_button = gr.Button("Пошук", interactive=False)
15
- warning_message = gr.Markdown(visible=False)
16
- search_output = gr.Markdown(label="Результат пошуку")
 
 
 
 
 
 
 
 
 
 
17
  state_nodes = gr.State()
18
 
19
- async def async_wrapper(text):
20
- return await search_action(text)
 
 
 
 
 
 
 
 
 
 
21
 
22
  def update_button_state(text: str) -> Tuple[gr.update, gr.update]:
23
  text = text.strip()
24
  if not text:
25
  return gr.update(value="Пошук", interactive=False), gr.update(visible=False)
26
- elif re.match(r"^https://reyestr\.court\.gov\.ua/Review/\d+$", text):
27
- return gr.update(value="Пошук за URL", interactive=True), gr.update(visible=False)
28
- elif text.startswith("http"):
29
- return gr.update(value="Пошук", interactive=False), gr.update(
30
- value="Неправильний формат URL. Використовуйте посилання формату https://reyestr.court.gov.ua/Review/{doc_id}",
31
- visible=True,
 
 
 
 
 
 
 
 
 
 
32
  )
33
- else:
34
- return gr.update(value="Пошук за текстом", interactive=True), gr.update(visible=False)
35
 
 
 
 
 
 
 
 
36
  search_button.click(
37
- fn=async_wrapper,
38
  inputs=input_field,
39
- outputs=[search_output, state_nodes]
 
40
  )
41
 
42
  input_field.change(
43
  fn=update_button_state,
44
  inputs=input_field,
45
  outputs=[search_button, warning_message],
 
 
 
 
 
 
 
46
  )
47
 
48
- return app
 
3
  from typing import Callable, Any, Tuple
4
  import asyncio
5
 
6
+
7
  def create_gradio_interface(search_action: Callable) -> gr.Blocks:
8
+ """Creates Gradio interface for the legal search system."""
9
+
10
+ with gr.Blocks() as demo:
11
  gr.Markdown("# Знаходьте правові позиції Верховного Суду")
12
 
13
+ with gr.Row():
14
+ input_field = gr.Textbox(
15
+ label="Введіть текст або посилання на судове рішення",
16
+ lines=1,
17
+ placeholder="Введіть запит або URL рішення суду...",
18
+ )
19
+
20
+ with gr.Row():
21
+ search_button = gr.Button("Пошук", interactive=False, variant="primary")
22
+ warning_message = gr.Markdown(visible=False)
23
+
24
+ with gr.Row():
25
+ search_output = gr.Markdown(
26
+ label="Результат пошуку",
27
+ value="Результати пошуку з'являться тут...",
28
+ )
29
+
30
  state_nodes = gr.State()
31
 
32
+ def sync_search(text: str) -> Tuple[str, Any]:
33
+ """Синхронна обгортка для асинхронного пошуку"""
34
+ try:
35
+ loop = asyncio.new_event_loop()
36
+ asyncio.set_event_loop(loop)
37
+ result = loop.run_until_complete(search_action(text))
38
+ loop.close()
39
+ return result
40
+ except Exception as e:
41
+ error_msg = f"Помилка при пошуку: {str(e)}"
42
+ print(error_msg)
43
+ return error_msg, None
44
 
45
  def update_button_state(text: str) -> Tuple[gr.update, gr.update]:
46
  text = text.strip()
47
  if not text:
48
  return gr.update(value="Пошук", interactive=False), gr.update(visible=False)
49
+
50
+ if re.match(r"^https://reyestr\.court\.gov\.ua/Review/\d+$", text):
51
+ return gr.update(
52
+ value="Пошук за URL",
53
+ interactive=True,
54
+ variant="primary"
55
+ ), gr.update(visible=False)
56
+
57
+ if text.startswith("http"):
58
+ return gr.update(
59
+ value="Пошук",
60
+ interactive=False,
61
+ variant="secondary"
62
+ ), gr.update(
63
+ value="⚠️ Неправильний формат URL. Використовуйте посилання формату https://reyestr.court.gov.ua/Review/{doc_id}",
64
+ visible=True
65
  )
 
 
66
 
67
+ return gr.update(
68
+ value="Пошук за текстом",
69
+ interactive=True,
70
+ variant="primary"
71
+ ), gr.update(visible=False)
72
+
73
+ # Event handlers
74
  search_button.click(
75
+ fn=sync_search, # Використовуємо синхронну версію
76
  inputs=input_field,
77
+ outputs=[search_output, state_nodes],
78
+ api_name="search"
79
  )
80
 
81
  input_field.change(
82
  fn=update_button_state,
83
  inputs=input_field,
84
  outputs=[search_button, warning_message],
85
+ api_name="update_state"
86
+ )
87
+
88
+ input_field.change(
89
+ lambda: gr.update(value="Результати пошуку з'являться тут..."),
90
+ None,
91
+ search_output,
92
  )
93
 
94
+ return demo
main.py CHANGED
@@ -249,10 +249,7 @@ def main():
249
  if initialize_components():
250
  print("Components initialized successfully!")
251
  app = create_gradio_interface(main_search_action)
252
- app.queue(max_size=1).launch(
253
- show_error=True,
254
- share=True
255
- )
256
  else:
257
  print(
258
  "Failed to initialize components. Please check the paths and try again.",
@@ -260,10 +257,6 @@ def main():
260
  )
261
  sys.exit(1)
262
 
263
- if __name__ == "__main__":
264
- # Видаляємо nest_asyncio.apply()
265
- main()
266
-
267
 
268
  if __name__ == "__main__":
269
  main()
 
249
  if initialize_components():
250
  print("Components initialized successfully!")
251
  app = create_gradio_interface(main_search_action)
252
+ app.launch(share=True)
 
 
 
253
  else:
254
  print(
255
  "Failed to initialize components. Please check the paths and try again.",
 
257
  )
258
  sys.exit(1)
259
 
 
 
 
 
260
 
261
  if __name__ == "__main__":
262
  main()