Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -3,13 +3,31 @@ from transformers import pipeline
3
 
4
  get_completion = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def summarize(input):
7
  output = get_completion(input)
8
- return output[0]['summary_text']
 
 
9
 
10
  demo = gr.Interface(fn=summarize,
11
  inputs=[gr.Textbox(label="Text to summarize", lines=6)],
12
- outputs=[gr.Textbox(label="Result", lines=3)],
13
  title="Text summarization with distilbart-cnn",
14
  description="Summarize any text using the `sshleifer/distilbart-cnn-12-6` model under the hood!",
15
  examples=[
 
3
 
4
  get_completion = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
5
 
6
+ def translate(input_text, source, target):
7
+ # source_readable = source
8
+ # if source == "Auto Detect" or source.startswith("Detected"):
9
+ # source, _ = auto_detect_language_code(input_text)
10
+ # if source in source_lang_dict.keys():
11
+ # source = source_lang_dict[source]
12
+ # target_lang_dict, _ = get_target_languages(source)
13
+ try:
14
+ # target = target_lang_dict[target]
15
+ model = f"Helsinki-NLP/opus-mt-{source}-{target}"
16
+ pipe = pipeline("translation", model=model)
17
+ translation = pipe(input_text)
18
+ return translation[0]['translation_text'], ""
19
+ except KeyError:
20
+ return "", f"Error: Translation direction {source_readable} to {target} is not supported by Helsinki Translation Models"
21
+
22
  def summarize(input):
23
  output = get_completion(input)
24
+ summary_origin = output[0]['summary_text']
25
+ summary_translated = translate(summary_origin,'en','zh')
26
+ return summary_origin, summary_translated
27
 
28
  demo = gr.Interface(fn=summarize,
29
  inputs=[gr.Textbox(label="Text to summarize", lines=6)],
30
+ outputs=[gr.Textbox(label="Result", lines=3),gr.Textbox(label="Translate Result", lines=3)],
31
  title="Text summarization with distilbart-cnn",
32
  description="Summarize any text using the `sshleifer/distilbart-cnn-12-6` model under the hood!",
33
  examples=[