Pranav368648638 commited on
Commit
370f105
·
verified ·
1 Parent(s): 2dcef9d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import os
4
+
5
+ from langchain_community.document_loaders import PyPDFLoader
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+ from langchain_huggingface import HuggingFaceEmbeddings
8
+ from langchain_community.vectorstores import Chroma
9
+ from langchain.chains import RetrievalQA
10
+
11
+ from transformers import pipeline
12
+ from langchain_community.llms import HuggingFacePipeline
13
+
14
+ # =========================
15
+ # LOAD PDF GUIDELINES
16
+ # =========================
17
+
18
+ PDF_PATH = "Lemonade_Guidlines.pdf"
19
+
20
+ loader = PyPDFLoader(PDF_PATH)
21
+ docs = loader.load()
22
+
23
+ # =========================
24
+ # CHUNKING
25
+ # =========================
26
+
27
+ text_splitter = RecursiveCharacterTextSplitter(
28
+ chunk_size=500,
29
+ chunk_overlap=100
30
+ )
31
+
32
+ chunks = text_splitter.split_documents(docs)
33
+
34
+ # =========================
35
+ # EMBEDDINGS
36
+ # =========================
37
+
38
+ embedding_model = HuggingFaceEmbeddings(
39
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
40
+ )
41
+
42
+ # =========================
43
+ # VECTOR DB (FASTER INIT)
44
+ # =========================
45
+
46
+ vectorstore = Chroma.from_documents(
47
+ documents=chunks,
48
+ embedding=embedding_model
49
+ )
50
+
51
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
52
+
53
+ # =========================
54
+ # LIGHTWEIGHT LLM (IMPORTANT FIX)
55
+ # =========================
56
+
57
+ pipe = pipeline(
58
+ "text-generation",
59
+ model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", # 🔥 small + fast
60
+ max_new_tokens=120,
61
+ temperature=0.2
62
+ )
63
+
64
+ llm = HuggingFacePipeline(pipeline=pipe)
65
+
66
+ # =========================
67
+ # QA CHAIN
68
+ # =========================
69
+
70
+ qa_chain = RetrievalQA.from_chain_type(
71
+ llm=llm,
72
+ retriever=retriever
73
+ )
74
+
75
+ # =========================
76
+ # VIOLATION CHECK
77
+ # =========================
78
+
79
+ def check_violation(row):
80
+
81
+ prompt = f"""
82
+ You are a strict legal billing auditor.
83
+
84
+ Check if the below entry violates guidelines.
85
+
86
+ Narrative: {row['Narrative (Full Text)']}
87
+ Task Code: {row['Task Code']}
88
+ Timekeeper: {row['Timekeeper Name']}
89
+ Hours: {row['Units (Hrs / Qty)']}
90
+
91
+ Respond strictly:
92
+
93
+ Violation: Yes or No
94
+ Reason: <one line>
95
+ """
96
+
97
+ try:
98
+ result = qa_chain.invoke({"query": prompt})
99
+ return result["result"]
100
+ except Exception as e:
101
+ return f"Error: {str(e)}"
102
+
103
+ # =========================
104
+ # FILE PROCESSING
105
+ # =========================
106
+
107
+ def process_file(file):
108
+
109
+ df = pd.read_excel(file, engine="openpyxl")
110
+
111
+ # Duplicate detection
112
+ df["Duplicate Count"] = df.duplicated(
113
+ subset=["Narrative (Full Text)", "Units (Hrs / Qty)", "Rate ($/Unit)"],
114
+ keep=False
115
+ ).astype(int)
116
+
117
+ results = []
118
+
119
+ for i, row in df.iterrows():
120
+
121
+ # 🔥 LIMIT rows to avoid timeout (VERY IMPORTANT)
122
+ if i > 30:
123
+ results.append("Skipped (limit reached)")
124
+ continue
125
+
126
+ res = check_violation(row)
127
+ results.append(res)
128
+
129
+ df["Violation Analysis"] = results
130
+
131
+ output_path = "audited_output.xlsx"
132
+ df.to_excel(output_path, index=False)
133
+
134
+ return output_path
135
+
136
+ # =========================
137
+ # UI
138
+ # =========================
139
+
140
+ interface = gr.Interface(
141
+ fn=process_file,
142
+ inputs=gr.File(label="Upload Billing Excel"),
143
+ outputs=gr.File(label="Download Result"),
144
+ title="Legal Billing Auditor",
145
+ description="AI-powered billing audit using RAG"
146
+ )
147
+
148
+ interface.launch()