Rulga commited on
Commit
f4e115f
·
1 Parent(s): c2a566f

Add knowledge base status check and enhance chat interface with new buttons and instructions

Browse files
Files changed (1) hide show
  1. app.py +69 -9
app.py CHANGED
@@ -52,22 +52,63 @@ def build_kb():
52
  except Exception as e:
53
  return f"API connection error: {str(e)}"
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # Create the Gradio interface
56
- with gr.Blocks() as demo:
57
- gr.Markdown("# Status Law Assistant")
58
 
59
  with gr.Row():
60
- with gr.Column():
61
- build_kb_btn = gr.Button("Create/Update Knowledge Base")
62
- kb_status = gr.Textbox(label="Knowledge Base Status")
 
 
 
 
 
 
 
63
  build_kb_btn.click(build_kb, inputs=None, outputs=kb_status)
 
64
 
 
65
  conversation_id = gr.State(None)
66
 
67
  with gr.Row():
68
- with gr.Column():
69
- chatbot = gr.Chatbot(label="Chat with Assistant")
70
- msg = gr.Textbox(label="Your Question")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  def respond(message, chat_history, conv_id):
73
  if not message.strip():
@@ -78,8 +119,27 @@ with gr.Blocks() as demo:
78
  chat_history[-1][1] = response
79
  return chat_history, new_conv_id
80
 
 
81
  msg.submit(respond, [msg, chatbot, conversation_id], [chatbot, conversation_id])
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  if __name__ == "__main__":
 
 
84
  # Launch Gradio interface
85
- demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
 
 
 
 
52
  except Exception as e:
53
  return f"API connection error: {str(e)}"
54
 
55
+ # Добавим функцию проверки статуса базы знаний
56
+ def check_kb_status():
57
+ try:
58
+ response = requests.get("http://127.0.0.1:8000/")
59
+ if response.status_code == 200:
60
+ data = response.json()
61
+ if data["knowledge_base_exists"]:
62
+ kb_info = data["kb_info"]
63
+ return f"✅ База знаний готова к работе\nВерсия: {kb_info['version']}\nРазмер: {kb_info['size']:.2f} MB"
64
+ else:
65
+ return "❌ База знаний не создана. Нажмите кнопку 'Create/Update Knowledge Base'"
66
+ except Exception as e:
67
+ return f"❌ Ошибка проверки статуса: {str(e)}"
68
+
69
  # Create the Gradio interface
70
+ with gr.Blocks(title="Status Law Assistant", theme=gr.themes.Soft()) as demo:
71
+ gr.Markdown("# 🤖 Status Law Assistant")
72
 
73
  with gr.Row():
74
+ with gr.Column(scale=1):
75
+ # Кнопки управления базой знаний
76
+ build_kb_btn = gr.Button("Create/Update Knowledge Base", variant="primary")
77
+ check_status_btn = gr.Button("Check Status")
78
+ kb_status = gr.Textbox(
79
+ label="Knowledge Base Status",
80
+ value="Checking status...",
81
+ interactive=False
82
+ )
83
+ # Привязываем обе кнопки
84
  build_kb_btn.click(build_kb, inputs=None, outputs=kb_status)
85
+ check_status_btn.click(check_kb_status, inputs=None, outputs=kb_status)
86
 
87
+ gr.Markdown("### 💬 Chat Interface")
88
  conversation_id = gr.State(None)
89
 
90
  with gr.Row():
91
+ with gr.Column(scale=2):
92
+ # Улучшенный интерфейс чата
93
+ chatbot = gr.Chatbot(
94
+ label="Conversation",
95
+ height=400,
96
+ show_label=False,
97
+ bubble_full_width=False
98
+ )
99
+ with gr.Row():
100
+ msg = gr.Textbox(
101
+ label="Введите ваш вопрос здесь",
102
+ placeholder="Напишите ваш вопрос и нажмите Enter...",
103
+ scale=4
104
+ )
105
+ submit_btn = gr.Button("Отправить", variant="primary", scale=1)
106
+
107
+ # Добавляем очистку истории
108
+ clear_btn = gr.Button("Очистить историю")
109
+
110
+ def clear_history():
111
+ return [], None
112
 
113
  def respond(message, chat_history, conv_id):
114
  if not message.strip():
 
119
  chat_history[-1][1] = response
120
  return chat_history, new_conv_id
121
 
122
+ # Привязываем обработчики
123
  msg.submit(respond, [msg, chatbot, conversation_id], [chatbot, conversation_id])
124
+ submit_btn.click(respond, [msg, chatbot, conversation_id], [chatbot, conversation_id])
125
+ clear_btn.click(clear_history, None, [chatbot, conversation_id])
126
+
127
+ # Добавляем информацию об использовании
128
+ with gr.Accordion("ℹ️ Как использовать", open=False):
129
+ gr.Markdown("""
130
+ 1. Сначала нажмите кнопку **Create/Update Knowledge Base** для создания базы знаний
131
+ 2. Дождитесь сообщения об успешном создании базы
132
+ 3. Введите ваш вопрос в текстовое поле и нажмите Enter или кнопку "Отправить"
133
+ 4. Используйте кнопку "Очистить историю" для начала новой беседы
134
+ """)
135
 
136
  if __name__ == "__main__":
137
+ # Проверяем статус базы знаний при запуске
138
+ initial_status = check_kb_status()
139
  # Launch Gradio interface
140
+ demo.launch(
141
+ server_name="0.0.0.0",
142
+ server_port=7860,
143
+ share=False
144
+ )
145
+ #demo.launch(server_name="0.0.0.0", server_port=7860, share=False)