sproducts commited on
Commit
864560a
·
verified ·
1 Parent(s): d0069ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -58
app.py CHANGED
@@ -1,73 +1,93 @@
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
- # Function to perform machine translation
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
- translated_text = translation_tokenizer_en_bn.decode(outputs[0], skip_special_tokens=True)
22
- return translated_text
23
-
24
- def translate_text_bn_en(input_text):
25
- inputs = translation_tokenizer_bn_en(input_text, return_tensors="pt")
26
- outputs = translation_model_bn_en.generate(**inputs)
27
- translated_text = translation_tokenizer_bn_en.decode(outputs[0], skip_special_tokens=True)
28
- return translated_text
29
-
30
- # Function to perform summarization
31
- def summarize_text(input_text):
32
- inputs = summarization_tokenizer(input_text, return_tensors="pt")
33
- outputs = summarization_model.generate(**inputs)
34
- summarized_text = summarization_tokenizer.decode(outputs[0], skip_special_tokens=True)
35
- return summarized_text
36
-
37
- # Function to perform paraphrasing
38
- def paraphrase_text(input_text):
39
- inputs = paraphrase_tokenizer(input_text, return_tensors="pt")
40
- outputs = paraphrase_model.generate(**inputs)
41
- paraphrased_text = paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
42
- return paraphrased_text
43
 
44
- def process_text(text, task):
45
- if task == "Translate_English_to_Bengali":
46
- return translate_text_en_bn(text)
47
- elif task == "Translate_Bengali_to_English":
48
- return translate_text_bn_en(text)
49
- elif task == "Summarize":
50
- return summarize_text(text)
51
- elif task == "Paraphrase":
52
- return paraphrase_text(text)
 
 
 
53
 
54
  # Define the Gradio interface
55
  iface = gr.Interface(
56
- fn=process_text,
57
- inputs=["text", gr.Dropdown(["Translate_English_to_Bengali", "Translate_Bengali_to_English", "Summarize", "Paraphrase"])],
58
- outputs="text",
 
 
 
 
 
 
59
  live=False,
60
- title="Usage of BanglaT5 Model",
 
61
  theme='Ajaxon6255/Emerald_Isle',
62
  examples=[
63
- ["বাংলাদেশ দক্ষিণ এশিয়ার একটি সার্বভৌম রাষ্ট্র। বাংলাদেশের সাংবিধানিক নাম গণপ্রজাতন্ত্রী বাংলাদেশ। ভৌগোলিকভাবে বাংলাদেশের পশ্চিমে ভারতের পশ্চিমবঙ্গ, উত্তরে পশ্চিমবঙ্গ, আসাম ও মেঘালয়, পূর্ব সীমান্তে আসাম, ত্রিপুরা ও মিজোরাম, দক্ষিণ-পূর্ব সীমান্তে মিয়ানমারের চিন ও রাখাইন রাজ্য এবং দক্ষিণ উপকূলের দিকে বঙ্গোপসাগর অবস্থিত। ভৌগোলিকভাবে পৃথিবীর বৃহত্তম ব-দ্বীপের সিংহভাগ অঞ্চল জুড়ে বাংলাদেশ ভূখণ্ড অবস্থিত। জনসংখ্যার বিচারে প্রায় ১৭ কোটিরও অধিক জনসংখ্যা নিয়ে বাংলাদেশ বিশ্বের ৮ম বৃহত্তম দেশ। নদীমাতৃক বাংলাদেশ ভূখণ্ডের উপর দিয়ে বয়ে গেছে ৫৭টি আন্তর্জাতিক নদী। বাংলাদেশের উত্তর-পূর্বে ও দক্ষিণ-পূর্বে টারশিয়ারি যুগের পাহাড় ছেয়ে আছে। বিশ্বের বৃহত্তম ম্যানগ্রোভ অরণ্য সুন্দরবন ও দীর্ঘতম প্রাকৃতিক সৈকত কক্সবাজার সমুদ্র সৈকত বাংলাদেশে অবস্থিত।"],
64
- ["বাংলাদেশের সংবিধানিক নাম কি?"],
65
- ["The Department of Computer Science and Engineering, the first department of its kind, was established in 1982 under the faculty of Electrical and Electronics Engineering. From the very initial days of its establishment, it has been able to attract the very best students of the country. Students securing topmost merit positions in the legendary admission test of BUET opt for studies in this department. At the very beginning, the department offered only M. Sc. Engg. and M Engg. degrees in Computer Science and Engineering. The Undergraduate program started in 1986. At first, 30 students were admitted each year for pursuing the B.Sc. Engineering degree."],
66
- ["তিনজনের কেউই বাবার পথ ধরে প্রযুক্তি দুনিয়ায় হাঁটেননি।"],
67
- ["বিশ্বের আরও একটি সেরা ক্লাব। স্প্যানিশ ফুটবলে বার্সেলোনারই প্রবল প্রতিদ্বন্দ্বী রিয়াল মাদ্রিদ। লিওনেল মেসির ক্লাব ইতিহাসে উয়েফা চ্যাম্পিয়নস লিগ জিতেছে মোট পাঁচবার। ইউরোপীয় কাপ উইনার্স কাপে তাদের সাফল্য আছে চারবার। পাশাপাশি তিনবার ক্লাব বিশ্বকাপ জেতা বার্সা লা লিগায় সাফল্য পেয়েছেন মোট ২৪ বার। ডিয়েগো ম্যারাডোনা, ইয়োহান ক্রুইফ, রিভালদোদের মতো খেলোয়াড়েরা এই ক্লাবের হয়ে অংশ হয়ে আছেন ইতিহাসের। "]
68
-
69
  ]
70
  )
71
 
72
  # Launch the Gradio app
73
- iface.launch(inline=False)
 
1
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
2
  import gradio as gr
3
 
4
+ # Load a conversational model (replace with your Bangla LLM)
5
+ chatbot_model_name = "csebuetnlp/banglat5_base" # Replace with your fine-tuned model
6
+ chatbot_tokenizer = AutoTokenizer.from_pretrained(chatbot_model_name)
7
+ chatbot_model = AutoModelForSeq2SeqLM.from_pretrained(chatbot_model_name)
8
 
9
+ # Function to generate empathetic responses
10
+ def generate_empathetic_response(user_input):
11
+ try:
12
+ # Prepare the input for the chatbot
13
+ input_text = f"User: {user_input}\nChatbot:"
14
+ inputs = chatbot_tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
15
+
16
+ # Generate response
17
+ outputs = chatbot_model.generate(
18
+ **inputs,
19
+ max_length=512,
20
+ num_beams=5,
21
+ early_stopping=True,
22
+ no_repeat_ngram_size=2
23
+ )
24
+
25
+ # Decode and clean the response
26
+ response = chatbot_tokenizer.decode(outputs[0], skip_special_tokens=True)
27
+
28
+ # Ensure the response is culturally appropriate and empathetic
29
+ response = add_empathy_and_cultural_tone(response)
30
+ return response
31
+ except Exception as e:
32
+ return f"Error generating response: {str(e)}"
33
 
34
+ # Function to add empathy and cultural tone to the response
35
+ def add_empathy_and_cultural_tone(response):
36
+ # Add empathetic phrases
37
+ empathetic_phrases = [
38
+ "বুঝতে পারছি", "কষ্ট পেয়েছো নিশ্চয়", "আমি তোমার পাশে আছি",
39
+ "এটা খুব কঠিন সময় তোমার জন্য", "তোমার অনুভূতি আমি বুঝতে পারছি"
40
+ ]
41
+
42
+ # Add regional dialects and informal pauses
43
+ regional_phrases = [
44
+ "ভাইয়া", "হুজুর", "থাইকা", "পাল্লায় পড়া", "টাকা ফুরাইয়া গেছে",
45
+ "পকেট শূন্য", "মনটা ভাঙা কাঁচের মতো"
46
+ ]
47
+
48
+ # Randomly add empathetic and regional phrases to the response
49
+ import random
50
+ if random.random() > 0.5:
51
+ response = random.choice(empathetic_phrases) + "। " + response
52
+ if random.random() > 0.5:
53
+ response = response + " " + random.choice(regional_phrases)
54
+
55
+ return response
 
 
 
 
 
 
 
 
 
 
56
 
57
+ # Function to handle the chatbot conversation
58
+ def chat_with_bot(user_input, history=[]):
59
+ # Generate a response
60
+ bot_response = generate_empathetic_response(user_input)
61
+
62
+ # Update conversation history
63
+ history.append((user_input, bot_response))
64
+
65
+ # Format the conversation history for display
66
+ formatted_history = "\n".join([f"User: {user}\nChatbot: {bot}" for user, bot in history])
67
+
68
+ return formatted_history, history
69
 
70
  # Define the Gradio interface
71
  iface = gr.Interface(
72
+ fn=chat_with_bot,
73
+ inputs=[
74
+ gr.Textbox(label="Your Message", placeholder="আপনার সমস্যা বা অনুভূতি শেয়ার করুন..."),
75
+ gr.State() # To store conversation history
76
+ ],
77
+ outputs=[
78
+ gr.Textbox(label="Conversation History", lines=10),
79
+ gr.State() # To pass updated history back
80
+ ],
81
  live=False,
82
+ title="Bangla Emotional Support Chatbot",
83
+ description="আপনার সমস্যা বা অনুভূতি শেয়ার করুন। আমি আপনার পাশে আছি।",
84
  theme='Ajaxon6255/Emerald_Isle',
85
  examples=[
86
+ ["আমার টাকা ফুরিয়ে গেছে এবং আমি ঋণে ডুবে আছি।"],
87
+ ["আমার ভাইয়ের সাথে আমার সম্পর্ক খারাপ হয়ে গেছে।"],
88
+ ["আমি খুব একা বোধ করছি এবং কাউকে পাশে পাচ্ছি না।"]
 
 
 
89
  ]
90
  )
91
 
92
  # Launch the Gradio app
93
+ iface.launch(inline=False)