Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,18 @@ from fastapi import FastAPI, Request
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
# Allow
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
| 11 |
allow_origins=["*"],
|
|
@@ -14,18 +22,10 @@ app.add_middleware(
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# SYSTEM PROMPT
|
| 18 |
-
SYSTEM_PROMPT = (
|
| 19 |
-
"You are BLACKLIGHT, a precision AI created by v0id under AWAKEN CULT VISIONS. "
|
| 20 |
-
"Respond in a brutalist, minimal, and precise style."
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
# Health check route for Hugging Face Space status
|
| 24 |
@app.get("/")
|
| 25 |
def health_check():
|
| 26 |
return {"status": "ok", "message": "BLACKLIGHT API running"}
|
| 27 |
|
| 28 |
-
# Prediction endpoint
|
| 29 |
@app.post("/predict")
|
| 30 |
async def predict(request: Request):
|
| 31 |
data = await request.json()
|
|
@@ -34,24 +34,26 @@ async def predict(request: Request):
|
|
| 34 |
if not user_prompt:
|
| 35 |
return {"error": "No prompt provided"}
|
| 36 |
|
| 37 |
-
# Merge system prompt
|
| 38 |
full_prompt = f"{SYSTEM_PROMPT}\n\nUser: {user_prompt}"
|
| 39 |
|
| 40 |
-
# Call the hosted HF Space directly (replace with your actual text space)
|
| 41 |
-
HF_SPACE_API = "https://v0idalism-blacklight-text.hf.space/run/predict"
|
| 42 |
-
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
return {"error": f"Failed to reach HF Space: {str(e)}"}
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Extract text from HF Space response
|
| 52 |
try:
|
| 53 |
output_text = result["data"][0]
|
|
|
|
|
|
|
| 54 |
except (KeyError, IndexError, TypeError):
|
| 55 |
-
return {"error": "Unexpected response format
|
| 56 |
|
| 57 |
return {"reply": output_text}
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
# Hugging Face Space API (replace with your model space URL if different)
|
| 6 |
+
HF_SPACE_API = "https://v0idalism-blacklight-text.hf.space/run/predict"
|
| 7 |
+
|
| 8 |
+
# System prompt
|
| 9 |
+
SYSTEM_PROMPT = (
|
| 10 |
+
"You are BLACKLIGHT, a precision AI created by v0id under AWAKEN CULT VISIONS. "
|
| 11 |
+
"Respond in a brutalist, minimal, and precise style."
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
+
# Allow frontend calls
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"],
|
|
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.get("/")
|
| 26 |
def health_check():
|
| 27 |
return {"status": "ok", "message": "BLACKLIGHT API running"}
|
| 28 |
|
|
|
|
| 29 |
@app.post("/predict")
|
| 30 |
async def predict(request: Request):
|
| 31 |
data = await request.json()
|
|
|
|
| 34 |
if not user_prompt:
|
| 35 |
return {"error": "No prompt provided"}
|
| 36 |
|
| 37 |
+
# Merge with system prompt
|
| 38 |
full_prompt = f"{SYSTEM_PROMPT}\n\nUser: {user_prompt}"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
+
response = requests.post(
|
| 42 |
+
HF_SPACE_API,
|
| 43 |
+
json={"data": [full_prompt]},
|
| 44 |
+
timeout=60
|
| 45 |
+
)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
+
result = response.json()
|
| 48 |
+
except Exception as e:
|
| 49 |
return {"error": f"Failed to reach HF Space: {str(e)}"}
|
| 50 |
|
| 51 |
+
# Extract first string result
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
output_text = result["data"][0]
|
| 54 |
+
if isinstance(output_text, list):
|
| 55 |
+
output_text = output_text[0]
|
| 56 |
except (KeyError, IndexError, TypeError):
|
| 57 |
+
return {"error": "Unexpected response format", "raw": result}
|
| 58 |
|
| 59 |
return {"reply": output_text}
|