Spaces:
Sleeping
Sleeping
| import asyncio | |
| import os | |
| import uuid | |
| import py_compile | |
| 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 | |
| from models.Database_Entity import StopSignal | |
| async def check_should_stop(chat_id: str, stop_event: object = None): | |
| await asyncio.sleep(0.1) | |
| if stop_event and stop_event.is_set(): | |
| print("🛑 Dừng qua stop_event.") | |
| return {"status": "cancelled"} | |
| 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 | |
| # Gọi sinh code | |
| code_python = return_code_python(output, folder=folder) | |
| code_clean = code_python.strip() | |
| if code_clean.startswith("```python"): | |
| code_clean = code_clean[9:].strip() | |
| if code_clean.endswith("```"): | |
| code_clean = code_clean[:-3].strip() | |
| code_clean = code_clean.replace("os_path:", "os.path").strip() | |
| encoding_fix = 'import sys\nsys.stdout.reconfigure(encoding="utf-8")\n\n' | |
| encoding_fix1= 'import numpy as np\n\n' | |
| code_clean = encoding_fix + encoding_fix1 + code_clean | |
| code_python=code_clean | |
| # Ghi ra file tạm để kiểm tra syntax | |
| temp_file_name = f"{uuid.uuid4().hex[:8]}.py" | |
| os.makedirs(folder, exist_ok=True) | |
| temp_path = os.path.join(folder, temp_file_name) | |
| with open(temp_path, "w", encoding="utf-8") as f: | |
| f.write(code_python) | |
| # Kiểm tra cú pháp | |
| try: | |
| py_compile.compile(temp_path, doraise=True) | |
| except py_compile.PyCompileError as e: | |
| print(f"❌ Lỗi cú pháp, tạo lại code: {e}") | |
| code_python = return_code_python(output, folder=folder) | |
| result_check = await check_should_stop(chat_id, stop_event) | |
| if result_check: | |
| return result_check | |
| return code_python, folder | |