Spaces:
Sleeping
Sleeping
Commit ·
fffe8f4
1
Parent(s): ec12a30
Write python function to fix error
Browse files- api/api.py +6 -0
- api/config.py +27 -1
- api/main.py +74 -0
api/api.py
CHANGED
|
@@ -37,6 +37,12 @@ def get_adalflow_default_root_path():
|
|
| 37 |
"""
|
| 38 |
Get the adalflow root path. In containerized environments, use a writable location.
|
| 39 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Check if we're in a containerized environment or if file system is read-only
|
| 41 |
default_path = os.path.expanduser(os.path.join("~", ".adalflow"))
|
| 42 |
|
|
|
|
| 37 |
"""
|
| 38 |
Get the adalflow root path. In containerized environments, use a writable location.
|
| 39 |
"""
|
| 40 |
+
# First check if ADALFLOW_CACHE_DIR environment variable is set
|
| 41 |
+
env_cache_dir = os.environ.get("ADALFLOW_CACHE_DIR")
|
| 42 |
+
if env_cache_dir:
|
| 43 |
+
os.makedirs(env_cache_dir, exist_ok=True)
|
| 44 |
+
return env_cache_dir
|
| 45 |
+
|
| 46 |
# Check if we're in a containerized environment or if file system is read-only
|
| 47 |
default_path = os.path.expanduser(os.path.join("~", ".adalflow"))
|
| 48 |
|
api/config.py
CHANGED
|
@@ -4,6 +4,29 @@ import logging
|
|
| 4 |
import re
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import List, Union, Dict, Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
|
@@ -12,7 +35,10 @@ from api.openrouter_client import OpenRouterClient
|
|
| 12 |
from api.bedrock_client import BedrockClient
|
| 13 |
from api.azureai_client import AzureAIClient
|
| 14 |
from api.dashscope_client import DashscopeClient
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Get API keys from environment variables
|
| 18 |
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
|
|
|
| 4 |
import re
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import List, Union, Dict, Any
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Load environment variables from .env file at module import time
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Ensure adalflow cache directory is set for containerized environments
|
| 13 |
+
# This prevents permission errors when running in Hugging Face Spaces or similar environments
|
| 14 |
+
if not os.environ.get("ADALFLOW_CACHE_DIR"):
|
| 15 |
+
try:
|
| 16 |
+
# Check if we can use the default home directory
|
| 17 |
+
default_path = os.path.expanduser(os.path.join("~", ".adalflow"))
|
| 18 |
+
os.makedirs(default_path, exist_ok=True)
|
| 19 |
+
# Test write permissions
|
| 20 |
+
test_file = os.path.join(default_path, "test_write")
|
| 21 |
+
with open(test_file, "w") as f:
|
| 22 |
+
f.write("test")
|
| 23 |
+
os.remove(test_file)
|
| 24 |
+
os.environ["ADALFLOW_CACHE_DIR"] = default_path
|
| 25 |
+
except (PermissionError, OSError):
|
| 26 |
+
# Use /tmp if home directory is not writable
|
| 27 |
+
fallback_path = "/tmp/adalflow"
|
| 28 |
+
os.makedirs(fallback_path, exist_ok=True)
|
| 29 |
+
os.environ["ADALFLOW_CACHE_DIR"] = fallback_path
|
| 30 |
|
| 31 |
logger = logging.getLogger(__name__)
|
| 32 |
|
|
|
|
| 35 |
from api.bedrock_client import BedrockClient
|
| 36 |
from api.azureai_client import AzureAIClient
|
| 37 |
from api.dashscope_client import DashscopeClient
|
| 38 |
+
# Import adalflow after environment setup to prevent path permission errors
|
| 39 |
+
import adalflow
|
| 40 |
+
GoogleGenAIClient = adalflow.GoogleGenAIClient
|
| 41 |
+
OllamaClient = adalflow.OllamaClient
|
| 42 |
|
| 43 |
# Get API keys from environment variables
|
| 44 |
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
api/main.py
CHANGED
|
@@ -7,6 +7,80 @@ from dotenv import load_dotenv
|
|
| 7 |
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from api.logging_config import setup_logging
|
| 11 |
|
| 12 |
# Configure logging
|
|
|
|
| 7 |
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
def setup_adalflow_paths():
|
| 11 |
+
"""
|
| 12 |
+
Set up adalflow paths for containerized environments like Hugging Face Spaces.
|
| 13 |
+
This prevents permission errors when adalflow tries to create directories in restricted locations.
|
| 14 |
+
"""
|
| 15 |
+
try:
|
| 16 |
+
# Check if we're in a containerized environment with restricted filesystem
|
| 17 |
+
default_path = os.path.expanduser(os.path.join("~", ".adalflow"))
|
| 18 |
+
|
| 19 |
+
# Try to create the default path first
|
| 20 |
+
try:
|
| 21 |
+
os.makedirs(default_path, exist_ok=True)
|
| 22 |
+
# Test if we can write to it
|
| 23 |
+
test_file = os.path.join(default_path, "test_write")
|
| 24 |
+
with open(test_file, "w") as f:
|
| 25 |
+
f.write("test")
|
| 26 |
+
os.remove(test_file)
|
| 27 |
+
# If successful, set the environment variables to this path
|
| 28 |
+
os.environ["ADALFLOW_CACHE_DIR"] = default_path
|
| 29 |
+
os.environ["ADALFLOW_HOME"] = default_path
|
| 30 |
+
# Set XDG variables which many libraries use for cache directories
|
| 31 |
+
os.environ["XDG_DATA_HOME"] = default_path
|
| 32 |
+
os.environ["XDG_CACHE_HOME"] = default_path
|
| 33 |
+
return default_path
|
| 34 |
+
except (PermissionError, OSError):
|
| 35 |
+
# Fall back to using a writable location in /tmp
|
| 36 |
+
fallback_path = "/tmp/adalflow"
|
| 37 |
+
try:
|
| 38 |
+
os.makedirs(fallback_path, exist_ok=True)
|
| 39 |
+
os.environ["ADALFLOW_CACHE_DIR"] = fallback_path
|
| 40 |
+
os.environ["ADALFLOW_HOME"] = fallback_path
|
| 41 |
+
# Set XDG variables for fallback
|
| 42 |
+
os.environ["XDG_DATA_HOME"] = fallback_path
|
| 43 |
+
os.environ["XDG_CACHE_HOME"] = fallback_path
|
| 44 |
+
# Also override HOME if we're in a restricted environment
|
| 45 |
+
if not os.path.exists(os.path.expanduser("~")) or not os.access(os.path.expanduser("~"), os.W_OK):
|
| 46 |
+
os.environ["HOME"] = "/tmp"
|
| 47 |
+
return fallback_path
|
| 48 |
+
except (PermissionError, OSError):
|
| 49 |
+
# Last resort: use current working directory
|
| 50 |
+
current_dir_path = os.path.join(os.getcwd(), ".adalflow")
|
| 51 |
+
os.makedirs(current_dir_path, exist_ok=True)
|
| 52 |
+
os.environ["ADALFLOW_CACHE_DIR"] = current_dir_path
|
| 53 |
+
os.environ["ADALFLOW_HOME"] = current_dir_path
|
| 54 |
+
# Set XDG variables for last resort
|
| 55 |
+
os.environ["XDG_DATA_HOME"] = current_dir_path
|
| 56 |
+
os.environ["XDG_CACHE_HOME"] = current_dir_path
|
| 57 |
+
return current_dir_path
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error setting up adalflow paths: {e}")
|
| 60 |
+
# Set a default fallback
|
| 61 |
+
fallback_path = "/tmp/adalflow"
|
| 62 |
+
os.makedirs(fallback_path, exist_ok=True)
|
| 63 |
+
os.environ["ADALFLOW_CACHE_DIR"] = fallback_path
|
| 64 |
+
os.environ["ADALFLOW_HOME"] = fallback_path
|
| 65 |
+
# Set XDG variables for emergency fallback
|
| 66 |
+
os.environ["XDG_DATA_HOME"] = fallback_path
|
| 67 |
+
os.environ["XDG_CACHE_HOME"] = fallback_path
|
| 68 |
+
# If we can't access home, set it to /tmp
|
| 69 |
+
try:
|
| 70 |
+
test_home = os.path.expanduser("~")
|
| 71 |
+
if not os.path.exists(test_home) or not os.access(test_home, os.W_OK):
|
| 72 |
+
os.environ["HOME"] = "/tmp"
|
| 73 |
+
except:
|
| 74 |
+
os.environ["HOME"] = "/tmp"
|
| 75 |
+
return fallback_path
|
| 76 |
+
|
| 77 |
+
# Set up adalflow paths before any imports that might use adalflow
|
| 78 |
+
adalflow_path = setup_adalflow_paths()
|
| 79 |
+
print(f"Adalflow path configured to: {adalflow_path}")
|
| 80 |
+
print(f"HOME environment variable: {os.environ.get('HOME')}")
|
| 81 |
+
print(f"ADALFLOW_HOME: {os.environ.get('ADALFLOW_HOME')}")
|
| 82 |
+
print(f"ADALFLOW_CACHE_DIR: {os.environ.get('ADALFLOW_CACHE_DIR')}")
|
| 83 |
+
|
| 84 |
from api.logging_config import setup_logging
|
| 85 |
|
| 86 |
# Configure logging
|