Spaces:
Runtime error
Runtime error
Update graph_rag.py
Browse files- graph_rag.py +60 -3
graph_rag.py
CHANGED
|
@@ -1,5 +1,62 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def qa_tool_graph_rag(user_question):
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__)
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 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:
|
| 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 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:
|
| 56 |
+
logger.error(f"Error in GraphRAG processing: {str(e)}")
|
| 57 |
+
return f"An error occurred: {str(e)}", [], gr.update(visible=False), gr.update(visible=False)
|
| 58 |
+
|
| 59 |
+
finally:
|
| 60 |
+
# Clean up: remove the extracted directory
|
| 61 |
+
if 'output_dir' in locals():
|
| 62 |
+
shutil.rmtree(output_dir)
|