Spaces:
Runtime error
Runtime error
Update graph_rag.py
Browse files- graph_rag.py +21 -24
graph_rag.py
CHANGED
|
@@ -13,36 +13,28 @@ logger = logging.getLogger(__name__)
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
def unzip_folder(zip_path, extract_path):
|
|
|
|
|
|
|
|
|
|
| 16 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 17 |
-
zip_ref.extractall(
|
| 18 |
|
| 19 |
-
logger.info(f"Extracted contents to {
|
| 20 |
-
logger.info(f"Contents of {
|
| 21 |
-
for
|
| 22 |
-
|
| 23 |
-
logger.info(os.path.join(root, file))
|
| 24 |
|
| 25 |
-
return
|
| 26 |
-
|
| 27 |
-
def find_data_dir(root_dir):
|
| 28 |
-
for root, dirs, files in os.walk(root_dir):
|
| 29 |
-
if 'output' in dirs:
|
| 30 |
-
return os.path.join(root, 'output')
|
| 31 |
-
return root_dir
|
| 32 |
|
| 33 |
-
def run_graphrag_query(query,
|
| 34 |
-
data_dir = find_data_dir(root_dir)
|
| 35 |
-
|
| 36 |
# Define the command
|
| 37 |
command = [
|
| 38 |
"python", "-m", "graphrag.query",
|
| 39 |
-
"--root",
|
| 40 |
-
"--method", "
|
| 41 |
query
|
| 42 |
]
|
| 43 |
|
| 44 |
-
logger.info(f"Running command: {' '.join(command)}")
|
| 45 |
-
|
| 46 |
# Run the command
|
| 47 |
result = subprocess.run(command, capture_output=True, text=True)
|
| 48 |
|
|
@@ -50,18 +42,20 @@ def run_graphrag_query(query, root_dir):
|
|
| 50 |
if result.returncode == 0:
|
| 51 |
return result.stdout
|
| 52 |
else:
|
| 53 |
-
logger.error(f"GraphRAG error: {result.stderr}")
|
| 54 |
return result.stderr
|
| 55 |
|
| 56 |
def qa_tool_graph_rag(user_question):
|
| 57 |
try:
|
| 58 |
# Paths from environment variables
|
| 59 |
zip_path = os.getenv('ZIP_PATH', '/home/user/app/ragtest.zip')
|
| 60 |
-
extract_path = os.getenv('EXTRACT_PATH', '/home/user/app
|
| 61 |
|
| 62 |
# Unzip the folder
|
| 63 |
output_dir = unzip_folder(zip_path, extract_path)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
# Run the GraphRAG query
|
| 66 |
answer = run_graphrag_query(user_question, output_dir)
|
| 67 |
|
|
@@ -79,5 +73,8 @@ def qa_tool_graph_rag(user_question):
|
|
| 79 |
|
| 80 |
finally:
|
| 81 |
# Clean up: remove the extracted directory
|
| 82 |
-
if
|
| 83 |
-
shutil.rmtree(
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
def unzip_folder(zip_path, extract_path):
|
| 16 |
+
output_dir = os.path.join(extract_path, "ragtest")
|
| 17 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 18 |
+
|
| 19 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 20 |
+
zip_ref.extractall(output_dir)
|
| 21 |
|
| 22 |
+
logger.info(f"Extracted contents to {output_dir}")
|
| 23 |
+
logger.info(f"Contents of {output_dir}:")
|
| 24 |
+
for file in os.listdir(output_dir):
|
| 25 |
+
logger.info(os.path.join(output_dir, file))
|
|
|
|
| 26 |
|
| 27 |
+
return output_dir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def run_graphrag_query(query, ragtest_dir):
|
|
|
|
|
|
|
| 30 |
# Define the command
|
| 31 |
command = [
|
| 32 |
"python", "-m", "graphrag.query",
|
| 33 |
+
"--root", ragtest_dir,
|
| 34 |
+
"--method", "local",
|
| 35 |
query
|
| 36 |
]
|
| 37 |
|
|
|
|
|
|
|
| 38 |
# Run the command
|
| 39 |
result = subprocess.run(command, capture_output=True, text=True)
|
| 40 |
|
|
|
|
| 42 |
if result.returncode == 0:
|
| 43 |
return result.stdout
|
| 44 |
else:
|
|
|
|
| 45 |
return result.stderr
|
| 46 |
|
| 47 |
def qa_tool_graph_rag(user_question):
|
| 48 |
try:
|
| 49 |
# Paths from environment variables
|
| 50 |
zip_path = os.getenv('ZIP_PATH', '/home/user/app/ragtest.zip')
|
| 51 |
+
extract_path = os.getenv('EXTRACT_PATH', '/home/user/app')
|
| 52 |
|
| 53 |
# Unzip the folder
|
| 54 |
output_dir = unzip_folder(zip_path, extract_path)
|
| 55 |
|
| 56 |
+
# Change to the directory containing the ragtest folder
|
| 57 |
+
os.chdir(extract_path)
|
| 58 |
+
|
| 59 |
# Run the GraphRAG query
|
| 60 |
answer = run_graphrag_query(user_question, output_dir)
|
| 61 |
|
|
|
|
| 73 |
|
| 74 |
finally:
|
| 75 |
# Clean up: remove the extracted directory
|
| 76 |
+
if 'output_dir' in locals():
|
| 77 |
+
shutil.rmtree(output_dir)
|
| 78 |
+
|
| 79 |
+
# Change back to the original directory
|
| 80 |
+
os.chdir('/home/user/app')
|