dsouzaJithesh commited on
Commit
44ab0cd
·
verified ·
1 Parent(s): 403f919

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +24 -0
  2. indexer.py +53 -0
  3. requirements.txt +14 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from indexer import index_text, answer_query
3
+
4
+ # Gradio interface function to handle the RAG system
5
+ def rag_system(query):
6
+ # Index the input text
7
+ vectorstore = index_text()
8
+
9
+ # Answer the query based on the indexed text
10
+ answer = answer_query(query, vectorstore)
11
+
12
+ return answer
13
+
14
+ # Build the Gradio interface
15
+ iface = gr.Interface(
16
+ fn=rag_system,
17
+ inputs=["text"],
18
+ outputs="text",
19
+ title="AI Alignment Bot",
20
+ description="If the model can't answer, it will say sorry I don't know"
21
+ )
22
+
23
+ # Launch the app
24
+ iface.launch()
indexer.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.vectorstores import FAISS
2
+ from langchain_core.documents import Document
3
+ from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
4
+ import os
5
+ from google import genai
6
+ from google.genai import types
7
+
8
+ # Set up the Gemini API key
9
+ import os
10
+
11
+ def index_text():
12
+
13
+ os.environ["NVIDIA_API_KEY"] ="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"
14
+
15
+ nvidia_embeddings = NVIDIAEmbeddings(
16
+ model="nvidia/llama-3.2-nv-embedqa-1b-v2",
17
+ truncate="NONE"
18
+ )
19
+ vectorstore = FAISS.load_local("nvidia_faiss_index", embeddings=nvidia_embeddings,allow_dangerous_deserialization=True)
20
+ return vectorstore
21
+
22
+
23
+ def answer_query(query, vectorstore):
24
+ RAG_TEMPLATE = """
25
+ #CONTEXT:
26
+ {context}
27
+
28
+ QUERY:
29
+ {query}
30
+
31
+ Use the provided context to answer the user query. Only use the provided context to answer the query.
32
+ If you do not know the answer, or it's not contained in the provided context, respond with "I don't know".
33
+ """
34
+ os.environ["GEMINI_API_KEY"] = "AIzaSyCP3iHlrhG-aDKaZlNKzYE3yXA-7pLGCxM"
35
+ client = genai.Client()
36
+ # Get relevant documents
37
+ retriever = vectorstore.as_retriever()
38
+ search_results = retriever.invoke(query, k=2)
39
+
40
+ # Combine context from retrieved documents
41
+ context = " ".join([doc.page_content for doc in search_results])
42
+
43
+ # Build prompt
44
+ prompt = RAG_TEMPLATE.format(context=context, query=query)
45
+
46
+ # Generate response using Gemini
47
+ response = client.models.generate_content(
48
+ model="gemini-2.5-pro",
49
+ contents=prompt,
50
+ config=types.GenerateContentConfig(),
51
+ )
52
+
53
+ return response.text
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ langchain
3
+ langchain_community
4
+ langchain-core
5
+ transformers
6
+ faiss-cpu
7
+ sentence-transformers
8
+ huggingface_hub
9
+ pydantic
10
+ google-genai
11
+ langchain_nvidia_ai_endpoints
12
+ langchain_community
13
+ langchain_core
14
+ faiss-cpu