Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,95 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import os
         | 
| 3 | 
            +
            from openai import OpenAI
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # تنظیم کلاینت - توکن از Secrets Hugging Face خوانده میشود
         | 
| 6 | 
            +
            client = OpenAI(
         | 
| 7 | 
            +
                base_url="https://router.huggingface.co/v1",
         | 
| 8 | 
            +
                api_key=os.environ.get("HF_TOKEN", "")
         | 
| 9 | 
            +
            )
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            def chat_with_ehteshami(message, history):
         | 
| 12 | 
            +
                try:
         | 
| 13 | 
            +
                    # اضافه کردن تاریخچه مکالمه
         | 
| 14 | 
            +
                    messages = [
         | 
| 15 | 
            +
                        {
         | 
| 16 | 
            +
                            "role": "system", 
         | 
| 17 | 
            +
                            "content": "You are Ehteshami AI - a helpful Persian and English assistant. پاسخها رو به فارسی بده مگر اینکه کاربر درخواست زبان دیگری کند."
         | 
| 18 | 
            +
                        }
         | 
| 19 | 
            +
                    ]
         | 
| 20 | 
            +
                    
         | 
| 21 | 
            +
                    # اضافه کردن تاریخچه چت
         | 
| 22 | 
            +
                    for user_msg, bot_msg in history:
         | 
| 23 | 
            +
                        messages.append({"role": "user", "content": user_msg})
         | 
| 24 | 
            +
                        messages.append({"role": "assistant", "content": bot_msg})
         | 
| 25 | 
            +
                    
         | 
| 26 | 
            +
                    # اضافه کردن پیام جدید
         | 
| 27 | 
            +
                    messages.append({"role": "user", "content": message})
         | 
| 28 | 
            +
                    
         | 
| 29 | 
            +
                    completion = client.chat.completions.create(
         | 
| 30 | 
            +
                        model="deepseek-ai/DeepSeek-V3.2-Exp:novita",
         | 
| 31 | 
            +
                        messages=messages,
         | 
| 32 | 
            +
                        max_tokens=500,
         | 
| 33 | 
            +
                        temperature=0.7
         | 
| 34 | 
            +
                    )
         | 
| 35 | 
            +
                    
         | 
| 36 | 
            +
                    response = completion.choices[0].message.content
         | 
| 37 | 
            +
                    return response
         | 
| 38 | 
            +
                    
         | 
| 39 | 
            +
                except Exception as e:
         | 
| 40 | 
            +
                    return f"⚠️ خطا در ارتباط با سرور: {str(e)}"
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            # ساخت رابط چت زیبا
         | 
| 43 | 
            +
            with gr.Blocks(theme=gr.themes.Soft(), title="Ehteshami AI") as demo:
         | 
| 44 | 
            +
                gr.Markdown(
         | 
| 45 | 
            +
                    """
         | 
| 46 | 
            +
                    # 🤖 Ehteshami AI  
         | 
| 47 | 
            +
                    **هوش مصنوعی احتشامی - دستیار فارسی/انگلیسی شما**
         | 
| 48 | 
            +
                    
         | 
| 49 | 
            +
                    🌐 ساخته شده با Hugging Face Spaces + DeepSeek V3.2
         | 
| 50 | 
            +
                    """
         | 
| 51 | 
            +
                )
         | 
| 52 | 
            +
                
         | 
| 53 | 
            +
                chatbot = gr.Chatbot(
         | 
| 54 | 
            +
                    label="مکالمه",
         | 
| 55 | 
            +
                    height=500,
         | 
| 56 | 
            +
                    show_copy_button=True
         | 
| 57 | 
            +
                )
         | 
| 58 | 
            +
                
         | 
| 59 | 
            +
                with gr.Row():
         | 
| 60 | 
            +
                    msg = gr.Textbox(
         | 
| 61 | 
            +
                        label="پیام شما",
         | 
| 62 | 
            +
                        placeholder="پیام خود را اینجا تایپ کنید... (فارسی یا انگلیسی)",
         | 
| 63 | 
            +
                        scale=4,
         | 
| 64 | 
            +
                        container=False
         | 
| 65 | 
            +
                    )
         | 
| 66 | 
            +
                    
         | 
| 67 | 
            +
                    send_btn = gr.Button("📤 ارسال", scale=1)
         | 
| 68 | 
            +
                
         | 
| 69 | 
            +
                with gr.Row():
         | 
| 70 | 
            +
                    clear_btn = gr.Button("🧹 پاک کردن مکالمه")
         | 
| 71 | 
            +
                    info_btn = gr.Button("ℹ️ اطلاعات")
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                def user_message(user_msg, chat_history):
         | 
| 74 | 
            +
                    if not user_msg.strip():
         | 
| 75 | 
            +
                        return "", chat_history
         | 
| 76 | 
            +
                    
         | 
| 77 | 
            +
                    bot_response = chat_with_ehteshami(user_msg, chat_history)
         | 
| 78 | 
            +
                    chat_history.append((user_msg, bot_response))
         | 
| 79 | 
            +
                    return "", chat_history
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                def clear_chat():
         | 
| 82 | 
            +
                    return []
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                def show_info():
         | 
| 85 | 
            +
                    return [("سیستم", "Ehteshami AI v1.0 - آماده خدماترسانی!")]
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                # اتصال رویدادها
         | 
| 88 | 
            +
                msg.submit(user_message, [msg, chatbot], [msg, chatbot])
         | 
| 89 | 
            +
                send_btn.click(user_message, [msg, chatbot], [msg, chatbot])
         | 
| 90 | 
            +
                clear_btn.click(clear_chat, None, chatbot)
         | 
| 91 | 
            +
                info_btn.click(show_info, None, chatbot)
         | 
| 92 | 
            +
             | 
| 93 | 
            +
            # اجرای برنامه
         | 
| 94 | 
            +
            if __name__ == "__main__":
         | 
| 95 | 
            +
                demo.launch(share=True, show_error=True)
         | 
