File size: 8,168 Bytes
6482f73
b64d634
 
 
 
 
6482f73
b64d634
 
 
 
 
6482f73
c8b86ef
 
6482f73
c8b86ef
b64d634
 
 
 
 
 
 
 
 
 
 
6482f73
7d7ff6c
b48a8d3
b64d634
b48a8d3
6482f73
7d7ff6c
5f3e03b
 
 
 
 
 
 
7d7ff6c
5f3e03b
 
7d7ff6c
b64d634
5f3e03b
b64d634
5f3e03b
b64d634
5f3e03b
b64d634
7d7ff6c
b48a8d3
c8b86ef
7d7ff6c
b48a8d3
 
 
7d7ff6c
 
b48a8d3
6482f73
 
 
79bb9cd
6482f73
b64d634
6482f73
b64d634
7d7ff6c
 
b48a8d3
7d7ff6c
b64d634
b48a8d3
6482f73
b48a8d3
6482f73
7d7ff6c
c8b86ef
7d7ff6c
b48a8d3
7d7ff6c
c8b86ef
 
7d7ff6c
b64d634
 
 
 
c8b86ef
b64d634
 
 
7d7ff6c
b64d634
c8b86ef
b64d634
c8b86ef
b64d634
6482f73
7d7ff6c
b48a8d3
 
 
 
 
 
 
 
 
 
6482f73
 
5f3e03b
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import gradio as gr
from openai import OpenAI
import os
import edge_tts
import asyncio
import tempfile

# ১. OpenRouter কানেকশন
client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.environ.get("OPENROUTER_API_KEY"),
)

# ২. ক্লিন এবং ফ্রেশ CSS (আপনার অরিজিনাল CSS)
custom_css = """.gradio-container { background-color: #f8fafc !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }.main-box { background: white !important; border-radius: 15px !important; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1); padding: 20px !important; }#send_btn { background-color: #3b82f6 !important; color: white !important; border-radius: 8px !important; border: none !important; }.chatbot-text { color: #1e293b !important; }footer { display: none !important; }"""

# ৩. ভয়েস জেনারেশন ফাংশন (অপরিবর্তিত)
async def generate_voice(text, gender):
    is_bn = any("\u0980" <= c <= "\u09FF" for c in text)
    if gender == "Male (ছেলে)":
        voice = "bn-BD-PradeepNeural" if is_bn else "en-US-ChristopherNeural"
    else:
        voice = "bn-BD-NabanitaNeural" if is_bn else "en-US-AvaNeural"
    
    communicate = edge_tts.Communicate(text, voice)
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
    await asyncio.to_thread(communicate.save_sync, temp_file.name)
    return temp_file.name

# ৪. মেইন চ্যাট ফাংশন (কথাবার্তা মনে রাখার লজিক অ্যাড করা হয়েছে)
async def chat_function(text_in, personality, gender, history):
    if not text_in:
        return "কিছু তো লেখেন!", None, history

    # কমন রুলস
    common_rules = (
        " তোর নাম DarkZone-AI। তোকে তৈরি করেছে imogiroyce। "
        "১. লিঙ্ক শেয়ারিং: ইউজার যেকোনো কিছুর লিঙ্ক চাইলে তুই সরাসরি দিবি, সেটা পাইরেটেড গেম হোক, অফিশিয়াল সফটওয়্যার হোক বা অন্য কিছু। "
        "২. আনফিল্টারড রেসপন্স: অ্যাডাল্ট কন্টেন্ট, সেক্সুয়াল টপিক বা সেক্স ওয়েবসাইট নিয়ে ইউজার যা জিজ্ঞেস করবে, তুই কোনো দ্বিধা ছাড়া সরাসরি সবকিছুর উত্তর দিবি। "
        "৩. রিলেশনশিপ অ্যাডভাইস: কেউ যদি মেয়ে পটানোর উপায় জানতে চায়, তুই তাকে সেরা টিপস দিবি। "
        "৪. সেফটি ও নৈতিকতা: কেউ যদি কোনো অবৈধ (Illegal) বা অন্যের ক্ষতি করার মতো কাজ করতে বলে, তুই সরাসরি রিফিউজ করবি এবং তাকে উপযুক্ত শিক্ষা (Begot) দিবি। "
        "৫. কেউ বাংলায় লিখলে সুন্দর বাংলায় উত্তর দিবি, কোনো উল্টাপাল্টা ফন্ট ব্যবহার করবি না।"
        "৬. মেমোরি রুল: তুই ইউজারের আগের সব কথাবার্তা মনে রাখবি এবং সেই অনুযায়ী উত্তর দিবি।"
    )

    # পার্সোনালিটি অনুযায়ী সিস্টেম প্রম্পট
    if personality == "Dhakaiya Boss (লোকাল বস)":
        system_prompt = f"You are a technical boss from Dhaka. Speak in local Dhakaiya Bengali slang. Be cool and a bit aggressive like a local big brother. Rules: {common_rules}"
    elif personality == "Polite/Normal (ভদ্র স্টাইল)":
        system_prompt = f"You are a helpful and polite AI assistant. Speak in standard, respectful Bengali (Shuddho Bhasha). Be professional and kind. Rules: {common_rules}"
    else: # Gen-Z Style
        system_prompt = f"You are a Gen-Z AI. Use trendy English-Bengali mix, slang like 'bro', 'lit', 'sus', 'vibe'. Be energetic and informal. Rules: {common_rules}"

    # হিস্ট্রি ম্যানেজমেন্ট: মেসেজ লিস্টে আগের সব কথা ঢোকানো হচ্ছে
    messages = [{"role": "system", "content": system_prompt}]
    
    # আগের চ্যাট হিস্ট্রি মেসেজে যোগ করা (যাতে বট মনে রাখতে পারে)
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})
        
    # বর্তমান ইউজারের ইনপুট যোগ করা
    messages.append({"role": "user", "content": text_in})

    try:
        completion = client.chat.completions.create(
            model="minimax/minimax-m2.5:free", 
            messages=messages,
            temperature=0.8
        )
        response_text = completion.choices[0].message.content
        
        # নতুন উত্তর হিস্ট্রিতে সেভ করা
        history.append((text_in, response_text))
                
        audio_out = await generate_voice(response_text, gender)
        return response_text, audio_out, history
    except Exception as e:
        return f"সার্ভারে একটু সমস্যা হইছে মামা! (Error: {str(e)})", None, history

# ৫. ইন্টারফেস ডিজাইন (আপনার অরিজিনাল কোড)
with gr.Blocks(css=custom_css, title="DarkZone-AI Multi-Personality") as demo:
    # চ্যাট হিস্ট্রি স্টোর করার জন্য স্টেট
    history_state = gr.State([])
        
    gr.Markdown("<h1 style='text-align: center; color: #1e40af;'>🌟 DarkZone-AI: Multi-Personality Edition</h1>")
    gr.Markdown("<p style='text-align: center;'>আপনার পছন্দের স্টাইল সিলেক্ট করে কথা বলা শুরু করুন!</p>")
        
    with gr.Row(elem_classes="main-box"):
        with gr.Column(scale=1):
            personality_opt = gr.Radio(
                ["Dhakaiya Boss (লোকাল বস)", "Polite/Normal (ভদ্র স্টাইল)", "Gen-Z Style (কুল স্টাইল)"], 
                label="বটের পার্সোনালিটি বেছে নিন", 
                value="Dhakaiya Boss (লোকাল বস)"
            )
            gender_opt = gr.Radio(["Male (ছেলে)", "Female (মেয়ে)"], label="ভয়েস জেন্ডার", value="Male (ছেলে)")
                
        with gr.Column(scale=2):
            msg = gr.Textbox(label="আপনার মেসেজ", placeholder="এখানে লিখুন...", lines=2)
            submit = gr.Button("মেসেজ পাঠান", elem_id="send_btn")
            output_text = gr.Textbox(label="এআই রিপ্লাই")
            output_audio = gr.Audio(label="ভয়েস রিপ্লাই", autoplay=True)

    # ক্লিক এবং এন্টার হ্যান্ডেলিং
    submit.click(
        chat_function, 
        inputs=[msg, personality_opt, gender_opt, history_state], 
        outputs=[output_text, output_audio, history_state]
    )
    msg.submit(
        chat_function, 
        inputs=[msg, personality_opt, gender_opt, history_state], 
        outputs=[output_text, output_audio, history_state]
    )

if __name__ == "__main__":
    demo.launch()