Spaces:
Sleeping
Sleeping
sanzanalora
commited on
Commit
•
7f24255
1
Parent(s):
b357021
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
# Load fine-tuned BanglaT5 models for different tasks
|
5 |
+
translation_model_en_bn = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/banglat5_nmt_en_bn")
|
6 |
+
translation_tokenizer_en_bn = AutoTokenizer.from_pretrained("csebuetnlp/banglat5_nmt_en_bn")
|
7 |
+
|
8 |
+
translation_model_bn_en = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/banglat5_nmt_bn_en")
|
9 |
+
translation_tokenizer_bn_en = AutoTokenizer.from_pretrained("csebuetnlp/banglat5_nmt_bn_en")
|
10 |
+
|
11 |
+
summarization_model = AutoModelForSeq2SeqLM.from_pretrained("skl25/banglat5_xlsum_fine-tuned")
|
12 |
+
summarization_tokenizer = AutoTokenizer.from_pretrained("skl25/banglat5_xlsum_fine-tuned")
|
13 |
+
|
14 |
+
paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/banglat5_banglaparaphrase")
|
15 |
+
paraphrase_tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/banglat5_banglaparaphrase")
|
16 |
+
|
17 |
+
# Define task functions
|
18 |
+
def translate_text_en_bn(input_text):
|
19 |
+
inputs = translation_tokenizer_en_bn(input_text, return_tensors="pt")
|
20 |
+
outputs = translation_model_en_bn.generate(**inputs)
|
21 |
+
return translation_tokenizer_en_bn.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
|
23 |
+
def translate_text_bn_en(input_text):
|
24 |
+
inputs = translation_tokenizer_bn_en(input_text, return_tensors="pt")
|
25 |
+
outputs = translation_model_bn_en.generate(**inputs)
|
26 |
+
return translation_tokenizer_bn_en.decode(outputs[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
def summarize_text(input_text):
|
29 |
+
inputs = summarization_tokenizer(input_text, return_tensors="pt")
|
30 |
+
outputs = summarization_model.generate(**inputs)
|
31 |
+
return summarization_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
def paraphrase_text(input_text):
|
34 |
+
inputs = paraphrase_tokenizer(input_text, return_tensors="pt")
|
35 |
+
outputs = paraphrase_model.generate(**inputs)
|
36 |
+
return paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
+
|
38 |
+
# Process input based on task
|
39 |
+
def process_text(text, task):
|
40 |
+
task_funcs = {
|
41 |
+
"Translate English to Bengali": translate_text_en_bn,
|
42 |
+
"Translate Bengali to English": translate_text_bn_en,
|
43 |
+
"Summarize": summarize_text,
|
44 |
+
"Paraphrase": paraphrase_text
|
45 |
+
}
|
46 |
+
return task_funcs.get(task, lambda x: "Invalid Task")(text)
|
47 |
+
|
48 |
+
# Task-specific examples
|
49 |
+
examples_en_bn_translation = [
|
50 |
+
["The sky is blue, and the weather is nice."],
|
51 |
+
["Artificial intelligence is shaping the future."],
|
52 |
+
["Bangladesh is known for its rich culture and heritage."]
|
53 |
+
]
|
54 |
+
|
55 |
+
examples_bn_en_translation = [
|
56 |
+
["বাংলাদেশ দক্ষিণ এশিয়ার একটি সার্বভৌম রাষ্ট্র।"],
|
57 |
+
["ঢাকা বাংলাদেশের রাজধানী।"],
|
58 |
+
["রবীন্দ্রনাথ ঠাকুরের গান বাংলা সংস্কৃতির একটি অবিচ্ছেদ্য অংশ।"]
|
59 |
+
]
|
60 |
+
|
61 |
+
examples_summarization = [
|
62 |
+
["The Department of Computer Science and Engineering, established in 1982, was the first of its kind in Bangladesh. "
|
63 |
+
"Attracting top students from all over the country, it offers both undergraduate and postgraduate degrees."],
|
64 |
+
["Climate change is one of the biggest challenges we face today. With rising temperatures and unpredictable weather, "
|
65 |
+
"the world needs to come together to find sustainable solutions."],
|
66 |
+
["Technology has advanced rapidly over the past decade, with innovations in fields like AI, robotics, and quantum computing."]
|
67 |
+
]
|
68 |
+
|
69 |
+
examples_paraphrasing = [
|
70 |
+
["The cat is sitting on the mat."],
|
71 |
+
["He was very happy to receive the award."],
|
72 |
+
["The weather today is sunny and warm."]
|
73 |
+
]
|
74 |
+
|
75 |
+
# Define the Gradio interface with enhanced visuals
|
76 |
+
iface = gr.Interface(
|
77 |
+
fn=process_text,
|
78 |
+
inputs=[
|
79 |
+
"text",
|
80 |
+
gr.Dropdown(
|
81 |
+
["Translate English to Bengali", "Translate Bengali to English", "Summarize", "Paraphrase"],
|
82 |
+
label="Choose Task",
|
83 |
+
elem_id="dropdown-task"
|
84 |
+
)
|
85 |
+
],
|
86 |
+
outputs="text",
|
87 |
+
title="BanglaT5 Model Hub",
|
88 |
+
description="A multi-functional tool for translation, summarization, and paraphrasing using BanglaT5 models.",
|
89 |
+
theme="finlaydog/seafoam",
|
90 |
+
examples=[
|
91 |
+
*examples_en_bn_translation, # Adding 3 examples for English to Bengali translation
|
92 |
+
*examples_bn_en_translation, # Adding 3 examples for Bengali to English translation
|
93 |
+
*examples_summarization, # Adding 3 examples for Summarization
|
94 |
+
*examples_paraphrasing # Adding 3 examples for Paraphrasing
|
95 |
+
],
|
96 |
+
allow_flagging="auto",
|
97 |
+
)
|
98 |
+
|
99 |
+
# Launch the Gradio app
|
100 |
+
iface.launch(inline=False)
|