Spaces:
Sleeping
Sleeping
Download app.py from abodealdrb/Qalam-University-API: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/abodealdrb/Qalam-University-API/resolve/main/app.py
- Command line
-
hf download hf://spaces/abodealdrb/Qalam-University-API/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abodealdrb/Qalam-University-API/resolve/main/app.py
2.96 kB
| 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() |