Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +4 -12
- main.py +40 -36
Dockerfile
CHANGED
|
@@ -2,20 +2,12 @@ FROM python:3.12-slim
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
# Install
|
| 6 |
-
RUN
|
| 7 |
-
RUN pip install uv
|
| 8 |
|
| 9 |
-
# Copy
|
| 10 |
-
COPY .
|
| 11 |
-
|
| 12 |
-
# Install ACE framework using uv (referencing the local pyproject.toml in the cloned repo)
|
| 13 |
-
# and install FastAPI components + boto3 for pydantic-ai bedrock support
|
| 14 |
-
RUN uv pip install --system fastapi uvicorn pydantic litellm boto3
|
| 15 |
-
# Since the cloned directory has pyproject.toml, we can install the local package
|
| 16 |
-
RUN uv pip install --system -e .
|
| 17 |
|
| 18 |
EXPOSE 7860
|
| 19 |
|
| 20 |
-
# We need the user to pass API keys in Space Secrets (e.g. OPENAI_API_KEY, GROQ_API_KEY)
|
| 21 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
# Install only what we need - lightweight and fast
|
| 6 |
+
RUN pip install fastapi uvicorn httpx pydantic
|
|
|
|
| 7 |
|
| 8 |
+
# Copy our application
|
| 9 |
+
COPY main.py .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
EXPOSE 7860
|
| 12 |
|
|
|
|
| 13 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
CHANGED
|
@@ -1,16 +1,10 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import os
|
| 5 |
-
import
|
| 6 |
-
import asyncio
|
| 7 |
|
| 8 |
-
|
| 9 |
-
import sys
|
| 10 |
-
sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
|
| 11 |
-
from ace import ACELiteLLM
|
| 12 |
-
|
| 13 |
-
app = FastAPI(title="Logic Engine with ACE")
|
| 14 |
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
|
@@ -22,39 +16,49 @@ app.add_middleware(
|
|
| 22 |
|
| 23 |
class ChatRequest(BaseModel):
|
| 24 |
prompt: str
|
| 25 |
-
model: str = "gpt-4o-mini"
|
| 26 |
-
|
| 27 |
-
# Configuration for Node 1 (Redis) and External Tools
|
| 28 |
-
REDIS_URL = os.environ.get("REDIS_URL", "https://augment17-redis-memory-core.hf.space")
|
| 29 |
-
VECTOR_DB_URL = os.environ.get("VECTOR_DB_URL", "")
|
| 30 |
-
|
| 31 |
-
# Initialize ACE agent (using LiteLLM under the hood to support 100+ providers)
|
| 32 |
-
agent = ACELiteLLM(model=os.environ.get("DEFAULT_MODEL", "gpt-4o-mini"))
|
| 33 |
|
| 34 |
@app.post("/chat")
|
| 35 |
async def chat_endpoint(request: ChatRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
raise HTTPException(status_code=500, detail=str(e))
|
| 57 |
|
| 58 |
@app.get("/health")
|
| 59 |
def health():
|
| 60 |
-
return {"status": "
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import os
|
| 5 |
+
import httpx
|
|
|
|
| 6 |
|
| 7 |
+
app = FastAPI(title="Logic Engine")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
|
|
|
| 16 |
|
| 17 |
class ChatRequest(BaseModel):
|
| 18 |
prompt: str
|
| 19 |
+
model: str = "gpt-4o-mini"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.post("/chat")
|
| 22 |
async def chat_endpoint(request: ChatRequest):
|
| 23 |
+
api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("GROQ_API_KEY")
|
| 24 |
+
|
| 25 |
+
if not api_key:
|
| 26 |
+
# Graceful fallback when no API key is configured
|
| 27 |
+
return {
|
| 28 |
+
"response": (
|
| 29 |
+
f"[Logic Engine] Received your message: \"{request.prompt}\"\n\n"
|
| 30 |
+
"No LLM API key is configured yet. Please add OPENAI_API_KEY or GROQ_API_KEY "
|
| 31 |
+
"via the Providers panel in the UI or in the Space Secrets settings on Hugging Face."
|
| 32 |
+
),
|
| 33 |
+
"doc": True
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
try:
|
| 37 |
+
# Use OpenAI-compatible API via httpx (works with OpenAI & Groq)
|
| 38 |
+
base_url = "https://api.groq.com/openai/v1" if os.environ.get("GROQ_API_KEY") else "https://api.openai.com/v1"
|
| 39 |
+
chosen_key = os.environ.get("GROQ_API_KEY") or os.environ.get("OPENAI_API_KEY")
|
| 40 |
+
model = "llama-3.1-8b-instant" if os.environ.get("GROQ_API_KEY") else request.model
|
| 41 |
+
|
| 42 |
+
async with httpx.AsyncClient(timeout=30) as client:
|
| 43 |
+
resp = await client.post(
|
| 44 |
+
f"{base_url}/chat/completions",
|
| 45 |
+
headers={"Authorization": f"Bearer {chosen_key}", "Content-Type": "application/json"},
|
| 46 |
+
json={
|
| 47 |
+
"model": model,
|
| 48 |
+
"messages": [
|
| 49 |
+
{"role": "system", "content": "You are Manus, a helpful autonomous AI agent."},
|
| 50 |
+
{"role": "user", "content": request.prompt}
|
| 51 |
+
]
|
| 52 |
+
}
|
| 53 |
+
)
|
| 54 |
+
resp.raise_for_status()
|
| 55 |
+
data = resp.json()
|
| 56 |
+
reply = data["choices"][0]["message"]["content"]
|
| 57 |
+
return {"response": reply, "doc": True}
|
| 58 |
+
|
| 59 |
except Exception as e:
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
|
| 62 |
@app.get("/health")
|
| 63 |
def health():
|
| 64 |
+
return {"status": "Logic Engine Running"}
|