Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import uvicorn | |
| from agent.core import CodeAgent | |
| # Agent nur initialisieren, wenn aktiviert | |
| AGENT_ACTIVE = True # Auf False setzen zum Deaktivieren | |
| if AGENT_ACTIVE: | |
| # Agenten Instanziierung - Umgebung für Agent aufbauen! | |
| agent = CodeAgent(api_key=None, dummy_mode=True, project="bild") | |
| print("✅ Agent aktiviert") | |
| else: | |
| agent = None # Agent deaktiviert | |
| print("⚠️ Agent ist deaktiviert - API läuft im Dummy-Modus") | |
| # ============================================ | |
| # FASTAPI FÜR PROJEKTE | |
| # ============================================ | |
| app = FastAPI(title="Code-Agent API") | |
| class PatchRequest(BaseModel): | |
| project: str | |
| file: str | |
| line: int | |
| content: str | |
| api_key: str | |
| # Einfache API-Key Validierung (später erweiterbar) | |
| API_KEYS = { | |
| "bildgenerierung": "test-key-123", | |
| "finanzen": "test-key-456", | |
| "hr": "test-key-789" | |
| } | |
| async def chat_with_agent(request: dict): | |
| """ | |
| Chat-Endpoint für Projekte | |
| """ | |
| # 1. API-Key prüfen | |
| api_key = request.get("api_key") | |
| project = request.get("project") | |
| message = request.get("message", "") | |
| expected_key = API_KEYS.get(project) | |
| if not expected_key or expected_key != api_key: | |
| raise HTTPException(403, "❌ Unauthorized") | |
| # 2. Agent antworten lassen | |
| try: | |
| # Hier deine Agent-Logik! | |
| response = agent.handle_message(message, []) | |
| return { | |
| "response": response, | |
| "project": project | |
| } | |
| except Exception as e: | |
| raise HTTPException(500, f"❌ Fehler: {str(e)}") | |
| async def patch_code(request: PatchRequest): | |
| """ | |
| POST-Endpoint für Code-Änderungen von Projekten | |
| """ | |
| # 1. API-Key prüfen | |
| expected_key = API_KEYS.get(request.project) | |
| if not expected_key or expected_key != request.api_key: | |
| raise HTTPException(403, "❌ Unauthorized: Falscher API-Key") | |
| print(f"📡 API-Aufruf von Projekt: {request.project}") | |
| print(f" Datei: {request.file}, Zeile: {request.line}") | |
| # 2. Agent ausführen | |
| try: | |
| result = agent.handle_patch( | |
| project=request.project, | |
| file=request.file, | |
| line=request.line, | |
| content=request.content | |
| ) | |
| # 3. Erfolg zurückgeben | |
| return { | |
| "status": "success", | |
| "result": result, | |
| "project": request.project, | |
| "file": request.file | |
| } | |
| except Exception as e: | |
| raise HTTPException(500, f"❌ Agent-Fehler: {str(e)}") | |
| async def health_check(): | |
| """Monitoring-Endpoint""" | |
| return { | |
| "status": "healthy", | |
| "agent": "running", | |
| "projects": list(API_KEYS.keys()) | |
| } | |
| # ============================================ | |
| # 6. START (angepasst für HF) | |
| # ============================================ | |
| if __name__ == "__main__": | |
| uvicorn.run( | |
| "app:app", # Wichtig: "app:app" für HF! | |
| host="0.0.0.0", | |
| port=7860, | |
| reload=False | |
| ) |