Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ client = OpenAI(
|
|
| 11 |
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# ২. ক্লিন এবং ফ্রেশ CSS (
|
| 15 |
custom_css = """
|
| 16 |
.gradio-container { background-color: #f8fafc !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
|
| 17 |
.main-box { background: white !important; border-radius: 15px !important; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1); padding: 20px !important; }
|
|
@@ -20,7 +20,7 @@ custom_css = """
|
|
| 20 |
footer { display: none !important; }
|
| 21 |
"""
|
| 22 |
|
| 23 |
-
# ৩. ভয়েস জেনারেশন ফাংশন
|
| 24 |
async def generate_voice(text, gender):
|
| 25 |
is_bn = any("\u0980" <= c <= "\u09FF" for c in text)
|
| 26 |
if gender == "Male (ছেলে)":
|
|
@@ -33,12 +33,12 @@ async def generate_voice(text, gender):
|
|
| 33 |
await asyncio.to_thread(communicate.save_sync, temp_file.name)
|
| 34 |
return temp_file.name
|
| 35 |
|
| 36 |
-
# ৪. মেইন চ্যাট ফাংশন
|
| 37 |
-
async def chat_function(text_in, personality, gender):
|
| 38 |
if not text_in:
|
| 39 |
-
return "কিছু তো লেখেন!", None
|
| 40 |
|
| 41 |
-
#
|
| 42 |
if personality == "Dhakaiya Boss (লোকাল বস)":
|
| 43 |
system_prompt = "You are DarkZone-AI, a technical boss from Dhaka. Speak in local Dhakaiya Bengali slang. Be cool and a bit aggressive like a local big brother."
|
| 44 |
elif personality == "Polite/Normal (ভদ্র স্টাইল)":
|
|
@@ -46,10 +46,16 @@ async def chat_function(text_in, personality, gender):
|
|
| 46 |
else: # Gen-Z Style
|
| 47 |
system_prompt = "You are a Gen-Z AI. Use trendy English-Bengali mix, slang like 'bro', 'lit', 'sus', 'vibe'. Be very energetic and informal."
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
completion = client.chat.completions.create(
|
|
@@ -58,13 +64,20 @@ async def chat_function(text_in, personality, gender):
|
|
| 58 |
temperature=0.8
|
| 59 |
)
|
| 60 |
response_text = completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
audio_out = await generate_voice(response_text, gender)
|
| 62 |
-
return response_text, audio_out
|
| 63 |
except Exception as e:
|
| 64 |
-
return f"সার্ভারে একটু সমস্যা হইছে মামা! (Error: {str(e)})", None
|
| 65 |
|
| 66 |
# ৫. ইন্টারফেস ডিজাইন
|
| 67 |
with gr.Blocks(css=custom_css, title="DarkZone-AI Multi-Personality") as demo:
|
|
|
|
|
|
|
|
|
|
| 68 |
gr.Markdown("<h1 style='text-align: center; color: #1e40af;'>🌟 DarkZone-AI: Multi-Personality Edition</h1>")
|
| 69 |
gr.Markdown("<p style='text-align: center;'>আপনার পছন্দের স্টাইল সিলেক্ট করে কথা বলা শুরু করুন!</p>")
|
| 70 |
|
|
@@ -83,9 +96,17 @@ with gr.Blocks(css=custom_css, title="DarkZone-AI Multi-Personality") as demo:
|
|
| 83 |
output_text = gr.Textbox(label="এআই রিপ্লাই")
|
| 84 |
output_audio = gr.Audio(label="ভয়েস রিপ্লাই", autoplay=True)
|
| 85 |
|
| 86 |
-
# ক্লিক এবং এন্টার
|
| 87 |
-
submit.click(
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
demo.launch()
|
|
|
|
| 11 |
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# ২. ক্লিন এবং ফ্রেশ CSS (আপনার অরিজিনাল CSS)
|
| 15 |
custom_css = """
|
| 16 |
.gradio-container { background-color: #f8fafc !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
|
| 17 |
.main-box { background: white !important; border-radius: 15px !important; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1); padding: 20px !important; }
|
|
|
|
| 20 |
footer { display: none !important; }
|
| 21 |
"""
|
| 22 |
|
| 23 |
+
# ৩. ভয়েস জেনারেশন ফাংশন (অপরিবর্তিত)
|
| 24 |
async def generate_voice(text, gender):
|
| 25 |
is_bn = any("\u0980" <= c <= "\u09FF" for c in text)
|
| 26 |
if gender == "Male (ছেলে)":
|
|
|
|
| 33 |
await asyncio.to_thread(communicate.save_sync, temp_file.name)
|
| 34 |
return temp_file.name
|
| 35 |
|
| 36 |
+
# ৪. মেইন চ্যাট ফাংশন (হিস্ট্রি অ্যাড করা হয়েছে)
|
| 37 |
+
async def chat_function(text_in, personality, gender, history):
|
| 38 |
if not text_in:
|
| 39 |
+
return "কিছু তো লেখেন!", None, history
|
| 40 |
|
| 41 |
+
# পার্সোনালিটি সেট করা
|
| 42 |
if personality == "Dhakaiya Boss (লোকাল বস)":
|
| 43 |
system_prompt = "You are DarkZone-AI, a technical boss from Dhaka. Speak in local Dhakaiya Bengali slang. Be cool and a bit aggressive like a local big brother."
|
| 44 |
elif personality == "Polite/Normal (ভদ্র স্টাইল)":
|
|
|
|
| 46 |
else: # Gen-Z Style
|
| 47 |
system_prompt = "You are a Gen-Z AI. Use trendy English-Bengali mix, slang like 'bro', 'lit', 'sus', 'vibe'. Be very energetic and informal."
|
| 48 |
|
| 49 |
+
# হিস্ট্রি ম্যানেজমেন্ট: মেসেজ লিস্ট তৈরি
|
| 50 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 51 |
+
|
| 52 |
+
# আগের চ্যাট হিস্ট্রি মেসেজে যোগ করা
|
| 53 |
+
for user_msg, bot_msg in history:
|
| 54 |
+
messages.append({"role": "user", "content": user_msg})
|
| 55 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 56 |
+
|
| 57 |
+
# বর্তমান ইউজারের ইনপুট যোগ করা
|
| 58 |
+
messages.append({"role": "user", "content": text_in})
|
| 59 |
|
| 60 |
try:
|
| 61 |
completion = client.chat.completions.create(
|
|
|
|
| 64 |
temperature=0.8
|
| 65 |
)
|
| 66 |
response_text = completion.choices[0].message.content
|
| 67 |
+
|
| 68 |
+
# নতুন উত্তর হিস্ট্রিতে সেভ করা
|
| 69 |
+
history.append((text_in, response_text))
|
| 70 |
+
|
| 71 |
audio_out = await generate_voice(response_text, gender)
|
| 72 |
+
return response_text, audio_out, history
|
| 73 |
except Exception as e:
|
| 74 |
+
return f"সার্ভারে একটু সমস্যা হইছে মামা! (Error: {str(e)})", None, history
|
| 75 |
|
| 76 |
# ৫. ইন্টারফেস ডিজাইন
|
| 77 |
with gr.Blocks(css=custom_css, title="DarkZone-AI Multi-Personality") as demo:
|
| 78 |
+
# চ্যাট হিস্ট্রি স্টোর করার জন্য স্টেট
|
| 79 |
+
history_state = gr.State([])
|
| 80 |
+
|
| 81 |
gr.Markdown("<h1 style='text-align: center; color: #1e40af;'>🌟 DarkZone-AI: Multi-Personality Edition</h1>")
|
| 82 |
gr.Markdown("<p style='text-align: center;'>আপনার পছন্দের স্টাইল সিলেক্ট করে কথা বলা শুরু করুন!</p>")
|
| 83 |
|
|
|
|
| 96 |
output_text = gr.Textbox(label="এআই রিপ্লাই")
|
| 97 |
output_audio = gr.Audio(label="ভয়েস রিপ্লাই", autoplay=True)
|
| 98 |
|
| 99 |
+
# ক্লিক এবং এন্টার হ্যান্ডেলিং (outputs-এ history_state যোগ করা হয়েছে)
|
| 100 |
+
submit.click(
|
| 101 |
+
chat_function,
|
| 102 |
+
inputs=[msg, personality_opt, gender_opt, history_state],
|
| 103 |
+
outputs=[output_text, output_audio, history_state]
|
| 104 |
+
)
|
| 105 |
+
msg.submit(
|
| 106 |
+
chat_function,
|
| 107 |
+
inputs=[msg, personality_opt, gender_opt, history_state],
|
| 108 |
+
outputs=[output_text, output_audio, history_state]
|
| 109 |
+
)
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
+
demo.launch()
|