{"repo": "artyomza312/doziscolar", "files": {"Sin.py": {"18febb45253e997e413049cd33aa2d2560611d17": {"t": 1785674365.0749898, "n": 5291, "h": "3be964466416c3d4", "c": "import numpy as np\nimport requests\nfrom langchain_community.document_loaders import PyPDFLoader\nfrom langchain.text_splitter import CharacterTextSplitter\nfrom sentence_transformers import SentenceTransformer\nimport faiss\nfrom langchain.llms.base import LLM\nfrom langchain.prompts import PromptTemplate\nfrom langchain.chains import LLMChain\nfrom .config import api_key\n# 1. Load and split PDF into chunks\npdf_path = \"test.pdf\"\nloader = PyPDFLoader(pdf_path)\npages = loader.load()\ntext_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n\nchunks, metadatas = [], []\nfor page in pages:\n page_num = page.metadata.get(\"page\")\n for idx, text in enumerate(text_splitter.split_text(page.page_content)):\n chunks.append(text)\n metadatas.append({\"page\": page_num, \"chunk_index\": idx})\n\n# 2. Create embeddings and FAISS index\nembedding_model = SentenceTransformer('all-MiniLM-L6-v2')\nembeddings = embedding_model.encode(chunks, show_progress_bar=True)\ndim = embeddings.shape[1]\nindex = faiss.IndexFlatL2(dim)\nindex.add(np.array(embeddings))\n\n# 3. Retrieval function\ndef retrieve_chunks(question: str, top_k: int = 3) -> list:\n q_emb = embedding_model.encode([question])\n distances, indices = index.search(np.array(q_emb), k=top_k)\n return [\n {\n \"text\": chunks[i],\n \"page\": metadatas[i][\"page\"],\n \"chunk_index\": metadatas[i][\"chunk_index\"],\n \"score\": float(dist)\n }\n for dist, i in zip(distances[0], indices[0])\n ]\n\n# 4. Prompt template \ncustom_template = \"\"\"\nAnswer the following question ONLY using the provided context below.\nIf the answer is not found in the context, say 'Not found in context.'\nCite all supporting text as [Page X, Chunk Y].\n\nContext:\n{summaries}\n\nQuestion: {question}\nAnswer:\n\"\"\"\nprompt = PromptTemplate(\n template=custom_template,\n input_variables=[\"summaries\", \"question\"]\n)\n\ndef build_context(retrieved: list) -> str:\n return \"\\n\\n\".join([\n f\"[Page {c['page']}, Chunk {c['chunk_index']}]\\n{c['text']}\"\n for c in retrieved\n ])\n\n# 5. OpenRouter LLM integration با فیلد Pydantic به‌جای __init__\nclass OpenRouterLLM(LLM):\n api_key: str\n model_name: str = \"openrouter/cypher-alpha:free\"\n api_url: str = \"https://openrouter.ai/api/v1/chat/completions\"\n\n def _call(self, prompt: str, stop=None, **kwargs) -> str:\n headers = {\n \"Authorization\": f\"Bearer {self.api_key}\",\n \"Content-Type\": \"application/json\"\n }\n payload = {\n \"model\": self.model_name,\n \"messages\": [{\"role\": \"user\", \"content\": prompt}]\n }\n resp = requests.post(self.api_url, headers=headers, json=payload, timeout=30)\n resp.raise_for_status()\n data = resp.json()\n return data[\"choices\"][0][\"message\"][\"content\"]\n\n @property\n def _identifying_params(self):\n return {\"model_name\": self.model_name}\n\n @property\n def _llm_type(self):\n return \"openrouter\"\n\n# 6. End-to-end execution\ndef answer_question(question: str, top_k: int = 3) -> str:\n retrieved = retrieve_chunks(question, top_k)\n context = build_context(retrieved)\n\n llm = OpenRouterLLM(\n api_key=api_key\n )\n chain = LLMChain(llm=llm, prompt=prompt)\n return chain.run(summaries=context, question=question)\n\n\nclass TranslateLLM(LLM):\n api_key: str\n model_name: str\n api_url: str = \"https://openrouter.ai/api/v1/chat/completions\"\n\n def _call(self, prompt: str, stop=None, **kwargs) -> str:\n headers = {\n \"Authorization\": f\"Bearer {self.api_key}\",\n \"Content-Type\": \"application/json\"\n }\n payload = {\n \"model\": self.model_name,\n \"messages\": [{\"role\": \"user\", \"content\": prompt}]\n }\n resp = requests.post(self.api_url, headers=headers, json=payload, timeout=30)\n resp.raise_for_status()\n data = resp.json()\n return data[\"choices\"][0][\"message\"][\"content\"]\n\n @property\n def _identifying_params(self):\n return {\"model_name\": self.model_name}\n\n @property\n def _llm_type(self):\n return \"openrouter\"\n\n\ndef translate_fa2en(text):\n llm = TranslateLLM(api_key=api_key, model_name=\"deepseek/deepseek-r1-0528:free\")\n\n prompt = f\"\"\"Translate the following Persian text to English. Only output the English translation, with no extra explanation.\n\n Persian: {text}\n English:\"\"\"\n\n response = llm(prompt)\n # Strip whitespace, just in case\n return response.strip()\n\ndef translate_en2fa(text):\n llm = TranslateLLM(api_key=api_key, model_name=\"deepseek/deepseek-r1-0528:free\")\n\n prompt = f\"\"\"Translate the following English text to Persian. Only output the Persian translation, with no extra explanation.\n\n English: {text}\n Persian:\"\"\"\n\n response = llm(prompt)\n # Strip whitespace, just in case\n return response.strip()\n\n\n\nif __name__ == \"__main__\":\n while True:\n q = input(\"سؤال را وارد کنید (یا '1' برای خروج): \").strip()\n if q == \"1\":\n print(\"خروج از برنامه.\")\n break\n q_new = translate_fa2en(q)\n print(f\"Processing: {q_new}\")\n pred = answer_question(q, top_k=3)\n pred_new = translate_en2fa(pred)\n print(\"Answer:\", pred_new)"}}}}