Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ from tenacity import retry, stop_after_attempt, wait_exponential
|
|
| 20 |
import re
|
| 21 |
import aiohttp
|
| 22 |
import aiofiles
|
|
|
|
| 23 |
|
| 24 |
# Configure logging with more detailed format
|
| 25 |
logging.basicConfig(
|
|
@@ -805,6 +806,48 @@ app.add_middleware(
|
|
| 805 |
allow_headers=["*"],
|
| 806 |
)
|
| 807 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
# Initialize OpenAI client and agent globally
|
| 809 |
agent = None
|
| 810 |
try:
|
|
|
|
| 20 |
import re
|
| 21 |
import aiohttp
|
| 22 |
import aiofiles
|
| 23 |
+
from huggingface_hub import HfApi
|
| 24 |
|
| 25 |
# Configure logging with more detailed format
|
| 26 |
logging.basicConfig(
|
|
|
|
| 806 |
allow_headers=["*"],
|
| 807 |
)
|
| 808 |
|
| 809 |
+
# Initialize OpenAI client with HF secret
|
| 810 |
+
try:
|
| 811 |
+
# Get API key from HF secrets
|
| 812 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 813 |
+
if hf_token:
|
| 814 |
+
api = HfApi(token=hf_token)
|
| 815 |
+
try:
|
| 816 |
+
openai_api_key = os.environ.get("OPENAI_API_KEY")
|
| 817 |
+
if not openai_api_key:
|
| 818 |
+
logger.error("OPENAI_API_KEY not found in environment variables")
|
| 819 |
+
raise ValueError("OPENAI_API_KEY not found")
|
| 820 |
+
|
| 821 |
+
# Configure OpenAI
|
| 822 |
+
openai.api_key = openai_api_key
|
| 823 |
+
|
| 824 |
+
# Test the API key
|
| 825 |
+
test_response = openai.ChatCompletion.create(
|
| 826 |
+
model="gpt-4",
|
| 827 |
+
messages=[{"role": "user", "content": "test"}],
|
| 828 |
+
max_tokens=5
|
| 829 |
+
)
|
| 830 |
+
logger.info("OpenAI initialized successfully with GPT-4")
|
| 831 |
+
except Exception as e:
|
| 832 |
+
logger.error(f"Error accessing OpenAI API key from secrets: {str(e)}")
|
| 833 |
+
raise
|
| 834 |
+
else:
|
| 835 |
+
logger.error("HF_TOKEN not found in environment variables")
|
| 836 |
+
raise ValueError("HF_TOKEN not found")
|
| 837 |
+
except Exception as e:
|
| 838 |
+
logger.error(f"Error initializing OpenAI: {str(e)}")
|
| 839 |
+
raise
|
| 840 |
+
|
| 841 |
+
# Create necessary directories
|
| 842 |
+
try:
|
| 843 |
+
base_dir = Path("downloaded_gaia_files")
|
| 844 |
+
for subdir in ['text', 'data', 'media', 'other']:
|
| 845 |
+
(base_dir / subdir).mkdir(parents=True, exist_ok=True)
|
| 846 |
+
logger.info("Created knowledge base directories successfully")
|
| 847 |
+
except Exception as e:
|
| 848 |
+
logger.error(f"Error creating directories: {str(e)}")
|
| 849 |
+
raise
|
| 850 |
+
|
| 851 |
# Initialize OpenAI client and agent globally
|
| 852 |
agent = None
|
| 853 |
try:
|