Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Securely fetch your API keys and endpoints
|
| 11 |
+
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 12 |
+
CIVILITY_API_URL = os.getenv("FLAREAI_API")
|
| 13 |
+
|
| 14 |
+
def detect_faces(image):
|
| 15 |
+
"""Send the image to the API and return the processed image and face data."""
|
| 16 |
+
try:
|
| 17 |
+
|
| 18 |
+
buffered = BytesIO()
|
| 19 |
+
image.save(buffered, format="JPEG")
|
| 20 |
+
buffered.seek(0)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
upload_url = "https://api.imgbb.com/1/upload"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
files = {'image': buffered}
|
| 27 |
+
data = {"key": IMGBB_API_KEY}
|
| 28 |
+
|
| 29 |
+
response = requests.post(upload_url, data=data, files=files)
|
| 30 |
+
|
| 31 |
+
if response.status_code != 200:
|
| 32 |
+
return f"Image upload failed with status code {response.status_code}: {response.text}"
|
| 33 |
+
|
| 34 |
+
image_url = response.json()["data"]["url"]
|
| 35 |
+
print(f"Image uploaded successfully: {image_url}")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
response = requests.post(CIVILITY_API_URL, json={"image_url": image_url})
|
| 39 |
+
response.raise_for_status()
|
| 40 |
+
|
| 41 |
+
result = response.json()
|
| 42 |
+
face_data = result.get("face_data", [])
|
| 43 |
+
detected_image_url = result.get("image_url", "")
|
| 44 |
+
|
| 45 |
+
if face_data:
|
| 46 |
+
face_details = "\n".join([
|
| 47 |
+
f"Face {index + 1}: Age: {face['age']}, Emotion: {face['emotion']}, Gender: {face['gender']}, Race: {face['race']}"
|
| 48 |
+
for index, face in enumerate(face_data)
|
| 49 |
+
])
|
| 50 |
+
else:
|
| 51 |
+
face_details = "No faces detected."
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
result_image = Image.open(BytesIO(requests.get(detected_image_url).content))
|
| 55 |
+
|
| 56 |
+
return result_image, face_details
|
| 57 |
+
except requests.exceptions.RequestException as e:
|
| 58 |
+
return f"An error occurred: {e}", ""
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
iface = gr.Interface(
|
| 62 |
+
fn=detect_faces,
|
| 63 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 64 |
+
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 65 |
+
title="FlareAI Face Detection",
|
| 66 |
+
description="Upload an image to detect faces using the CivilityAI API.",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
iface.launch()
|