Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,52 +9,43 @@ load_dotenv()
|
|
| 9 |
|
| 10 |
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 11 |
FLAREAI_API = os.getenv("FLAREAI_API")
|
| 12 |
-
VIDEO_API_URL = os.getenv("FLAREAI_VIDEO")
|
| 13 |
|
| 14 |
-
def
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
response = requests.post(VIDEO_API_URL, json=payload)
|
| 21 |
-
if response.status_code != 200:
|
| 22 |
-
return f"Video processing failed with status code {response.status_code}: {response.text}"
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
else:
|
| 28 |
-
buffered = BytesIO()
|
| 29 |
-
input_data.save(buffered, format="JPEG")
|
| 30 |
-
buffered.seek(0)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return f"Image upload failed with status code {response.status_code}: {response.text}"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
response.raise_for_status()
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
except requests.exceptions.RequestException as e:
|
| 56 |
return f"An error occurred: {e}", ""
|
| 57 |
|
|
|
|
|
|
|
| 58 |
css = """
|
| 59 |
.gradio-container {
|
| 60 |
font-family: 'Roboto', sans-serif;
|
|
@@ -123,13 +114,14 @@ css = """
|
|
| 123 |
}
|
| 124 |
"""
|
| 125 |
|
|
|
|
| 126 |
iface = gr.Interface(
|
| 127 |
-
fn=
|
| 128 |
-
inputs=
|
| 129 |
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 130 |
-
title="FlareAI Face Detection
|
| 131 |
-
description="
|
| 132 |
-
theme="huggingface",
|
| 133 |
css=css
|
| 134 |
)
|
| 135 |
|
|
|
|
| 9 |
|
| 10 |
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 11 |
FLAREAI_API = os.getenv("FLAREAI_API")
|
|
|
|
| 12 |
|
| 13 |
+
def detect_faces(image):
|
| 14 |
try:
|
| 15 |
+
buffered = BytesIO()
|
| 16 |
+
image.save(buffered, format="JPEG")
|
| 17 |
+
buffered.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
upload_url = "https://api.imgbb.com/1/upload"
|
| 20 |
+
files = {'image': buffered}
|
| 21 |
+
data = {"key": IMGBB_API_KEY}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
response = requests.post(upload_url, data=data, files=files)
|
| 24 |
+
if response.status_code != 200:
|
| 25 |
+
return f"Image upload failed with status code {response.status_code}: {response.text}"
|
| 26 |
|
| 27 |
+
image_url = response.json()["data"]["url"]
|
| 28 |
+
print(f"Image uploaded successfully: {image_url}")
|
|
|
|
| 29 |
|
| 30 |
+
response = requests.post(FLAREAI_API, json={"image_url": image_url})
|
| 31 |
+
response.raise_for_status()
|
|
|
|
| 32 |
|
| 33 |
+
result = response.json()
|
| 34 |
+
face_data = result.get("face_data", [])
|
| 35 |
+
detected_image_url = result.get("image_url", "")
|
| 36 |
|
| 37 |
+
if face_data:
|
| 38 |
+
face_details = "\n".join([f"Face {index + 1}: Age: {face['age']}, Emotion: {face['emotion']}, Gender: {face['gender']}, Race: {face['race']}" for index, face in enumerate(face_data)])
|
| 39 |
+
else:
|
| 40 |
+
face_details = "No faces detected."
|
| 41 |
|
| 42 |
+
result_image = Image.open(BytesIO(requests.get(detected_image_url).content))
|
| 43 |
+
return result_image, face_details
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
return f"An error occurred: {e}", ""
|
| 46 |
|
| 47 |
+
|
| 48 |
+
# Professional CSS to style the Gradio UI
|
| 49 |
css = """
|
| 50 |
.gradio-container {
|
| 51 |
font-family: 'Roboto', sans-serif;
|
|
|
|
| 114 |
}
|
| 115 |
"""
|
| 116 |
|
| 117 |
+
# Gradio Interface
|
| 118 |
iface = gr.Interface(
|
| 119 |
+
fn=detect_faces,
|
| 120 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 121 |
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 122 |
+
title="FlareAI Face Detection",
|
| 123 |
+
description="Made with ❤️ by Skycodee | Leverage advanced AI for face detection.",
|
| 124 |
+
theme="huggingface", # Using Huggingface theme for consistency
|
| 125 |
css=css
|
| 126 |
)
|
| 127 |
|