| import tempfile | |
| from fastapi import FastAPI, File, UploadFile | |
| from gradio_client import Client, handle_file | |
| app = FastAPI() | |
| client = Client("kevansoon/FaceRecognition-LivenessDetection-Demo") | |
| async def face_recognition_post(image: UploadFile = File(...)): | |
| # Save uploaded image to temp file | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file: | |
| content = await image.read() | |
| temp_file.write(content) | |
| temp_filepath = temp_file.name | |
| # Hardcoded second image URL | |
| image2_url = "https://qvnhhditkzzeudppuezf.supabase.co/storage/v1/object/public/post-images/post-images/1752289670997-kevan.jpg" | |
| # Call Gradio client with temp file path and URL wrapped in handle_file | |
| result = client.predict( | |
| frame1=handle_file(temp_filepath), # Pass local file path wrapped with handle_file | |
| frame2=handle_file(image2_url), # Pass URL wrapped with handle_file | |
| api_name="/face_compare" | |
| ) | |
| # Optional: remove temp file after use if desired | |
| # import os | |
| # os.unlink(temp_filepath) | |
| # Parse the result string as before | |
| str_ = result | |
| start_index = str_.find("Similarity") | |
| sliced_str = str_[start_index:] if start_index != -1 else "" | |
| import re | |
| decimal_match = re.search(r"<td>(0?\.\d+)<\/td>", sliced_str) | |
| decimal_number = float(decimal_match.group(1)) if decimal_match else None | |
| print("Similarity decimal:", decimal_number) | |
| return {"result":str(decimal_number)} | |