Spaces:
Running
Running
| import os | |
| import gradio as gr | |
| import requests | |
| import json | |
| import io | |
| from gradio.components import Image | |
| from PIL import Image as PILImage, ImageDraw, ImageFont # This import may be needed if you're processing images | |
| from PIL import Image | |
| def process_image(image, screenReplyThreshold, printedCopyThreshold, portraitReplaceThreshold): | |
| # Convert PIL image to bytes to send in POST request | |
| img_bytes = io.BytesIO() | |
| image.save(img_bytes, format="JPEG") | |
| img_bytes.seek(0) | |
| url = "http://127.0.0.1:9000/process_image" | |
| files = {'image': img_bytes} | |
| result = requests.post(url=url, files=files) | |
| if result.ok: | |
| json_result = result.json() | |
| if json_result.get("resultCode") == "Error": | |
| return {"resultCode": "Error", "result": "Failed to process image"} | |
| process_results = json_result.get("result") | |
| status = process_results.get("status") | |
| if status == "Ok": | |
| screenReply = process_results.get("screenReply") | |
| portraitReplace = process_results.get("portraitReplace") | |
| printedCopy = process_results.get("printedCopy") | |
| # Check for "Spoof" condition | |
| if screenReply < 0.5 or portraitReplace < 0.5 or printedCopy < 0.5: | |
| process_results["status"] = "Spoof" | |
| else: | |
| process_results["status"] = "Real" | |
| # Update json_result with the modified process_results | |
| json_result["result"] = process_results | |
| return json_result | |
| else: | |
| return {"resultCode": "Error", "result": result.text} | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image(type='pil') | |
| gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'], | |
| inputs=image_input) | |
| process_button = gr.Button("Process") | |
| with gr.Column(): | |
| json_output = gr.JSON() | |
| process_button.click(process_image, inputs=[image_input], outputs=[json_output]) | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |