danosethrus commited on
Commit
09ba767
·
verified ·
1 Parent(s): e32c63f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -18
app.py CHANGED
@@ -1,31 +1,23 @@
1
- import os
2
- import requests
3
  from fastapi import FastAPI, Request
4
- from fastapi.responses import JSONResponse
5
- from fastapi.middleware.cors import CORSMiddleware
 
 
6
 
7
  app = FastAPI()
8
 
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"],
12
- allow_methods=["*"],
13
- allow_headers=["*"],
14
- )
15
-
16
- # NO MORE MANUALLY PASTED TOKENS!
17
- # Hugging Face provides this automatically if the Space is yours.
18
  HF_TOKEN = os.environ.get("HF_TOKEN")
19
  API_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
20
 
 
 
 
 
 
21
  @app.post("/ask")
22
  async def ask_ai(request: Request):
23
  data = await request.json()
24
-
25
- # If the secret isn't set, this will tell us in the logs
26
- if not HF_TOKEN:
27
- return JSONResponse(content={"error": "Secret HF_TOKEN not found in Settings"}, status_code=500)
28
-
29
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
30
  response = requests.post(API_URL, headers=headers, json=data)
31
  return JSONResponse(content=response.json())
 
 
 
1
  from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ import requests
5
+ import os
6
 
7
  app = FastAPI()
8
 
9
+ # Your Secret Token (Make sure this is in your Space Secrets!)
 
 
 
 
 
 
 
 
10
  HF_TOKEN = os.environ.get("HF_TOKEN")
11
  API_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
12
 
13
+ @app.get("/", response_class=HTMLResponse)
14
+ async def read_index():
15
+ with open("index.html", "r") as f:
16
+ return f.read()
17
+
18
  @app.post("/ask")
19
  async def ask_ai(request: Request):
20
  data = await request.json()
 
 
 
 
 
21
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
22
  response = requests.post(API_URL, headers=headers, json=data)
23
  return JSONResponse(content=response.json())