Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Модельдер: ашық қолжетімді
|
| 8 |
+
# ----------------------------
|
| 9 |
+
|
| 10 |
+
# 1️⃣ Жеміс/өсімдік классификациясы
|
| 11 |
+
FRUIT_MODEL = "nateraw/vit-base-fruit-classification"
|
| 12 |
+
fruit_extractor = AutoFeatureExtractor.from_pretrained(FRUIT_MODEL)
|
| 13 |
+
fruit_model = AutoModelForImageClassification.from_pretrained(FRUIT_MODEL)
|
| 14 |
+
fruit_classifier = pipeline("image-classification", model=fruit_model, feature_extractor=fruit_extractor)
|
| 15 |
+
|
| 16 |
+
# 2️⃣ Қоқыс классификациясы
|
| 17 |
+
TRASH_MODEL = "keremberke/vit-base-patch16-224-trash"
|
| 18 |
+
trash_extractor = AutoFeatureExtractor.from_pretrained(TRASH_MODEL)
|
| 19 |
+
trash_model = AutoModelForImageClassification.from_pretrained(TRASH_MODEL)
|
| 20 |
+
trash_classifier = pipeline("image-classification", model=trash_model, feature_extractor=trash_extractor)
|
| 21 |
+
|
| 22 |
+
# 3️⃣ Аударма (2 модель)
|
| 23 |
+
# Қазақша - ағылшынша
|
| 24 |
+
TRANSLATE_MODEL_KZ_EN = "Helsinki-NLP/opus-mt-kaz-en"
|
| 25 |
+
tokenizer_kz_en = AutoTokenizer.from_pretrained(TRANSLATE_MODEL_KZ_EN)
|
| 26 |
+
model_kz_en = AutoModelForSeq2SeqLM.from_pretrained(TRANSLATE_MODEL_KZ_EN)
|
| 27 |
+
translator_kz_en = pipeline("translation", model=model_kz_en, tokenizer=tokenizer_kz_en)
|
| 28 |
+
|
| 29 |
+
# Қазақша - Монголша
|
| 30 |
+
TRANSLATE_MODEL_KZ_MN = "Helsinki-NLP/opus-mt-kaz-mon"
|
| 31 |
+
tokenizer_kz_mn = AutoTokenizer.from_pretrained(TRANSLATE_MODEL_KZ_MN)
|
| 32 |
+
model_kz_mn = AutoModelForSeq2SeqLM.from_pretrained(TRANSLATE_MODEL_KZ_MN)
|
| 33 |
+
translator_kz_mn = pipeline("translation", model=model_kz_mn, tokenizer=tokenizer_kz_mn)
|
| 34 |
+
|
| 35 |
+
# ----------------------------
|
| 36 |
+
# Функциялар
|
| 37 |
+
# ----------------------------
|
| 38 |
+
|
| 39 |
+
def classify_fruit(img):
|
| 40 |
+
results = fruit_classifier(img)
|
| 41 |
+
return {r['label']: float(r['score']) for r in results}
|
| 42 |
+
|
| 43 |
+
def classify_trash(img):
|
| 44 |
+
results = trash_classifier(img)
|
| 45 |
+
return {r['label']: float(r['score']) for r in results}
|
| 46 |
+
|
| 47 |
+
def translate_text(text):
|
| 48 |
+
en = translator_kz_en(text)[0]['translation_text']
|
| 49 |
+
mn = translator_kz_mn(text)[0]['translation_text']
|
| 50 |
+
return en, mn
|
| 51 |
+
|
| 52 |
+
# ----------------------------
|
| 53 |
+
# Gradio интерфейс
|
| 54 |
+
# ----------------------------
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("## 🌱 EcoVision: Жеміс/өсімдік, қоқыс классификациясы + аударма")
|
| 58 |
+
|
| 59 |
+
with gr.Tab("Суретке түсіру"):
|
| 60 |
+
with gr.Row():
|
| 61 |
+
fruit_input = gr.Image(label="Жеміс/өсімдік суреті", type="pil")
|
| 62 |
+
fruit_output = gr.Label(label="Жеміс/өсімдік нәтижесі")
|
| 63 |
+
fruit_btn = gr.Button("Тексеру")
|
| 64 |
+
fruit_btn.click(classify_fruit, inputs=fruit_input, outputs=fruit_output)
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
trash_input = gr.Image(label="Қоқыс суреті", type="pil")
|
| 68 |
+
trash_output = gr.Label(label="Қоқыс нәтижесі")
|
| 69 |
+
trash_btn = gr.Button("Тексеру")
|
| 70 |
+
trash_btn.click(classify_trash, inputs=trash_input, outputs=trash_output)
|
| 71 |
+
|
| 72 |
+
with gr.Tab("Аударма"):
|
| 73 |
+
text_input = gr.Textbox(label="Мәтін (қазақша)")
|
| 74 |
+
en_output = gr.Textbox(label="Ағылшынша")
|
| 75 |
+
mn_output = gr.Textbox(label="Монголша")
|
| 76 |
+
trans_btn = gr.Button("Аудару")
|
| 77 |
+
trans_btn.click(translate_text, inputs=text_input, outputs=[en_output, mn_output])
|
| 78 |
+
|
| 79 |
+
demo.launch()
|