import streamlit as st import gradio as gr from datetime import datetime import persianutils as pu from transformers import pipeline # تنظیمات پایه SETTINGS = { "dark_mode": False, "font_size": 14, "language": "fa", "theme_color": "#fa8721" } # پیامهای پیشفرض DEFAULT_MESSAGES = [ "سلام! چطور میتونم کمکتون کنم؟", "خوشحالم که اینجا هستید!", "بفرمایید، در خدمتم" ] # آیکونهای SVG ICONS = { 'settings': '''''', 'user': '''''', 'send': '''''' } # تابع پردازش پیام و تاریخ def chat_assistant(message, history): """پردازش پیام و تولید پاسخ""" current_time = pu.convert_en_numbers(datetime.now().strftime("%H:%M")) response = DEFAULT_MESSAGES[0] # پیام پیشفرض history = history + [(message, response)] return "", history # تابع ساخت رابط کاربری def create_interface(): """ساخت رابط کاربری""" with gr.Blocks(css=get_custom_css()) as demo: # هدر gr.HTML(create_header()) # بخش چت chatbox = gr.Chatbot( elem_id="chatbox", label="چت با دستیار", placeholder="پیام خود را تایپ کنید...", height="70vh" ) # بخش ورودی with gr.Row(): with gr.Column(scale=10): msg_box = gr.Textbox( show_label=False, placeholder="پیام خود را بنویسید...", elem_id="message-box" ) with gr.Column(scale=2): send_btn = gr.Button( "ارسال", variant="primary", elem_id="send-button" ) # تنظیمات with gr.Accordion("تنظیمات", open=False): dark_mode = gr.Checkbox( label="حالت شب", value=SETTINGS["dark_mode"] ) font_size = gr.Slider( minimum=12, maximum=20, value=SETTINGS["font_size"], label="اندازه فونت" ) language = gr.Dropdown( choices=["فارسی", "English"], value="فارسی", label="زبان" ) # رویدادها msg_box.submit( chat_assistant, [msg_box, chatbox], [msg_box, chatbox] ) send_btn.click( chat_assistant, [msg_box, chatbox], [msg_box, chatbox] ) return demo # تابع برای اضافه کردن استایلهای سفارشی def get_custom_css(): """استایلهای سفارشی""" return """ @font-face { font-family: 'Vazir'; src: url('https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v27.2.2/dist/Vazir.woff2') format('woff2'); } * { font-family: 'Vazir', Arial, sans-serif; direction: rtl; } .container { max-width: 800px; margin: 0 auto; padding: 20px; } .chat-area { height: 500px; overflow-y: auto; padding: 20px; background: #f5f5f5; border-radius: 10px; } .message { margin: 10px 0; padding: 10px; border-radius: 10px; animation: fadeIn 0.3s ease-out; } .user-message { background: white; margin-left: 20%; } .bot-message { background: #fa8721; color: white; margin-right: 20%; } @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } } """ # تابع برای ساخت هدر def create_header(): """ساخت هدر""" return f"""