Spaces:
Sleeping
Sleeping
| import nest_asyncio | |
| import asyncio | |
| from .execute_query import execute_query_user | |
| from .pipeline import run_pipeline | |
| from .merge_output import parse_step_data_blocks | |
| from .prompt_databse import prompt_user as prompt_user1 | |
| from .prompt_query import query as prompt_query1 | |
| from .return_code_python import return_code_python | |
| import google.generativeai as genai | |
| from .gemini.result_sql import generate_step | |
| import asyncio | |
| from models.Database_Entity import StopSignal | |
| async def check_should_stop(chat_id: str, stop_event: object = None): | |
| # Trường hợp dừng qua RAM (in-memory) | |
| await asyncio.sleep(0.1) | |
| if stop_event and stop_event.is_set(): | |
| print("🛑 Dừng qua stop_event.") | |
| return {"status": "cancelled"} | |
| # Trường hợp dừng qua MongoDB | |
| await asyncio.sleep(0.1) | |
| if StopSignal.objects(chat_history=chat_id, is_stopped=True).first(): | |
| print("🛑 Dừng vì có StopSignal trong DB.") | |
| return {"status": "cancelled"} | |
| return None | |
| async def analyze(query: str,user_id, languages, role,chat_id: str = "", stop_event: object = None): | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| data2 = generate_step(prompt_query1.format(question =query,prompt_user = prompt_user1)) | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| data_test3 = await run_pipeline(data2, execute_query_user, user_id, role, languages) | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| data_final = data2 | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| parsed_steps = parse_step_data_blocks(data_final) | |
| output = "" | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| for step in parsed_steps: | |
| step_id = step["id"] | |
| output += f"Yêu cầu {step_id} : {step['content']}\n" | |
| if step_id in data_test3: | |
| data = data_test3[step_id][0] | |
| result_str = "\n".join([f"{item}" for item in data]) | |
| output += f"Kết quả yêu cầu {step_id} :\n{result_str}\n" | |
| else: | |
| output += "Kết quả:\nKhông có dữ liệu tương ứng.\n" | |
| output += "-"*50 + "\n\n" | |
| folder = "test5" | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| code_python = return_code_python(output,folder="test5") | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| return code_python, folder | |