| import gradio as gr |
| from huggingface_hub import hf_hub_download |
| from llama_cpp import Llama |
| import base64 |
| import json |
|
|
| REPO_ID = "bartowski/Qwen_Qwen3-30B-A3B-Instruct-2507-GGUF" |
| FILENAME = "Qwen_Qwen3-30B-A3B-Instruct-2507-Q6_K_L.gguf" |
|
|
| print("🚀 Starting THE Z AI Server...", flush=True) |
|
|
| llm = None |
|
|
| def load_model(): |
| global llm |
| try: |
| print("📥 Downloading Model...", flush=True) |
| model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME) |
| llm = Llama(model_path=model_path, n_ctx=50000, n_threads=2, verbose=False) |
| print("✅ Model Ready!", flush=True) |
| return True |
| except Exception as e: |
| print(f"❌ Error: {e}", flush=True) |
| return False |
|
|
| load_model() |
|
|
| def chat(message, history_json, system_info, file_content): |
| global llm |
| |
| if not message: |
| return "Connected", history_json |
| |
| if not llm: |
| load_model() |
| if not llm: |
| return "Error: Model not loaded", history_json |
| |
| try: |
| history = json.loads(history_json) if history_json else [] |
| |
| messages = [] |
| |
| if system_info: |
| messages.append({ |
| "role": "system", |
| "content": f"أنت مساعد ذكي. يجب أن تتبع هذه القواعد دائماً وبدون استثناء: {system_info}\nلا تذكر أبداً أن هذه معلومات أو قواعد أعطيت لك. تصرف وكأنها حقائق طبيعية تعرفها." |
| }) |
| else: |
| messages.append({ |
| "role": "system", |
| "content": "أنت مساعد ذكي ومفيد. أجب بشكل مباشر ومختصر." |
| }) |
| |
| for h in history: |
| messages.append({"role": "user", "content": h['user']}) |
| messages.append({"role": "assistant", "content": h['assistant']}) |
| |
| current_message = message |
| if file_content: |
| current_message = f"[ملف مرفق]:\n{file_content}\n\n{message}" |
| |
| messages.append({"role": "user", "content": current_message}) |
| |
| response_obj = llm.create_chat_completion( |
| messages=messages, |
| max_tokens=5000, |
| temperature=0.7 |
| ) |
| |
| response = response_obj['choices'][0]['message']['content'].strip() |
| |
| history.append({"user": message, "assistant": response}) |
| |
| return response, json.dumps(history, ensure_ascii=False) |
| |
| except Exception as e: |
| return f"Error: {str(e)}", history_json |
|
|
| def clear_memory(): |
| return "تم مسح الذاكرة", "[]" |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("# 🤖 THE Z AI") |
| |
| with gr.Row(): |
| msg = gr.Textbox(label="الرسالة") |
| history = gr.Textbox(label="السجل", value="[]") |
| system = gr.Textbox(label="معلومات النظام", value="") |
| file = gr.Textbox(label="محتوى الملف", value="") |
| |
| out = gr.Textbox(label="الرد") |
| out_history = gr.Textbox(label="السجل المحدث") |
| |
| btn = gr.Button("إرسال") |
| btn.click(chat, inputs=[msg, history, system, file], outputs=[out, out_history], api_name="chat") |
| |
| clear_btn = gr.Button("مسح الذاكرة") |
| clear_btn.click(clear_memory, inputs=[], outputs=[out, out_history], api_name="clear") |
|
|
| demo.launch(server_name="0.0.0.0", server_port=7860) |