Upload 12 files
Browse files
app.py
CHANGED
|
@@ -2,17 +2,14 @@
|
|
| 2 |
import os
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
-
from fastapi import FastAPI
|
| 6 |
-
from fastapi.responses import FileResponse
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from PIL import Image
|
| 10 |
-
from jade.core import JadeAgent
|
| 11 |
-
from code_jade.core import CodeJadeAgent
|
| 12 |
|
| 13 |
print("Iniciando a J.A.D.E. com FastAPI...")
|
| 14 |
agent = JadeAgent()
|
| 15 |
-
code_agent = CodeJadeAgent()
|
| 16 |
print("J.A.D.E. pronta para receber requisições.")
|
| 17 |
|
| 18 |
app = FastAPI(title="J.A.D.E. API")
|
|
@@ -25,68 +22,6 @@ class UserRequest(BaseModel):
|
|
| 25 |
user_input: str
|
| 26 |
image_base64: str | None = None
|
| 27 |
|
| 28 |
-
class CodeRequest(BaseModel):
|
| 29 |
-
user_input: str
|
| 30 |
-
|
| 31 |
-
def get_workspace_files():
|
| 32 |
-
workspace = code_agent.cfg.get("work_dir", "./workspace")
|
| 33 |
-
if not os.path.exists(workspace):
|
| 34 |
-
return set()
|
| 35 |
-
return set(os.listdir(workspace))
|
| 36 |
-
|
| 37 |
-
@app.post("/code/chat")
|
| 38 |
-
def handle_code_chat(request: CodeRequest):
|
| 39 |
-
try:
|
| 40 |
-
# Detect existing files
|
| 41 |
-
files_before = get_workspace_files()
|
| 42 |
-
|
| 43 |
-
# Run agent
|
| 44 |
-
response = code_agent.chat_loop(request.user_input)
|
| 45 |
-
|
| 46 |
-
# Detect new files
|
| 47 |
-
files_after = get_workspace_files()
|
| 48 |
-
new_files = files_after - files_before
|
| 49 |
-
|
| 50 |
-
generated_images = []
|
| 51 |
-
workspace = code_agent.cfg.get("work_dir", "./workspace")
|
| 52 |
-
|
| 53 |
-
for filename in new_files:
|
| 54 |
-
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
|
| 55 |
-
file_path = os.path.join(workspace, filename)
|
| 56 |
-
try:
|
| 57 |
-
with open(file_path, "rb") as img_file:
|
| 58 |
-
b64_data = base64.b64encode(img_file.read()).decode('utf-8')
|
| 59 |
-
generated_images.append({
|
| 60 |
-
"filename": filename,
|
| 61 |
-
"b64": b64_data
|
| 62 |
-
})
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"Error reading generated image {filename}: {e}")
|
| 65 |
-
|
| 66 |
-
return {
|
| 67 |
-
"success": True,
|
| 68 |
-
"bot_response": response,
|
| 69 |
-
"generated_images": generated_images
|
| 70 |
-
}
|
| 71 |
-
except Exception as e:
|
| 72 |
-
print(f"Erro crítico no endpoint /code/chat: {e}")
|
| 73 |
-
return {"success": False, "error": str(e)}
|
| 74 |
-
|
| 75 |
-
@app.get("/download/{filename}")
|
| 76 |
-
def download_file(filename: str):
|
| 77 |
-
# Sanitize and check existence in workspace
|
| 78 |
-
workspace = code_agent.cfg.get("work_dir", "./workspace")
|
| 79 |
-
file_path = os.path.join(workspace, filename)
|
| 80 |
-
|
| 81 |
-
# Basic path traversal protection
|
| 82 |
-
if not os.path.abspath(file_path).startswith(os.path.abspath(workspace)):
|
| 83 |
-
raise HTTPException(status_code=403, detail="Access denied")
|
| 84 |
-
|
| 85 |
-
if os.path.exists(file_path) and os.path.isfile(file_path):
|
| 86 |
-
return FileResponse(file_path, filename=filename)
|
| 87 |
-
else:
|
| 88 |
-
raise HTTPException(status_code=404, detail="File not found")
|
| 89 |
-
|
| 90 |
@app.post("/chat")
|
| 91 |
def handle_chat(request: UserRequest):
|
| 92 |
try:
|
|
|
|
| 2 |
import os
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
+
from fastapi import FastAPI
|
|
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from PIL import Image
|
| 9 |
+
from jade.core import JadeAgent
|
|
|
|
| 10 |
|
| 11 |
print("Iniciando a J.A.D.E. com FastAPI...")
|
| 12 |
agent = JadeAgent()
|
|
|
|
| 13 |
print("J.A.D.E. pronta para receber requisições.")
|
| 14 |
|
| 15 |
app = FastAPI(title="J.A.D.E. API")
|
|
|
|
| 22 |
user_input: str
|
| 23 |
image_base64: str | None = None
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.post("/chat")
|
| 26 |
def handle_chat(request: UserRequest):
|
| 27 |
try:
|