generated from westfarn/web_django_template
Initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import json
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import JsonResponse
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from social_ai.ollama import OllamaError, generate_social_post
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def api_generate(request):
|
||||
"""JSON endpoint to draft posts via Ollama. POST JSON: prompt, platform."""
|
||||
try:
|
||||
payload = json.loads(request.body.decode() or "{}")
|
||||
except json.JSONDecodeError:
|
||||
payload = request.POST.dict()
|
||||
prompt = (payload.get("prompt") or "").strip()
|
||||
if not prompt:
|
||||
return JsonResponse({"error": "prompt required"}, status=400)
|
||||
try:
|
||||
text = generate_social_post(
|
||||
prompt, platform=(payload.get("platform") or "").strip()
|
||||
)
|
||||
except OllamaError as exc:
|
||||
return JsonResponse({"error": str(exc)}, status=502)
|
||||
return JsonResponse({"text": text})
|
||||
Reference in New Issue
Block a user