Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tensorflow.keras import layers, models
|
| 6 |
+
import requests
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
class FoodQualityAI:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.model = None
|
| 14 |
+
self.image_size = (256, 256)
|
| 15 |
+
|
| 16 |
+
def test(self, image_url):
|
| 17 |
+
if self.model is None:
|
| 18 |
+
raise Exception("Model not loaded. Please load a model before testing.")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
response = requests.get(image_url)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
except Exception as ex:
|
| 24 |
+
raise Exception("Error downloading the image: " + str(ex))
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise Exception("Error processing the image: " + str(e))
|
| 30 |
+
|
| 31 |
+
img = img.resize(self.image_size)
|
| 32 |
+
img_array = np.array(img) / 255.0
|
| 33 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 34 |
+
|
| 35 |
+
prediction = self.model.predict(img_array)
|
| 36 |
+
|
| 37 |
+
class_names = [d for d in os.listdir(self.train_dir)
|
| 38 |
+
if os.path.isdir(os.path.join(self.train_dir, d))]
|
| 39 |
+
if not class_names:
|
| 40 |
+
num_classes = prediction.shape[1]
|
| 41 |
+
class_names = [f"class_{i}" for i in range(num_classes)]
|
| 42 |
+
|
| 43 |
+
predicted_idx = np.argmax(prediction)
|
| 44 |
+
predicted_class = class_names[predicted_idx]
|
| 45 |
+
confidence = float(np.max(prediction))
|
| 46 |
+
|
| 47 |
+
return predicted_class, confidence
|
| 48 |
+
|
| 49 |
+
def load(self, filename='food_quality_model.h5'):
|
| 50 |
+
try:
|
| 51 |
+
self.model = models.load_model(filename)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
raise Exception(f"Не удалсь загрузить модель ИИ {filename}: {str(e)}")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
food_quality_ai = FoodQualityAI()
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
food_quality_ai.load('food_quality_model.h5')
|
| 60 |
+
print("Model loaded successfully.")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print("Error loading model:", e)
|
| 63 |
+
sys.exit(1)
|
| 64 |
+
|
| 65 |
+
def classify_image_url(image_url):
|
| 66 |
+
try:
|
| 67 |
+
predicted_class, confidence = food_quality_ai.test(image_url)
|
| 68 |
+
return f"Модель думает что это: {predicted_class}\nС вероятностю в: {confidence:.2f}"
|
| 69 |
+
except Exception as err:
|
| 70 |
+
return f"Ошибка: {err}"
|
| 71 |
+
|
| 72 |
+
iface = gr.Interface(
|
| 73 |
+
fn=classify_image_url,
|
| 74 |
+
inputs=gr.Textbox(label="Ссылка на тестовую картинку", placeholder="Введите ссылку..."),
|
| 75 |
+
outputs=gr.Textbox(label="Мнение модели"),
|
| 76 |
+
title="ИИ для определения состояния еды.",
|
| 77 |
+
description="Вставьте ссылку чтобы проверить как работает модель."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
iface.launch()
|