Spaces:
Runtime error
Runtime error
Download app.py from ScanLogic/RAG_Model: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/ScanLogic/RAG_Model/resolve/refs%2Fpr%2F2/app.py
- Command line
-
hf download hf://spaces/ScanLogic/RAG_Model@refs/pr/2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ScanLogic/RAG_Model/resolve/refs%2Fpr%2F2/app.py
3.31 kB
| import gradio as gr | |
| import pandas as pd | |
| import os | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import Chroma | |
| from langchain.chains import RetrievalQA | |
| from transformers import pipeline | |
| from langchain_community.llms import HuggingFacePipeline | |
| print("Hi") | |
| print('try') | |
| # ========================= | |
| # LOAD PDF GUIDELINES | |
| # ========================= | |
| PDF_PATH = "Lemonade_Guidlines.pdf" | |
| loader = PyPDFLoader(PDF_PATH) | |
| docs = loader.load() | |
| # ========================= | |
| # CHUNKING | |
| # ========================= | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=500, | |
| chunk_overlap=100 | |
| ) | |
| chunks = text_splitter.split_documents(docs) | |
| # ========================= | |
| # EMBEDDINGS | |
| # ========================= | |
| embedding_model = HuggingFaceEmbeddings( | |
| model_name="sentence-transformers/all-MiniLM-L6-v2" | |
| ) | |
| # ========================= | |
| # VECTOR DB (FASTER INIT) | |
| # ========================= | |
| vectorstore = Chroma.from_documents( | |
| documents=chunks, | |
| embedding=embedding_model | |
| ) | |
| retriever = vectorstore.as_retriever(search_kwargs={"k": 2}) | |
| # ========================= | |
| # LIGHTWEIGHT LLM (IMPORTANT FIX) | |
| # ========================= | |
| pipe = pipeline( | |
| "text-generation", | |
| model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", # 🔥 small + fast | |
| max_new_tokens=120, | |
| temperature=0.2 | |
| ) | |
| llm = HuggingFacePipeline(pipeline=pipe) | |
| # ========================= | |
| # QA CHAIN | |
| # ========================= | |
| qa_chain = RetrievalQA.from_chain_type( | |
| llm=llm, | |
| retriever=retriever | |
| ) | |
| # ========================= | |
| # VIOLATION CHECK | |
| # ========================= | |
| def check_violation(row): | |
| prompt = f""" | |
| You are a strict legal billing auditor. | |
| Check if the below entry violates guidelines. | |
| Narrative: {row['Narrative (Full Text)']} | |
| Task Code: {row['Task Code']} | |
| Timekeeper: {row['Timekeeper Name']} | |
| Hours: {row['Units (Hrs / Qty)']} | |
| Respond strictly: | |
| Violation: Yes or No | |
| Reason: <one line> | |
| """ | |
| try: | |
| result = qa_chain.invoke({"query": prompt}) | |
| return result["result"] | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # ========================= | |
| # FILE PROCESSING | |
| # ========================= | |
| def process_file(file): | |
| df = pd.read_excel(file, engine="openpyxl") | |
| # Duplicate detection | |
| df["Duplicate Count"] = df.duplicated( | |
| subset=["Narrative (Full Text)", "Units (Hrs / Qty)", "Rate ($/Unit)"], | |
| keep=False | |
| ).astype(int) | |
| results = [] | |
| for i, row in df.iterrows(): | |
| # 🔥 LIMIT rows to avoid timeout (VERY IMPORTANT) | |
| if i > 30: | |
| results.append("Skipped (limit reached)") | |
| continue | |
| res = check_violation(row) | |
| results.append(res) | |
| df["Violation Analysis"] = results | |
| output_path = "audited_output.xlsx" | |
| df.to_excel(output_path, index=False) | |
| return output_path | |
| # ========================= | |
| # UI | |
| # ========================= | |
| interface = gr.Interface( | |
| fn=process_file, | |
| inputs=gr.File(label="Upload Billing Excel"), | |
| outputs=gr.File(label="Download Result"), | |
| title="Legal Billing Auditor", | |
| description="AI-powered billing audit using RAG" | |
| ) | |
| interface.launch() |