File size: 3,454 Bytes
daa5b50
 
 
 
 
 
da0dc0f
 
daa5b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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)