Spaces:
Sleeping
Sleeping
File size: 2,956 Bytes
f3968a2 c608e3c 2c4f737 c608e3c f3968a2 c608e3c db89e2b f3968a2 c608e3c f3968a2 c608e3c f3968a2 c608e3c f3968a2 c608e3c f3968a2 c608e3c f3968a2 c608e3c 2c4f737 c608e3c f3968a2 2c4f737 f3968a2 2c4f737 f3968a2 c608e3c 2c4f737 c608e3c f3968a2 876742f | 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 | import gradio as gr
from langchain_huggingface import HuggingFaceEndpoint
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
# التحديث 1: استيراد مقسم النصوص من مكتبته الخاصة
from langchain_text_splitters import RecursiveCharacterTextSplitter
# التحديث 2: (حل مشكلتك الحالية) استيراد القوالب من النواة Core
from langchain_core.prompts import PromptTemplate
import os
# ---------------------------------------------------------
# 1. إعداد الاتصال بالموديل
# ---------------------------------------------------------
# نستخدم موديل Llama-3 الرسمي مؤقتاً
REPO_ID = "mistralai/Mistral-7B-Instruct-v0.3"
# جلب التوكن
hf_token = os.getenv("HF_TOKEN")
# الاتصال بالموديل (Serverless)
llm = HuggingFaceEndpoint(
repo_id=REPO_ID,
max_new_tokens=512,
temperature=0.1,
repetition_penalty=1.1,
huggingfacehub_api_token=hf_token
)
# ---------------------------------------------------------
# 2. بناء قاعدة المعرفة (RAG)
# ---------------------------------------------------------
print("Building RAG Knowledge Base...")
try:
with open("knowledge_base.txt", "r", encoding="utf-8") as f:
knowledge_text = f.read()
except FileNotFoundError:
knowledge_text = "لا توجد معلومات. يرجى رفع ملف knowledge_base.txt"
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
docs = text_splitter.create_documents([knowledge_text])
embeddings = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-small")
db = FAISS.from_documents(docs, embeddings)
retriever = db.as_retriever(search_kwargs={"k": 3})
# ---------------------------------------------------------
# 3. دالة المعالجة
# ---------------------------------------------------------
custom_prompt_template = """
أنت المساعد الذكي لجامعة القلم. استخدم المعلومات التالية للإجابة على سؤال الطالب بدقة.
المعلومات المرجعية:
{context}
سؤال الطالب:
{question}
الإجابة:
"""
prompt = PromptTemplate(template=custom_prompt_template, input_variables=["context", "question"])
def predict(message):
try:
relevant_docs = retriever.invoke(message)
context = "\n".join([d.page_content for d in relevant_docs])
full_input = custom_prompt_template.format(context=context, question=message)
response = llm.invoke(full_input)
return response
except Exception as e:
return f"Error: {str(e)}"
# ---------------------------------------------------------
# 4. تشغيل الواجهة
# ---------------------------------------------------------
iface = gr.Interface(fn=predict, inputs="text", outputs="text")
iface.launch() |