At41rv commited on
Commit
f7a70b1
·
verified ·
1 Parent(s): 12bd851

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import os
4
+
5
+ app = FastAPI()
6
+
7
+ # Example: Simple data model for requests
8
+ class Item(BaseModel):
9
+ name: str
10
+ description: str | None = None
11
+ price: float
12
+ tax: float | None = None
13
+
14
+ # Root endpoint
15
+ @app.get("/")
16
+ def read_root():
17
+ return {"message": "Hello from your Hugging Face Space backend!"}
18
+
19
+ # Example: An API endpoint that processes data
20
+ @app.post("/items/")
21
+ async def create_item(item: Item):
22
+ # Simulate some processing
23
+ processed_price = item.price * (1 + (item.tax or 0))
24
+ return {"item_name": item.name, "processed_price": processed_price, "status": "processed"}
25
+
26
+ # Example: Accessing a secret (from Hugging Face Space secrets)
27
+ @app.get("/secret-data/")
28
+ def get_secret_data():
29
+ secret_value = os.getenv("MY_SECRET_KEY", "Secret not found or not set")
30
+ return {"secret_info": f"The secret is: {secret_value}"}
31
+
32
+ # It's crucial for Hugging Face Spaces that your app listens on port 7860
33
+ # Uvicorn (ASGI server for FastAPI) will handle this, but ensure your Dockerfile
34
+ # and command are configured to expose this port.
35
+ if __name__ == "__main__":
36
+ import uvicorn
37
+ uvicorn.run(app, host="0.0.0.0", port=7860)