File size: 4,078 Bytes
864560a
c2aec30
 
864560a
 
 
 
c2aec30
864560a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2aec30
864560a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2aec30
864560a
 
 
 
 
 
 
 
 
 
 
 
c2aec30
 
 
864560a
 
 
 
 
 
 
 
 
c2aec30
864560a
 
c2aec30
 
864560a
 
 
c2aec30
18dca7c
ef1547c
c2aec30
864560a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
import gradio as gr

# Load a conversational model (replace with your Bangla LLM)
chatbot_model_name = "csebuetnlp/banglat5_base"  # Replace with your fine-tuned model
chatbot_tokenizer = AutoTokenizer.from_pretrained(chatbot_model_name)
chatbot_model = AutoModelForSeq2SeqLM.from_pretrained(chatbot_model_name)

# Function to generate empathetic responses
def generate_empathetic_response(user_input):
    try:
        # Prepare the input for the chatbot
        input_text = f"User: {user_input}\nChatbot:"
        inputs = chatbot_tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
        
        # Generate response
        outputs = chatbot_model.generate(
            **inputs,
            max_length=512,
            num_beams=5,
            early_stopping=True,
            no_repeat_ngram_size=2
        )
        
        # Decode and clean the response
        response = chatbot_tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Ensure the response is culturally appropriate and empathetic
        response = add_empathy_and_cultural_tone(response)
        return response
    except Exception as e:
        return f"Error generating response: {str(e)}"

# Function to add empathy and cultural tone to the response
def add_empathy_and_cultural_tone(response):
    # Add empathetic phrases
    empathetic_phrases = [
        "বুঝতে পারছি", "কষ্ট পেয়েছো নিশ্চয়", "আমি তোমার পাশে আছি", 
        "এটা খুব কঠিন সময় তোমার জন্য", "তোমার অনুভূতি আমি বুঝতে পারছি"
    ]
    
    # Add regional dialects and informal pauses
    regional_phrases = [
        "ভাইয়া", "হুজুর", "থাইকা", "পাল্লায় পড়া", "টাকা ফুরাইয়া গেছে", 
        "পকেট শূন্য", "মনটা ভাঙা কাঁচের মতো"
    ]
    
    # Randomly add empathetic and regional phrases to the response
    import random
    if random.random() > 0.5:
        response = random.choice(empathetic_phrases) + "। " + response
    if random.random() > 0.5:
        response = response + " " + random.choice(regional_phrases)
    
    return response

# Function to handle the chatbot conversation
def chat_with_bot(user_input, history=[]):
    # Generate a response
    bot_response = generate_empathetic_response(user_input)
    
    # Update conversation history
    history.append((user_input, bot_response))
    
    # Format the conversation history for display
    formatted_history = "\n".join([f"User: {user}\nChatbot: {bot}" for user, bot in history])
    
    return formatted_history, history

# Define the Gradio interface
iface = gr.Interface(
    fn=chat_with_bot,
    inputs=[
        gr.Textbox(label="Your Message", placeholder="আপনার সমস্যা বা অনুভূতি শেয়ার করুন..."),
        gr.State()  # To store conversation history
    ],
    outputs=[
        gr.Textbox(label="Conversation History", lines=10),
        gr.State()  # To pass updated history back
    ],
    live=False,
    title="Bangla Emotional Support Chatbot",
    description="আপনার সমস্যা বা অনুভূতি শেয়ার করুন। আমি আপনার পাশে আছি।",
    theme='Ajaxon6255/Emerald_Isle',
    examples=[
        ["আমার টাকা ফুরিয়ে গেছে এবং আমি ঋণে ডুবে আছি।"],
        ["আমার ভাইয়ের সাথে আমার সম্পর্ক খারাপ হয়ে গেছে।"],
        ["আমি খুব একা বোধ করছি এবং কাউকে পাশে পাচ্ছি না।"]
    ]
)

# Launch the Gradio app
iface.launch(inline=False)