Spaces:
Running
Running
ai-puppy
commited on
Commit
Β·
4be3026
1
Parent(s):
a302af0
save
Browse files- app.py +61 -3
- install_deno.sh +17 -0
- packages.txt +2 -0
app.py
CHANGED
@@ -2,9 +2,49 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import asyncio
|
4 |
import tempfile
|
|
|
|
|
5 |
from dotenv import find_dotenv, load_dotenv
|
6 |
from langchain.chat_models import init_chat_model
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Import the CodeAct agent functionality
|
10 |
from agent import FileInjectedPyodideSandbox, create_pyodide_eval_fn, create_codeact
|
@@ -43,7 +83,7 @@ def analyze_file_with_question(user_question):
|
|
43 |
"""
|
44 |
Analyze the uploaded file using the new guided approach with user question
|
45 |
"""
|
46 |
-
global uploaded_file_path
|
47 |
|
48 |
try:
|
49 |
if not uploaded_file_path or not os.path.exists(uploaded_file_path):
|
@@ -52,12 +92,30 @@ def analyze_file_with_question(user_question):
|
|
52 |
if not user_question or user_question.strip() == "":
|
53 |
user_question = "Provide a comprehensive analysis of this file including security, performance, and data insights."
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# Use the new guided analysis approach
|
56 |
result = analyze_file_with_guidance_sync(uploaded_file_path, user_question)
|
57 |
return result
|
58 |
|
59 |
except Exception as e:
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
async def analyze_uploaded_file():
|
63 |
"""Legacy function - kept for backward compatibility"""
|
|
|
2 |
import gradio as gr
|
3 |
import asyncio
|
4 |
import tempfile
|
5 |
+
import subprocess
|
6 |
+
import shutil
|
7 |
from dotenv import find_dotenv, load_dotenv
|
8 |
from langchain.chat_models import init_chat_model
|
9 |
+
|
10 |
+
# Auto-install Deno if not found (for Hugging Face Spaces)
|
11 |
+
def ensure_deno_installed():
|
12 |
+
"""Install Deno if not already installed (for Hugging Face Spaces compatibility)"""
|
13 |
+
try:
|
14 |
+
# Check if Deno is already installed
|
15 |
+
result = subprocess.run(['deno', '--version'], capture_output=True, text=True)
|
16 |
+
if result.returncode == 0:
|
17 |
+
print(f"β
Deno already installed: {result.stdout.split()[1]}")
|
18 |
+
return True
|
19 |
+
except FileNotFoundError:
|
20 |
+
pass
|
21 |
+
|
22 |
+
print("π§ Deno not found. Installing Deno for PyodideSandbox...")
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Install Deno using the official installer
|
26 |
+
install_cmd = "curl -fsSL https://deno.land/install.sh | sh"
|
27 |
+
result = subprocess.run(install_cmd, shell=True, capture_output=True, text=True)
|
28 |
+
|
29 |
+
if result.returncode == 0:
|
30 |
+
# Add Deno to PATH
|
31 |
+
deno_path = os.path.expanduser("~/.deno/bin")
|
32 |
+
if deno_path not in os.environ.get("PATH", ""):
|
33 |
+
os.environ["PATH"] = f"{deno_path}:{os.environ.get('PATH', '')}"
|
34 |
+
|
35 |
+
print("β
Deno installed successfully!")
|
36 |
+
return True
|
37 |
+
else:
|
38 |
+
print(f"β Deno installation failed: {result.stderr}")
|
39 |
+
return False
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
print(f"β Error installing Deno: {e}")
|
43 |
+
return False
|
44 |
+
|
45 |
+
# Install Deno before importing sandbox dependencies
|
46 |
+
print("π Checking Deno installation...")
|
47 |
+
deno_available = ensure_deno_installed()
|
48 |
|
49 |
# Import the CodeAct agent functionality
|
50 |
from agent import FileInjectedPyodideSandbox, create_pyodide_eval_fn, create_codeact
|
|
|
83 |
"""
|
84 |
Analyze the uploaded file using the new guided approach with user question
|
85 |
"""
|
86 |
+
global uploaded_file_path, deno_available
|
87 |
|
88 |
try:
|
89 |
if not uploaded_file_path or not os.path.exists(uploaded_file_path):
|
|
|
92 |
if not user_question or user_question.strip() == "":
|
93 |
user_question = "Provide a comprehensive analysis of this file including security, performance, and data insights."
|
94 |
|
95 |
+
# Check if Deno is available for sandbox operations
|
96 |
+
if not deno_available:
|
97 |
+
return """β Deno runtime not available. This is required for code execution in the sandbox.
|
98 |
+
|
99 |
+
π Troubleshooting:
|
100 |
+
1. This usually happens on deployment platforms that don't have Deno pre-installed
|
101 |
+
2. The app attempted to install Deno automatically but failed
|
102 |
+
3. Try restarting the space or contact support
|
103 |
+
|
104 |
+
π Alternative: You can still upload files, but advanced code analysis may be limited."""
|
105 |
+
|
106 |
# Use the new guided analysis approach
|
107 |
result = analyze_file_with_guidance_sync(uploaded_file_path, user_question)
|
108 |
return result
|
109 |
|
110 |
except Exception as e:
|
111 |
+
error_msg = str(e)
|
112 |
+
if "Deno" in error_msg or "deno" in error_msg:
|
113 |
+
return f"""β Deno-related error in analysis: {error_msg}
|
114 |
+
|
115 |
+
π§ This appears to be a Deno runtime issue. The sandbox requires Deno for code execution.
|
116 |
+
Try restarting the application or contact support if this persists."""
|
117 |
+
else:
|
118 |
+
return f"β Error in guided analysis: {error_msg}"
|
119 |
|
120 |
async def analyze_uploaded_file():
|
121 |
"""Legacy function - kept for backward compatibility"""
|
install_deno.sh
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
echo "π§ Installing Deno for PyodideSandbox..."
|
4 |
+
|
5 |
+
# Install Deno
|
6 |
+
curl -fsSL https://deno.land/install.sh | sh
|
7 |
+
|
8 |
+
# Add Deno to PATH for the current session
|
9 |
+
export DENO_INSTALL="/home/user/.deno"
|
10 |
+
export PATH="$DENO_INSTALL/bin:$PATH"
|
11 |
+
|
12 |
+
# Verify installation
|
13 |
+
echo "β
Deno installed at: $(which deno)"
|
14 |
+
echo "β
Deno version: $(deno --version)"
|
15 |
+
|
16 |
+
echo "π Starting Python application..."
|
17 |
+
python app.py
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
curl
|
2 |
+
unzip
|