amirkhanbloch commited on
Commit
a327c91
·
verified ·
1 Parent(s): 6779a56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -3
app.py CHANGED
@@ -1,7 +1,38 @@
1
  from fastapi import FastAPI
 
 
 
 
 
2
 
 
3
  app = FastAPI()
4
 
5
- @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ import requests
3
+ import io
4
+ from PIL import Image
5
+ from fastapi.responses import StreamingResponse
6
+ import os
7
 
8
+ # Initialize the FastAPI app
9
  app = FastAPI()
10
 
11
+ # Get the Hugging Face API key from environment variables
12
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
13
+ HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
14
+
15
+ headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
16
+
17
+ # Function to query Hugging Face model API
18
+ def query(payload):
19
+ response = requests.post(API_URL, headers=headers, json=payload)
20
+ return response.content
21
+
22
+ # FastAPI route to return the image
23
+ @app.get("/generate-image")
24
+ def generate_image():
25
+ # Send a request to the Hugging Face model
26
+ image_bytes = query({"inputs": "cat with dog"})
27
+
28
+ # Open the image
29
+ image = Image.open(io.BytesIO(image_bytes))
30
+
31
+ # Create a StreamingResponse to return the image as a response
32
+ img_byte_arr = io.BytesIO()
33
+ image.save(img_byte_arr, format='PNG')
34
+ img_byte_arr.seek(0)
35
+
36
+ return StreamingResponse(img_byte_arr, media_type="image/png")
37
+
38
+