danosethrus commited on
Commit
7ad9903
·
verified ·
1 Parent(s): bda1962

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -38
app.py CHANGED
@@ -2,58 +2,33 @@ import os
2
  import requests
3
  from fastapi import FastAPI, Request
4
  from fastapi.responses import JSONResponse, HTMLResponse
5
- from fastapi.middleware.cors import CORSMiddleware
6
 
7
  app = FastAPI()
8
 
9
- # This allows your index.html to talk to your Python backend
10
- app.add_middleware(
11
- CORSMiddleware,
12
- allow_origins=["*"],
13
- allow_methods=["*"],
14
- allow_headers=["*"],
15
- )
16
-
17
- # Pulls your secret token from the 'Settings' vault
18
  HF_TOKEN = os.environ.get("HF_TOKEN")
19
-
20
- # TARGET: Your actual Model (without the 's')
21
- # Change the URL to this:
22
  MODEL_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
23
 
24
  @app.get("/", response_class=HTMLResponse)
25
  async def home():
26
- try:
27
- with open("index.html") as f:
28
- return f.read()
29
- except FileNotFoundError:
30
- return "Error: index.html not found in Files tab!"
31
 
32
  @app.post("/ask")
33
  async def ask_ai(request: Request):
34
- user_data = await request.json()
 
35
 
36
- headers = {
37
- "Authorization": f"Bearer {HF_TOKEN}",
38
- "Content-Type": "application/json"
39
- }
40
-
41
- # 'wait_for_model' is the key: it prevents 503 errors while the brain loads
42
  payload = {
43
- "inputs": user_data.get("inputs", ""),
44
  "options": {"wait_for_model": True}
45
  }
46
 
47
- try:
48
- response = requests.post(MODEL_URL, headers=headers, json=payload, timeout=60)
49
-
50
- # This helps you see the result in the 'Logs' tab
51
- print(f"DEBUG: Status {response.status_code}")
52
- print(f"DEBUG: Response {response.text}")
53
-
54
- # If everything is okay, send the AI answer back to the website
55
- return JSONResponse(content=response.json(), status_code=response.status_code)
56
 
57
- except Exception as e:
58
- print(f"CRITICAL ERROR: {str(e)}")
59
- return JSONResponse({"error": "The bridge is broken. Check Logs."}, status_code=500)
 
2
  import requests
3
  from fastapi import FastAPI, Request
4
  from fastapi.responses import JSONResponse, HTMLResponse
 
5
 
6
  app = FastAPI()
7
 
 
 
 
 
 
 
 
 
 
8
  HF_TOKEN = os.environ.get("HF_TOKEN")
9
+ # The standard address for a text-generation model
 
 
10
  MODEL_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
11
 
12
  @app.get("/", response_class=HTMLResponse)
13
  async def home():
14
+ with open("index.html") as f:
15
+ return f.read()
 
 
 
16
 
17
  @app.post("/ask")
18
  async def ask_ai(request: Request):
19
+ data = await request.json()
20
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
21
 
22
+ # We add 'wait_for_model' so it doesn't error out while loading the 4GB file
 
 
 
 
 
23
  payload = {
24
+ "inputs": data.get("inputs", ""),
25
  "options": {"wait_for_model": True}
26
  }
27
 
28
+ response = requests.post(MODEL_URL, headers=headers, json=payload)
29
+
30
+ # This will show the real answer or error in your Logs
31
+ print(f"DEBUG Status: {response.status_code}")
32
+ print(f"DEBUG Response: {response.text}")
 
 
 
 
33
 
34
+ return JSONResponse(content=response.json())