File size: 7,034 Bytes
9d77ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b3b0f
9d77ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer, pipeline
from threading import Thread
import gradio as gr
import spaces

# Load system prompt from file
def load_system_prompt():
    try:
        with open('system_prompt.txt', 'r', encoding='utf-8') as f:
            return f.read().strip()
    except FileNotFoundError:
        return "أنت مساعد ذكي مفيد."

DEFAULT_SYSTEM_PROMPT = load_system_prompt()

model_path = "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit"

# إذا كان فيه HF_TOKEN في البيئة
hf_token = os.getenv("HF_TOKEN")

# استخدام ChatPipeline بدلاً من text-generation العادي
tokenizer = AutoTokenizer.from_pretrained(model_path, token=hf_token)
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype="auto", device_map="auto", token=hf_token)

# إنشاء chat pipeline مخصص مع streaming
def create_chat_pipeline(tokenizer, model):
    """إنشاء pipeline مخصص للدردشة مع chat template و streaming"""
    def chat_generate(messages, streamer=None, **kwargs):
        # تحويل الرسائل للـ chat template
        if hasattr(tokenizer, 'apply_chat_template') and tokenizer.chat_template is not None:
            prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
        else:
            # Fallback للموديلات اللي ما عندها chat template
            prompt = ""
            for msg in messages:
                if msg["role"] == "system":
                    prompt += f"System: {msg['content']}\n"
                elif msg["role"] == "user":
                    prompt += f"Human: {msg['content']}\n"
                elif msg["role"] == "assistant":
                    prompt += f"Assistant: {msg['content']}\n"
            prompt += "Assistant:"

        inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

        # توليد الرد مع streaming إذا كان مطلوب
        if streamer:
            generation_kwargs = {
                **inputs,
                "max_new_tokens": kwargs.get('max_new_tokens', 512),
                "temperature": kwargs.get('temperature', 0.7),
                "top_p": kwargs.get('top_p', 0.9),
                "top_k": kwargs.get('top_k', 50),
                "repetition_penalty": kwargs.get('repetition_penalty', 1.1),
                "do_sample": True,
                "pad_token_id": tokenizer.eos_token_id,
                "streamer": streamer,
            }

            # نرجع الـ thread للتشغيل
            return generation_kwargs
        else:
            # للتوليد العادي بدون streaming
            with torch.no_grad():
                outputs = model.generate(
                    **inputs,
                    max_new_tokens=kwargs.get('max_new_tokens', 512),
                    temperature=kwargs.get('temperature', 0.7),
                    top_p=kwargs.get('top_p', 0.9),
                    top_k=kwargs.get('top_k', 50),
                    repetition_penalty=kwargs.get('repetition_penalty', 1.1),
                    do_sample=True,
                    pad_token_id=tokenizer.eos_token_id,
                    return_dict_in_generate=True,
                    output_scores=False,
                )

            response = tokenizer.decode(outputs.sequences[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
            return [{"generated_text": response}]

    return chat_generate

pipe = create_chat_pipeline(tokenizer, model)

def format_conversation_history(chat_history):
    messages = []
    for item in chat_history:
        role = item["role"]
        content = item["content"]
        if isinstance(content, list):
            content = content[0]["text"] if content and "text" in content[0] else str(content)
        messages.append({"role": role, "content": content})
    return messages

@spaces.GPU()
def generate_response(input_data, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
    # حذف الهستوري كله - نبدأ محادثة جديدة دايماً
    # chat_history = []  # تعطيل الهستوري

    new_message = {"role": "user", "content": input_data}

    # Build messages for Llama chat template
    messages = [{"role": "system", "content": DEFAULT_SYSTEM_PROMPT}]
    # لا نضيف chat_history القديم

    # استخدام ChatPipeline المخصص مع streaming
    streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

    generation_kwargs = pipe(
        messages,
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        top_p=top_p,
        top_k=top_k,
        repetition_penalty=repetition_penalty
    )

    thread = Thread(target=model.generate, kwargs=generation_kwargs)
    thread.start()

    # Stream the response
    response = ""
    for chunk in streamer:
        response += chunk
        yield response

demo = gr.ChatInterface(
    fn=generate_response,
    additional_inputs=[
        gr.Slider(label="الحد الأقصى للكلمات الجديدة", minimum=64, maximum=4096, step=1, value=2048),
        gr.Slider(label="درجة الحرارة", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
        gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
        gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=50),
        gr.Slider(label="عقوبة التكرار", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
    ],
    examples=[
        [{"text": "النت عندي معطل من الصبح، تقدر تساعدني؟"}],
        [{"text": "عندي مشكلة بالاتصال بالواي فاي"}],
        [{"text": "شنو الباقات المتوفرة عندكم؟"}],
        [{"text": "كيف أعيد ضبط الجهاز؟"}],
        [{"text": "My device is not working properly"}],
    ],
    cache_examples=False,
    type="messages",
    title="دعم عملاء TechSolutions - مساعد أليكس (العراقي)",
    description="""🤖 مساعد خدمة عملاء ذكي لـ TechSolutions

✨ المميزات:
- 🌐 دعم ثنائي اللغة (عربي وإنجليزي)
- 💬 لهجة محادثة طبيعية
- 🔧 دعم فني واستكشاف الأخطاء
- 📋 معلومات الخدمات والإرشاد
- 🎯 مدعوم بـ موديل anaspro العراقي (Llama 3.1 محسن للعربية العراقية)

احجي مع أليكس لحل مشاكلك التقنية، استفسر عن الخدمات، أو احصل على معلومات المنتجات.""",
    fill_height=True,
    textbox=gr.Textbox(
        label="اكتب رسالتك هنا",
        placeholder="مثال: عندي مشكلة بالجهاز..."
    ),
    stop_btn="إيقاف التوليد",
    multimodal=False,
    theme=gr.themes.Soft()
)

if __name__ == "__main__":
    demo.launch()