v0idalism commited on
Commit
ebf19c8
·
verified ·
1 Parent(s): a95a93d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -2,10 +2,18 @@ from fastapi import FastAPI, Request
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import requests
4
 
5
- # FastAPI app (must be named `app` so HF detects it)
 
 
 
 
 
 
 
 
6
  app = FastAPI()
7
 
8
- # Allow all origins so your website frontend can call this API
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 with user input
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
- r = requests.post(HF_SPACE_API, json={"data": [full_prompt]}, timeout=60)
45
- r.raise_for_status()
46
- except requests.RequestException as e:
 
 
 
 
 
47
  return {"error": f"Failed to reach HF Space: {str(e)}"}
48
 
49
- result = r.json()
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 from HF Space", "raw": result}
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}