File size: 2,032 Bytes
eddaea3 | 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 | from langchain_community.llms import HuggingFaceHub
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
import os
import requests
import gradio as gr
# Configuration
HF_TOKEN = os.environ.get("HF_TOKEN", "")
CLOUDFLARE_API = "https://notary-662-sbz.pages.dev/api/db/chats"
# 1. Setup Llama 3 via Hugging Face Inference API
llm = HuggingFaceHub(
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
huggingfacehub_api_token=HF_TOKEN,
model_kwargs={"temperature": 0.7, "max_new_tokens": 512}
)
# 2. Setup Persian-capable Embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
def query_rag_system(user_query, chat_id="default"):
# Load the 100 docs index (assuming it's saved locally in the Space)
try:
vector_store = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_store.as_retriever())
# Get response from Llama 3 + RAG
result = qa_chain.run(user_query)
# 3. Sync with Cloudflare D1
chat_data = {
"id": chat_id,
"title": user_query[:30],
"docType": "notary_rag_llama",
"messages": [
{"role": "user", "text": user_query},
{"role": "model", "text": result}
]
}
requests.post(CLOUDFLARE_API, json=chat_data)
return result
except Exception as e:
return f"خطا در اتصال به بانک اسناد: {str(e)}"
# Gradio Interface
iface = gr.Interface(
fn=query_rag_system,
inputs="text",
outputs="text",
title="Notary Llama-3 RAG Engine",
description="این سیستم مستقیماً به Cloudflare و ۱۰۰ فایل PDF محضر متصل است."
)
if __name__ == "__main__":
iface.launch()
|