Romanchik333 commited on
Commit
12288ea
·
1 Parent(s): 1282945
Files changed (1) hide show
  1. app.py +44 -12
app.py CHANGED
@@ -1,25 +1,44 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- #sentencepiece no installed??
5
-
6
- translator = pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru")
7
  image_classifier = pipeline("image-classification", model="microsoft/resnet-50")
 
 
 
8
 
9
  def translate_to_russian(text):
10
  try:
11
- translation = translator(text)
12
  return translation[0]['translation_text']
13
  except Exception as e:
14
- print(f"Ошибка перевода: {e}")
 
 
 
 
 
 
 
 
15
  return text
16
 
17
  def classify_image(image):
18
  results = image_classifier(image)
19
  output = {}
20
  for result in results:
21
- output[translate_to_russian(result['label'])] = result['score']
22
  return output
 
 
 
 
 
 
 
 
23
 
24
  with gr.Blocks() as demo:
25
  gr.Markdown("#Домашнее задание для курса ML2")
@@ -29,12 +48,25 @@ with gr.Blocks() as demo:
29
  label_output = gr.Label(label="Результаты классификации")
30
  btn_classify = gr.Button("Классифицировать")
31
  btn_classify.click(fn=classify_image, inputs=img_input, outputs=label_output)
32
- with gr.Tab("Перевод текста en-ru"):
 
 
 
 
 
 
 
 
 
 
 
33
  with gr.Row():
34
- text_input = gr.Textbox(label="EN text", placeholder="Напишите сюда английский текст")
35
- text_output = gr.Textbox(label="RU text", placeholder="Переведённый текст")
36
- btn_translate = gr.Button("Перевести")
37
- btn_translate.click(fn=translate_to_russian, inputs=text_input, outputs=text_output)
38
-
 
 
39
  if __name__ == "__main__":
40
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from transformers import BlipProcessor, BlipForConditionalGeneration
4
 
5
+ translator_ru = pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru")
6
+ translator_en = pipeline("translation_ru_to_en", model="Helsinki-NLP/opus-mt-ru-en")
 
7
  image_classifier = pipeline("image-classification", model="microsoft/resnet-50")
8
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
9
+ captioner = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
10
+
11
 
12
  def translate_to_russian(text):
13
  try:
14
+ translation = translator_ru(text)
15
  return translation[0]['translation_text']
16
  except Exception as e:
17
+ print(f"Ошибка перевода на ru: {e}")
18
+ return text
19
+
20
+ def translate_to_english(text):
21
+ try:
22
+ translation = translator_en(text)
23
+ return translation[0]['translation_text']
24
+ except Exception as e:
25
+ print(f"Ошибка перевода на en: {e}")
26
  return text
27
 
28
  def classify_image(image):
29
  results = image_classifier(image)
30
  output = {}
31
  for result in results:
32
+ output[translate_to_russian((result['label'].split(','))[0])] = result['score']
33
  return output
34
+
35
+ def capt_image(image, text):
36
+ if len(text) != 0:
37
+ inputs = processor(image, translate_to_english(text), return_tensors="pt")
38
+ else:
39
+ inputs = processor(image, return_tensors="pt")
40
+ out = captioner.generate(**inputs)
41
+ return translate_to_russian(processor.decode(out[0], skip_special_tokens=True))
42
 
43
  with gr.Blocks() as demo:
44
  gr.Markdown("#Домашнее задание для курса ML2")
 
48
  label_output = gr.Label(label="Результаты классификации")
49
  btn_classify = gr.Button("Классифицировать")
50
  btn_classify.click(fn=classify_image, inputs=img_input, outputs=label_output)
51
+ with gr.Tab("Перевод текста"):
52
+ with gr.Row("en-ru"):
53
+ text_input_1 = gr.Textbox(label="EN text", placeholder="Напишите сюда английский текст")
54
+ text_output_1 = gr.Textbox(label="RU text", placeholder="Переведённый текст")
55
+ btn_translate_en_ru = gr.Button("Перевести")
56
+ btn_translate_en_ru.click(fn=translate_to_russian, inputs=text_input_1, outputs=text_output_1)
57
+ with gr.Row("ru-en"):
58
+ text_input_2 = gr.Textbox(label="RU text", placeholder="Напишите сюда русский текст")
59
+ text_output_2 = gr.Textbox(label="EN text", placeholder="Переведённый текст")
60
+ btn_translate_ru_en = gr.Button("Перевести")
61
+ btn_translate_ru_en.click(fn=translate_to_english, inputs=text_input_2, outputs=text_output_2)
62
+ with gr.Tab("Написание заголовков"):
63
  with gr.Row():
64
+ with gr.Column():
65
+ img_capt = gr.Image(type="pil", label="Загрузите изображение")
66
+ pre_capt = gr.Textbox(label="Начало заголовка")
67
+ with gr.Column():
68
+ caption = gr.Label(label="Заголовок для картинки")
69
+ btn_captioning = gr.Button("Написать заголовок")
70
+ btn_captioning.click(fn=capt_image, inputs=[img_capt, pre_capt], outputs=caption)
71
  if __name__ == "__main__":
72
  demo.launch()