Download app.py from Atulit23/RealVFake: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/Atulit23/RealVFake/resolve/main/app.py
- Command line
-
hf download hf://spaces/Atulit23/RealVFake/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Atulit23/RealVFake/resolve/main/app.py
1.25 kB
| 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') |