File size: 4,723 Bytes
7e0b3c1
 
ccccb88
 
eaa998e
ccccb88
 
 
 
 
 
 
 
 
a526abd
5114a9c
eaa998e
 
 
 
ccccb88
eaa998e
a526abd
5114a9c
 
ccccb88
eaa998e
ccccb88
 
7e0b3c1
a3d234b
ccccb88
a3d234b
7e0b3c1
 
 
a3d234b
a526abd
 
 
 
 
 
eaa998e
ccccb88
 
 
 
a526abd
 
 
eaa998e
a526abd
ccccb88
eaa998e
ccccb88
 
eaa998e
ccccb88
 
 
a526abd
 
ccccb88
 
eaa998e
ccccb88
a526abd
 
 
ccccb88
eaa998e
ccccb88
eaa998e
 
 
a526abd
ccccb88
eaa998e
 
 
a526abd
 
 
 
eaa998e
 
 
a526abd
 
 
 
 
 
 
eaa998e
 
ccccb88
eaa998e
ccccb88
eaa998e
ccccb88
 
a526abd
 
 
 
 
 
ccccb88
 
 
7e0b3c1
 
4b8d1fe
eaa998e
7e0b3c1
 
eaa998e
4b8d1fe
ccccb88
a526abd
7e0b3c1
4b8d1fe
eaa998e
 
 
7e0b3c1
eaa998e
 
 
 
 
 
 
 
a3d234b
eaa998e
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
import numpy as np
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import os

# التحقق من توفر GPU
device = "cuda" if torch.cuda.is_available() else "cpu"

@st.cache_resource
def load_model():
    """
    تحميل النموذج والمُرمِّز مع التخزين المؤقت
    """
    model_name = "joermd/speedy-llama2"
    
    # تهيئة الـtokenizer أولاً
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    
    # تهيئة النموذج مع إعدادات مناسبة
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
        low_cpu_mem_usage=True,
        device_map="auto"
    )
    
    return model, tokenizer

def reset_conversation():
    '''
    إعادة تعيين المحادثة
    '''
    st.session_state.conversation = []
    st.session_state.messages = []
    return None

def format_prompt(prompt):
    """
    تنسيق المدخل بالطريقة المناسبة لنموذج LLaMA
    """
    return f"<s>[INST] {prompt} [/INST]"

def generate_response(model, tokenizer, prompt, temperature=0.7, max_length=500):
    """
    توليد استجابة من النموذج
    """
    try:
        # تنسيق المدخل
        formatted_prompt = format_prompt(prompt)
        
        # تحضير المدخلات
        inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device)
        
        # توليد النص
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_length=max_length,
                temperature=temperature,
                do_sample=True,
                pad_token_id=tokenizer.eos_token_id,
                repetition_penalty=1.2  # لتجنب التكرار
            )
        
        # فك ترميز النص المولد
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # إزالة النص الأصلي من الاستجابة
        response = response.replace(formatted_prompt, "").strip()
        return response
        
    except Exception as e:
        return f"حدث خطأ أثناء توليد الاستجابة: {str(e)}"

# تهيئة Streamlit
st.title("LLaMA-2 Chat 🦙")

# إضافة أزرار التحكم في الشريط الجانبي
with st.sidebar:
    st.header("إعدادات")
    temperature = st.slider("درجة الإبداعية", min_value=0.1, max_value=1.0, value=0.7, step=0.1,
                          help="قيمة أعلى تعني إجابات أكثر إبداعية وتنوعاً")
    max_tokens = st.slider("الحد الأقصى للكلمات", min_value=50, max_value=1000, value=500, step=50,
                          help="الحد الأقصى لطول الإجابة")
    
    if st.button("مسح المحادثة"):
        reset_conversation()
    
    st.markdown("---")
    st.markdown("""
    ### معلومات النموذج
    - **النموذج:** Speedy LLaMA-2
    - **الجهاز:** {} 
    """.format("GPU ⚡" if device == "cuda" else "CPU 💻"))

# تحميل النموذج
try:
    with st.spinner("جاري تحميل النموذج... قد يستغرق هذا بضع دقائق..."):
        model, tokenizer = load_model()
    st.sidebar.success("تم تحميل النموذج بنجاح! 🎉")
except Exception as e:
    st.error(f"حدث خطأ أثناء تحميل النموذج: {str(e)}")
    st.error("""
    تأكد من تثبيت جميع المكتبات المطلوبة:
    ```bash
    pip install transformers torch accelerate streamlit
    ```
    """)
    st.stop()

# تهيئة سجل المحادثة
if "messages" not in st.session_state:
    st.session_state.messages = []

# عرض المحادثة السابقة
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])

# معالجة إدخال المستخدم
if prompt := st.chat_input("اكتب رسالتك هنا..."):
    st.session_state.messages.append({"role": "user", "content": prompt})
    
    with st.chat_message("user"):
        st.write(prompt)
    
    with st.chat_message("assistant"):
        with st.spinner("جاري التفكير..."):
            response = generate_response(
                model=model,
                tokenizer=tokenizer,
                prompt=prompt,
                temperature=temperature,
                max_length=max_tokens
            )
            st.write(response)
            st.session_state.messages.append({"role": "assistant", "content": response})