Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,32 @@
|
|
1 |
import os
|
|
|
2 |
import json
|
3 |
import requests
|
4 |
import gradio as gr
|
5 |
-
from huggingface_hub import hf_hub_download
|
6 |
from PIL import Image
|
7 |
|
8 |
# Environment Variables
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
10 |
HF_DATASET = os.getenv('HF_DATASET')
|
11 |
-
SPACE_SUBDOMAIN = os.
|
12 |
|
13 |
def get_image_with_auth(file_name):
|
14 |
"""Retrieve an image using Hugging Face's hub with authentication."""
|
15 |
image_path = hf_hub_download(repo_id=HF_DATASET, repo_type="dataset", filename=file_name, token=HF_TOKEN)
|
16 |
return Image.open(image_path)
|
17 |
|
18 |
-
def recognize_face(
|
19 |
"""
|
20 |
-
Function to send an image URL to the FastAPI backend and receive results.
|
21 |
"""
|
22 |
-
# Ensure the SPACE_SUBDOMAIN environment variable is set
|
23 |
-
if not SPACE_SUBDOMAIN:
|
24 |
-
raise ValueError("SPACE_SUBDOMAIN environment variable is not set")
|
25 |
-
|
26 |
-
# Construct the image URL using the full path
|
27 |
-
image_url = f"https://{SPACE_SUBDOMAIN}.hf.space/file={image_path}"
|
28 |
|
29 |
# Set the URL to your FastAPI endpoint
|
30 |
-
url = 'https://dwancin-face-match.hf.space/recognize/'
|
31 |
-
|
32 |
-
# Debugging: Print image URL and API URL
|
33 |
-
print(f"Image URL: {image_url}")
|
34 |
-
print(f"API URL: {url}")
|
35 |
|
36 |
# Prepare the payload with the image data and specify the type
|
37 |
payload = {
|
38 |
-
"image":
|
39 |
"type": "url"
|
40 |
}
|
41 |
|
@@ -44,19 +35,15 @@ def recognize_face(image_path):
|
|
44 |
"Authorization": f"Bearer {HF_TOKEN}"
|
45 |
}
|
46 |
|
47 |
-
# Debugging: Print payload and headers
|
48 |
-
print(f"Payload: {json.dumps(payload, indent=2)}")
|
49 |
-
print(f"Headers: {headers}")
|
50 |
-
|
51 |
# Send POST request to FastAPI server with the image data and type
|
52 |
response = requests.post(url, json=payload, headers=headers)
|
53 |
|
54 |
# Process response
|
55 |
if response.status_code == 200:
|
56 |
response_data = response.json()
|
57 |
-
|
58 |
-
if
|
59 |
-
image_file = get_image_with_auth(
|
60 |
formatted_json = json.dumps(response_data, indent=4)
|
61 |
info = f"```json\n{formatted_json}\n```"
|
62 |
print(formatted_json)
|
@@ -105,4 +92,4 @@ with gr.Blocks(
|
|
105 |
submit.click(fn=recognize_face, inputs=input_image, outputs=[output_image, output_info])
|
106 |
|
107 |
# Launch
|
108 |
-
demo.launch(show_api=False)
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
import json
|
4 |
import requests
|
5 |
import gradio as gr
|
6 |
+
from huggingface_hub import HfFileSystem, hf_hub_download
|
7 |
from PIL import Image
|
8 |
|
9 |
# Environment Variables
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
HF_DATASET = os.getenv('HF_DATASET')
|
12 |
+
SPACE_SUBDOMAIN = os.environ['SPACE_SUBDOMAIN']
|
13 |
|
14 |
def get_image_with_auth(file_name):
|
15 |
"""Retrieve an image using Hugging Face's hub with authentication."""
|
16 |
image_path = hf_hub_download(repo_id=HF_DATASET, repo_type="dataset", filename=file_name, token=HF_TOKEN)
|
17 |
return Image.open(image_path)
|
18 |
|
19 |
+
def recognize_face(image):
|
20 |
"""
|
21 |
+
Function to send either an image URL to the FastAPI backend and receive results.
|
22 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Set the URL to your FastAPI endpoint
|
25 |
+
url = 'https://dwancin-face-match-api.hf.space/recognize/'
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Prepare the payload with the image data and specify the type
|
28 |
payload = {
|
29 |
+
"image": f"https://{SPACE_SUBDOMAIN}.hf.space/file={image}",
|
30 |
"type": "url"
|
31 |
}
|
32 |
|
|
|
35 |
"Authorization": f"Bearer {HF_TOKEN}"
|
36 |
}
|
37 |
|
|
|
|
|
|
|
|
|
38 |
# Send POST request to FastAPI server with the image data and type
|
39 |
response = requests.post(url, json=payload, headers=headers)
|
40 |
|
41 |
# Process response
|
42 |
if response.status_code == 200:
|
43 |
response_data = response.json()
|
44 |
+
image_path = response_data.get('image')
|
45 |
+
if image_path:
|
46 |
+
image_file = get_image_with_auth(image_path)
|
47 |
formatted_json = json.dumps(response_data, indent=4)
|
48 |
info = f"```json\n{formatted_json}\n```"
|
49 |
print(formatted_json)
|
|
|
92 |
submit.click(fn=recognize_face, inputs=input_image, outputs=[output_image, output_info])
|
93 |
|
94 |
# Launch
|
95 |
+
demo.launch(show_api=False)
|