import gradio as gr from huggingface_hub import hf_hub_download from llama_cpp import Llama # --- 1. تحميل النموذج من Hugging Face Repo --- print("⏳ جاري تحميل النموذج من المستودع...") model_path = hf_hub_download( repo_id="Bilalshai/UnivCh-GGUF", # اسم المستودع filename="UnivCh-Llama3.1-8B-Q4_K_M.gguf" # اسم الملف داخل المستودع ) print(f"✅ تم تنزيل النموذج: {model_path}") # --- 2. تهيئة النموذج --- print("🧠 جاري تحميل النموذج في الذاكرة...") llm = Llama( model_path=model_path, n_ctx=2048, n_threads=8 # عدد الخيوط حسب قدرات Space ) print("🎉 النموذج جاهز!") # --- 3. دالة الدردشة --- def chat_fn(message, history=[]): prompt = "" for user_msg, bot_msg in history: prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n" prompt += f"User: {message}\nAssistant:" response = llm(prompt, max_tokens=512, stop=["User:", "Assistant:"]) answer = response["choices"][0]["text"].strip() history.append((message, answer)) return history, history # --- 4. واجهة Gradio --- iface = gr.ChatInterface( fn=chat_fn, title="🤖 UnivCh Chatbot", description="دردشة مع نموذج UnivCh-Llama3.1-8B من Hugging Face Repo" ) if __name__ == "__main__": iface.launch()