AbdulMoid commited on
Commit
f38d527
·
verified ·
1 Parent(s): 325b617

Update graph_rag.py

Browse files
Files changed (1) hide show
  1. graph_rag.py +31 -13
graph_rag.py CHANGED
@@ -1,10 +1,10 @@
1
  import os
2
  import zipfile
3
- import tempfile
4
  import shutil
5
- from graphrag.query import query_engine
6
  import logging
 
7
  from dotenv import load_dotenv
 
8
 
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
10
  logger = logging.getLogger(__name__)
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
13
  load_dotenv()
14
 
15
  def unzip_folder(zip_path, extract_path):
16
- output_dir = os.path.join(extract_path, "output_diagrams")
17
  os.makedirs(output_dir, exist_ok=True)
18
 
19
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
@@ -26,30 +26,45 @@ def unzip_folder(zip_path, extract_path):
26
 
27
  return output_dir
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def qa_tool_graph_rag(user_question):
30
  try:
31
  # Paths from environment variables
32
- zip_path = os.getenv('ZIP_PATH', '/home/user/app/output_diagrams.zip')
33
  extract_path = os.getenv('EXTRACT_PATH', '/home/user/app')
34
 
35
  # Unzip the folder
36
  output_dir = unzip_folder(zip_path, extract_path)
37
 
38
- # Set the GRAPHRAG_ROOT environment variable
39
- os.environ['GRAPHRAG_ROOT'] = output_dir
40
 
41
- # Perform the query
42
- result = query_engine(user_question, method="global")
43
 
44
- # Process the result
45
- answer = result.get('answer', "No answer found.")
46
 
47
  # For simplicity, we're not handling images here.
48
  # You may need to adjust this based on how GraphRAG returns results.
49
  images = []
50
 
51
- logger.info(f"GraphRAG answer generated: {answer}")
52
-
53
  return answer, images, gr.update(visible=True), gr.update(visible=True)
54
 
55
  except Exception as e:
@@ -59,4 +74,7 @@ def qa_tool_graph_rag(user_question):
59
  finally:
60
  # Clean up: remove the extracted directory
61
  if 'output_dir' in locals():
62
- shutil.rmtree(output_dir)
 
 
 
 
1
  import os
2
  import zipfile
 
3
  import shutil
 
4
  import logging
5
+ import subprocess
6
  from dotenv import load_dotenv
7
+ import gradio as gr
8
 
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
10
  logger = logging.getLogger(__name__)
 
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:
 
26
 
27
  return output_dir
28
 
29
+ def run_graphrag_query(query):
30
+ # Define the command
31
+ command = [
32
+ "python", "-m", "graphrag.query",
33
+ "--root", "./ragtest",
34
+ "--method", "local",
35
+ query
36
+ ]
37
+
38
+ # Run the command
39
+ result = subprocess.run(command, capture_output=True, text=True)
40
+
41
+ # Return the output or error message
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)
61
 
62
+ logger.info(f"GraphRAG answer generated: {answer}")
 
63
 
64
  # For simplicity, we're not handling images here.
65
  # You may need to adjust this based on how GraphRAG returns results.
66
  images = []
67
 
 
 
68
  return answer, images, gr.update(visible=True), gr.update(visible=True)
69
 
70
  except Exception as e:
 
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')