bkbilal09 commited on
Commit
c0ce8e6
Β·
verified Β·
1 Parent(s): a789f6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -49
app.py CHANGED
@@ -9,53 +9,64 @@ from langchain_groq import ChatGroq
9
  from langchain_core.prompts import PromptTemplate
10
  from langchain_classic.chains import RetrievalQA
11
 
 
12
  warnings.filterwarnings("ignore")
13
 
14
  # --- CONFIGURATION ---
15
- # On Hugging Face, this will look for a Secret named 'MY_GROQ_KEY'
16
  GROQ_API_KEY = os.environ.get("MY_GROQ_KEY")
17
 
18
- # High-quality embedding model
19
  embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
20
  rag_chain = None
21
 
22
  def build_rag_system(file):
23
  global rag_chain
24
  if file is None: return "❌ Error: No document uploaded."
25
- if not GROQ_API_KEY: return "❌ Error: Groq API Key not found in Environment Secrets."
26
 
27
  try:
 
28
  loader = PyPDFLoader(file.name) if file.name.endswith(".pdf") else TextLoader(file.name)
29
  documents = loader.load()
30
 
 
31
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=80)
32
  texts = text_splitter.split_documents(documents)
33
 
 
34
  vector_db = FAISS.from_documents(texts, embeddings)
35
  retriever = vector_db.as_retriever(search_kwargs={"k": 3})
36
 
 
37
  llm = ChatGroq(
38
  groq_api_key=GROQ_API_KEY,
39
  model_name="llama-3.3-70b-versatile",
40
  temperature=0
41
  )
42
 
43
- template = """You are a professional research assistant. Answer ONLY using the context below.
44
- If the information is missing, strictly say: "I don't have enough information about this in the provided documents."
 
45
 
46
  Context: {context}
47
  Question: {question}
48
- Professional Answer:"""
49
 
50
  QA_PROMPT = PromptTemplate.from_template(template)
51
- rag_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type_kwargs={"prompt": QA_PROMPT})
 
 
 
 
52
 
53
- return "βœ… Document Indexed Successfully!"
54
  except Exception as e:
55
  return f"❌ System Error: {str(e)}"
56
 
57
  def predict(message, history):
58
- if rag_chain is None: return "Please upload a file and build the knowledge base first."
 
59
  try:
60
  res = rag_chain.invoke({"query": message})
61
  return res["result"]
@@ -63,46 +74,23 @@ def predict(message, history):
63
  return f"🚨 API ERROR: {str(e)}"
64
 
65
  # --- PROFESSIONAL UI DESIGN ---
66
- theme = gr.themes.Monochrome(
67
- primary_hue="blue",
68
- secondary_hue="slate",
69
- radius_size="lg",
70
- font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
71
- ).set(
72
- button_primary_background_fill="*primary_600",
73
- button_primary_background_fill_hover="*primary_700",
74
- button_primary_text_color="white",
75
- body_background_fill="*neutral_50",
76
- )
77
-
78
- with gr.Blocks(theme=theme, title="VerityVault AI") as demo:
79
- with gr.Column(elem_id="container"):
80
- gr.Markdown(
81
- f"""
82
- # πŸ›‘οΈ VerityVault AI
83
- ### Developed by: **Your Name**
84
- *A high-precision Retrieval-Augmented Generation system for secure document analysis.*
85
- """
86
- )
87
-
88
- with gr.Row():
89
- with gr.Column(scale=1):
90
- file_input = gr.File(label="πŸ“„ Source Document", file_types=[".pdf", ".txt"])
91
- build_btn = gr.Button("πŸš€ BUILD KNOWLEDGE BASE", variant="primary")
92
- status = gr.Textbox(label="System Intelligence Status", placeholder="Ready for upload...", interactive=False)
93
-
94
- with gr.Accordion("ℹ️ Instructions", open=False):
95
- gr.Markdown("1. Upload a PDF or TXT file.\n2. Click Build.\n3. Ask questions based ONLY on that file.")
96
-
97
- with gr.Column(scale=2):
98
- gr.ChatInterface(
99
- fn=predict,
100
- type="messages",
101
- description="The engine will refuse to answer if data is not in the source file."
102
- )
103
-
104
- gr.Markdown("---")
105
- gr.Markdown("βš–οΈ *Powered by Llama 3.3 & Groq LPUs. Data is processed locally in volatile memory.*")
106
 
107
  build_btn.click(build_rag_system, inputs=[file_input], outputs=[status])
108
 
 
9
  from langchain_core.prompts import PromptTemplate
10
  from langchain_classic.chains import RetrievalQA
11
 
12
+ # Suppress unnecessary logs
13
  warnings.filterwarnings("ignore")
14
 
15
  # --- CONFIGURATION ---
16
+ # Ensure you have 'MY_GROQ_KEY' in your HF Space Secrets
17
  GROQ_API_KEY = os.environ.get("MY_GROQ_KEY")
18
 
19
+ # High-quality embedding model (Runs on CPU)
20
  embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
21
  rag_chain = None
22
 
23
  def build_rag_system(file):
24
  global rag_chain
25
  if file is None: return "❌ Error: No document uploaded."
26
+ if not GROQ_API_KEY: return "❌ Error: Groq API Key missing in Secrets!"
27
 
28
  try:
29
+ # Load PDF or TXT
30
  loader = PyPDFLoader(file.name) if file.name.endswith(".pdf") else TextLoader(file.name)
31
  documents = loader.load()
32
 
33
+ # Split into chunks
34
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=80)
35
  texts = text_splitter.split_documents(documents)
36
 
37
+ # Create Vector Store
38
  vector_db = FAISS.from_documents(texts, embeddings)
39
  retriever = vector_db.as_retriever(search_kwargs={"k": 3})
40
 
41
+ # Initialize Groq Llama 3.3
42
  llm = ChatGroq(
43
  groq_api_key=GROQ_API_KEY,
44
  model_name="llama-3.3-70b-versatile",
45
  temperature=0
46
  )
47
 
48
+ # Strict Prompting
49
+ template = """You are a professional assistant. Answer ONLY using the context.
50
+ If the answer is not there, say: "I don't have enough information about this in the provided documents."
51
 
52
  Context: {context}
53
  Question: {question}
54
+ Answer:"""
55
 
56
  QA_PROMPT = PromptTemplate.from_template(template)
57
+ rag_chain = RetrievalQA.from_chain_type(
58
+ llm=llm,
59
+ retriever=retriever,
60
+ chain_type_kwargs={"prompt": QA_PROMPT}
61
+ )
62
 
63
+ return "βœ… Document Vault Successfully Built!"
64
  except Exception as e:
65
  return f"❌ System Error: {str(e)}"
66
 
67
  def predict(message, history):
68
+ if rag_chain is None:
69
+ return "Please upload a document and click Build first."
70
  try:
71
  res = rag_chain.invoke({"query": message})
72
  return res["result"]
 
74
  return f"🚨 API ERROR: {str(e)}"
75
 
76
  # --- PROFESSIONAL UI DESIGN ---
77
+ # Removed 'type="messages"' to fix the deployment error
78
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="VerityVault AI") as demo:
79
+ gr.Markdown("# πŸ›‘οΈ VerityVault AI")
80
+ gr.Markdown("### Developed by: **Bilal**")
81
+ gr.Markdown("*Secure document intelligence with zero hallucinations.*")
82
+
83
+ with gr.Row():
84
+ with gr.Column(scale=1):
85
+ file_input = gr.File(label="πŸ“„ Source Document", file_types=[".pdf", ".txt"])
86
+ build_btn = gr.Button("πŸš€ BUILD VAULT", variant="primary")
87
+ status = gr.Textbox(label="Vault Status", interactive=False)
88
+
89
+ with gr.Column(scale=2):
90
+ gr.ChatInterface(
91
+ fn=predict,
92
+ description="The vault will only answer based on your uploaded file."
93
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  build_btn.click(build_rag_system, inputs=[file_input], outputs=[status])
96