File size: 1,253 Bytes
9953840 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image
import requests
from io import BytesIO
from matplotlib import pyplot as plt
import numpy as np
import gradio as gr
import json
model = load_model('real-fake-best.h5')
def index(image_url):
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
img = np.array(img)
resize = tf.image.resize(img, (32, 32))
y_pred = model.predict(np.expand_dims(resize / 255, 0))
predictions = {"Fake": y_pred[0][0]*100, "Real": y_pred[0][1]*100}
print("Predictions:", y_pred)
predicted_class = np.argmax(y_pred)
print("Predicted Class:", predicted_class)
return json.dumps(predictions)
inputs_image_url = [
gr.Textbox(type="text", label="Image URL"),
]
outputs_result_dict = [
gr.Textbox(type="text", label="Result Dictionary"),
]
interface_image_url = gr.Interface(
fn=index,
inputs=inputs_image_url,
outputs=outputs_result_dict,
title="AI Image Detection",
cache_examples=False,
)
gr.TabbedInterface(
[interface_image_url],
tab_names=['Image inference']
).queue().launch()
# 0 -> AI
# 1 -> Real
# if y_pred > 0.5:
# print(f'REAL')
# else:
# print(f'AI') |