Sirawitch commited on
Commit
cc25ca0
·
verified ·
1 Parent(s): 8eb97be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -0
app.py CHANGED
@@ -3,18 +3,33 @@ from pydantic import BaseModel
3
  from typing import Optional
4
  from huggingface_hub import InferenceClient
5
  import uvicorn
 
6
 
7
  app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
8
  client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")
9
 
 
10
  class Query(BaseModel):
11
  queryResult: Optional[dict] = None
12
  queryText: Optional[str] = None
13
 
 
14
  @app.get("/")
15
  async def root():
16
  return {"message": "Welcome to the API"}
17
 
 
18
  @app.post("/webhook")
19
  async def webhook(query: Query):
20
  try:
@@ -41,5 +56,6 @@ async def webhook(query: Query):
41
  except Exception as e:
42
  raise HTTPException(status_code=500, detail=str(e))
43
 
 
44
  if __name__ == "__main__":
45
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
3
  from typing import Optional
4
  from huggingface_hub import InferenceClient
5
  import uvicorn
6
+ from fastapi.middleware.cors import CORSMiddleware
7
 
8
  app = FastAPI()
9
+
10
+ # CORS middleware
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Hugging Face client
20
  client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")
21
 
22
+ # Pydantic model for query
23
  class Query(BaseModel):
24
  queryResult: Optional[dict] = None
25
  queryText: Optional[str] = None
26
 
27
+ # Root route
28
  @app.get("/")
29
  async def root():
30
  return {"message": "Welcome to the API"}
31
 
32
+ # Webhook route
33
  @app.post("/webhook")
34
  async def webhook(query: Query):
35
  try:
 
56
  except Exception as e:
57
  raise HTTPException(status_code=500, detail=str(e))
58
 
59
+ # Run the app
60
  if __name__ == "__main__":
61
  uvicorn.run(app, host="0.0.0.0", port=7860)