test1 / demo.py
ml13571's picture
Update demo.py
7c662c7 verified
Raw
History Blame Contribute Delete
3.43 kB
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):
# 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/deepfake_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"}
status = json_result.get("result").get("status")
confidence = json_result.get("result").get("confidence")
prediction = json_result.get("result").get("prediction")
similarity = json_result.get("result").get("similarity")
media_type = json_result.get("result").get("media_type")
if status == "Not AI Generated":
status_html = f'<span style="color:green; font-weight:bold;">{status}</span>'
confidence_html = f'<span style="color:green; font-weight:bold;">{confidence} %</span>'
else:
status_html = f'<span style="color:red; font-weight:bold;">{status}</span>'
confidence_html = f'<span style="color:red; font-weight:bold;">{confidence} %</span>'
html = ("<table>"
"<tr>"
"<th>Evaluation Field</th>"
"<th>Value</th>"
"</tr>"
"<tr>"
"<td>Result</td>"
"<td>{status_html}</td>"
"</tr>"
"<tr>"
"<td>Confidence</td>"
"<td>{confidence_html}</td>"
"</tr>"
"<tr>"
"<td>Similarity</td>"
"<td>{similarity} % matches to known AI images</td>"
"</tr>"
"<tr>"
"<td>Media Type</td>"
"<td>{media_type}</td>"
"</tr>"
"</table>".format(status_html=status_html, confidence_html=confidence_html, similarity=similarity, media_type=media_type))
if status == "Ok":
# Update json_result with the modified process_results
json_result["result"] = process_results
return html
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.HTML()
process_button.click(process_image, inputs=[image_input], outputs=[json_output])
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fml13571-test1.hf.space"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fml13571-test1.hf.space&label=VISITORS&countColor=%23263759" /></a>')
demo.launch(server_name="0.0.0.0", server_port=7860)