Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,56 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
import gradio as gr
|
4 |
|
@@ -7,7 +59,7 @@ input_translators = {
|
|
7 |
"Hindi": pipeline("translation_hi_to_en", model="Helsinki-NLP/opus-mt-hi-en"),
|
8 |
"French": pipeline("translation_fr_to_en", model="Helsinki-NLP/opus-mt-fr-en"),
|
9 |
"German": pipeline("translation_de_to_en", model="Helsinki-NLP/opus-mt-de-en"),
|
10 |
-
"English": None
|
11 |
}
|
12 |
|
13 |
# Summarization model
|
@@ -15,37 +67,53 @@ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
15 |
|
16 |
# Output translators (English → target)
|
17 |
output_translators = {
|
18 |
-
"None": None,
|
19 |
"Hindi": pipeline("translation_en_to_hi", model="Helsinki-NLP/opus-mt-en-hi"),
|
20 |
"French": pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr"),
|
21 |
"German": pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de"),
|
22 |
}
|
23 |
|
24 |
-
def summarize_multilang(text, input_lang, output_lang):
|
25 |
-
# Step 1: Translate to English (if needed)
|
26 |
if input_lang != "English":
|
27 |
translator = input_translators[input_lang]
|
28 |
text = translator(text)[0]['translation_text']
|
29 |
|
30 |
-
# Step 2: Summarize
|
31 |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
32 |
|
33 |
-
# Step 3:
|
34 |
-
if output_lang
|
35 |
summary = output_translators[output_lang](summary)[0]['translation_text']
|
36 |
|
37 |
return summary
|
38 |
|
39 |
-
# Gradio
|
40 |
-
gr.
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
gr.
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
|
|
1 |
|
2 |
+
# from transformers import pipeline
|
3 |
+
# import gradio as gr
|
4 |
+
|
5 |
+
# # Input language translators (to English)
|
6 |
+
# input_translators = {
|
7 |
+
# "Hindi": pipeline("translation_hi_to_en", model="Helsinki-NLP/opus-mt-hi-en"),
|
8 |
+
# "French": pipeline("translation_fr_to_en", model="Helsinki-NLP/opus-mt-fr-en"),
|
9 |
+
# "German": pipeline("translation_de_to_en", model="Helsinki-NLP/opus-mt-de-en"),
|
10 |
+
# "English": None # No translation needed
|
11 |
+
# }
|
12 |
+
|
13 |
+
# # Summarization model
|
14 |
+
# summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
15 |
+
|
16 |
+
# # Output translators (English → target)
|
17 |
+
# output_translators = {
|
18 |
+
# "None": None,
|
19 |
+
# "Hindi": pipeline("translation_en_to_hi", model="Helsinki-NLP/opus-mt-en-hi"),
|
20 |
+
# "French": pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr"),
|
21 |
+
# "German": pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de"),
|
22 |
+
# }
|
23 |
+
|
24 |
+
# def summarize_multilang(text, input_lang, output_lang):
|
25 |
+
# # Step 1: Translate to English (if needed)
|
26 |
+
# if input_lang != "English":
|
27 |
+
# translator = input_translators[input_lang]
|
28 |
+
# text = translator(text)[0]['translation_text']
|
29 |
+
|
30 |
+
# # Step 2: Summarize
|
31 |
+
# summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
32 |
+
|
33 |
+
# # Step 3: Translate summary (if needed)
|
34 |
+
# if output_lang != "None":
|
35 |
+
# summary = output_translators[output_lang](summary)[0]['translation_text']
|
36 |
+
|
37 |
+
# return summary
|
38 |
+
|
39 |
+
# # Gradio interface
|
40 |
+
# gr.Interface(
|
41 |
+
# fn=summarize_multilang,
|
42 |
+
# inputs=[
|
43 |
+
# gr.Textbox(lines=10, label="Input Text"),
|
44 |
+
# gr.Dropdown(choices=["English", "Hindi", "French", "German"], label="Input Language"),
|
45 |
+
# gr.Dropdown(choices=["None", "Hindi", "French", "German"], label="Translate Summary To")
|
46 |
+
# ],
|
47 |
+
# outputs=gr.Textbox(label="Final Summary"),
|
48 |
+
# title="SummarAI",
|
49 |
+
# description="Supports input in Hindi, French, German, or English. Summarizes and optionally translates the summary."
|
50 |
+
# ).launch()
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
from transformers import pipeline
|
55 |
import gradio as gr
|
56 |
|
|
|
59 |
"Hindi": pipeline("translation_hi_to_en", model="Helsinki-NLP/opus-mt-hi-en"),
|
60 |
"French": pipeline("translation_fr_to_en", model="Helsinki-NLP/opus-mt-fr-en"),
|
61 |
"German": pipeline("translation_de_to_en", model="Helsinki-NLP/opus-mt-de-en"),
|
62 |
+
"English": None
|
63 |
}
|
64 |
|
65 |
# Summarization model
|
|
|
67 |
|
68 |
# Output translators (English → target)
|
69 |
output_translators = {
|
|
|
70 |
"Hindi": pipeline("translation_en_to_hi", model="Helsinki-NLP/opus-mt-en-hi"),
|
71 |
"French": pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr"),
|
72 |
"German": pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de"),
|
73 |
}
|
74 |
|
75 |
+
def summarize_multilang(text, input_lang, toggle_translation, output_lang):
|
76 |
+
# Step 1: Translate input to English (if needed)
|
77 |
if input_lang != "English":
|
78 |
translator = input_translators[input_lang]
|
79 |
text = translator(text)[0]['translation_text']
|
80 |
|
81 |
+
# Step 2: Summarize in English
|
82 |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
83 |
|
84 |
+
# Step 3: Optionally translate the summary
|
85 |
+
if toggle_translation and output_lang in output_translators:
|
86 |
summary = output_translators[output_lang](summary)[0]['translation_text']
|
87 |
|
88 |
return summary
|
89 |
|
90 |
+
# Gradio Interface with Toggle
|
91 |
+
with gr.Blocks() as demo:
|
92 |
+
gr.Markdown("# 🌍 SummarAI - Multi-Language Text Summarizer")
|
93 |
+
gr.Markdown("Summarize your content in multiple languages. Optionally translate the summary!")
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
input_text = gr.Textbox(lines=10, label="Input Text", placeholder="Paste or type your content here...")
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
input_lang = gr.Dropdown(choices=["English", "Hindi", "French", "German"], label="Input Language", value="English")
|
100 |
+
toggle_translation = gr.Checkbox(label="Translate Summary?", value=False)
|
101 |
+
output_lang = gr.Dropdown(choices=["Hindi", "French", "German"], label="Translate Summary To", visible=False)
|
102 |
+
|
103 |
+
output_summary = gr.Textbox(label="Final Summary")
|
104 |
+
|
105 |
+
# Toggle visibility logic
|
106 |
+
def update_output_lang_visibility(toggle):
|
107 |
+
return gr.update(visible=toggle)
|
108 |
+
|
109 |
+
toggle_translation.change(update_output_lang_visibility, inputs=toggle_translation, outputs=output_lang)
|
110 |
+
|
111 |
+
submit_btn = gr.Button("Summarize")
|
112 |
+
submit_btn.click(
|
113 |
+
fn=summarize_multilang,
|
114 |
+
inputs=[input_text, input_lang, toggle_translation, output_lang],
|
115 |
+
outputs=output_summary
|
116 |
+
)
|
117 |
+
|
118 |
+
demo.launch()
|
119 |
|