Romanchik333 commited on
Commit
5e6b438
·
1 Parent(s): 366c065
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ translator = pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru")
5
+ image_classifier = pipeline("image-classification", model="microsoft/resnet-50")
6
+
7
+ # 3. Функция для перевода текста
8
+ def translate_to_russian(text):
9
+ try:
10
+ translation = translator(text)
11
+ return translation[0]['translation_text']
12
+ except Exception as e:
13
+ print(f"Ошибка перевода: {e}")
14
+ return text
15
+
16
+ def classify_image(image):
17
+ results = image_classifier(image)
18
+ output = {}
19
+ for result in results:
20
+ output[translate_to_russian(result['label'])] = result['score']
21
+ return output
22
+
23
+ with gr.Blocks() as demo:
24
+ gr.Markdown("Домашнее задание для курса ML2")
25
+ with gr.Tab("Классификация изображений"):
26
+ with gr.Row():
27
+ img_input = gr.Image(type="pil", label="Загрузите изображение")
28
+ label_output = gr.Label(label="Результаты классификации")
29
+ btn_classify = gr.Button("Классифицировать")
30
+ btn_classify.click(fn=classify_image, inputs=img_input, outputs=label_output)
31
+ with gr.Tab("Перевод текста en-ru"):
32
+ with gr.Row():
33
+ text_input = gr.Textbox(label="EN text", placeholder="Напишите сюда английский текст")
34
+ text_output = gr.Textbox(label="RU text", placeholder="Переведённый текст")
35
+ btn_translate = gr.Button("Перевести")
36
+ btn_translate.click(fn=translate_to_russian, inputs=text_input, outputs=text_output)
37
+
38
+ if __name__ == "__main__":
39
+ demo.launch()