Spaces:
Sleeping
Sleeping
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import gradio as grimport osfrom openai import OpenAI
# تنظیم کلاینت - توکن از Secrets Hugging Face خوانده میشودclient = OpenAI( base_url="https://router.huggingface.co/v1", api_key=os.environ.get("HF_TOKEN", ""))
def chat_with_ehteshami(message, history): try: # اضافه کردن تاریخچه مکالمه messages = [ { "role": "system", "content": "You are Ehteshami AI - a helpful Persian and English assistant. پاسخها رو به فارسی بده مگر اینکه کاربر درخواست زبان دیگری کند." } ] # اضافه کردن تاریخچه چت 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": message}) completion = client.chat.completions.create( model="deepseek-ai/DeepSeek-V3.2-Exp:novita", messages=messages, max_tokens=500, temperature=0.7 ) response = completion.choices[0].message.content return response except Exception as e: return f"⚠️ خطا در ارتباط با سرور: {str(e)}"
# ساخت رابط چت زیباwith gr.Blocks(theme=gr.themes.Soft(), title="Ehteshami AI") as demo: gr.Markdown( """ # 🤖 Ehteshami AI **هوش مصنوعی احتشامی - دستیار فارسی/انگلیسی شما** 🌐 ساخته شده با Hugging Face Spaces + DeepSeek V3.2 """ ) chatbot = gr.Chatbot( label="مکالمه", height=500, show_copy_button=True ) with gr.Row(): msg = gr.Textbox( label="پیام شما", placeholder="پیام خود را اینجا تایپ کنید... (فارسی یا انگلیسی)", scale=4, container=False ) send_btn = gr.Button("📤 ارسال", scale=1) with gr.Row(): clear_btn = gr.Button("🧹 پاک کردن مکالمه") info_btn = gr.Button("ℹ️ اطلاعات")
def user_message(user_msg, chat_history): if not user_msg.strip(): return "", chat_history bot_response = chat_with_ehteshami(user_msg, chat_history) chat_history.append((user_msg, bot_response)) return "", chat_history
def clear_chat(): return []
def show_info(): return [("سیستم", "Ehteshami AI v1.0 - آماده خدماترسانی!")]
# اتصال رویدادها msg.submit(user_message, [msg, chatbot], [msg, chatbot]) send_btn.click(user_message, [msg, chatbot], [msg, chatbot]) clear_btn.click(clear_chat, None, chatbot) info_btn.click(show_info, None, chatbot)
# اجرای برنامهif __name__ == "__main__": demo.launch(share=True, show_error=True)app.pyAdded+95