GlitchGhost commited on
Commit
e84b2a5
Β·
verified Β·
1 Parent(s): 24bece8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -8,7 +8,7 @@ from bs4 import BeautifulSoup
8
  from sentence_transformers import SentenceTransformer
9
  from langchain_community.vectorstores import FAISS
10
  from langchain.embeddings.base import Embeddings
11
- import google.generativeai as genai
12
 
13
  # === Embeddings Wrapper ===
14
  class SentenceTransformerEmbeddings(Embeddings):
@@ -35,27 +35,28 @@ def split_text(text, chunk_size=500, overlap=50):
35
  start += chunk_size - overlap
36
  return chunks
37
 
38
- def ask_gemini(question, context, api_key):
39
- genai.configure(api_key=api_key)
40
- model = genai.GenerativeModel("gemini-pro")
41
- prompt = f"""You are a helpful assistant. Use the context below to answer the question.
42
-
43
- Context:
44
- {context}
45
-
46
- Question: {question}
47
- Answer:"""
48
- response = model.generate_content(prompt)
49
- return response.text
 
50
 
51
  def create_vectorstore(chunks):
52
  embeddings = SentenceTransformerEmbeddings()
53
  return FAISS.from_texts(chunks, embedding=embeddings)
54
 
55
- def generate_answer(vectorstore, question, api_key):
56
  docs = vectorstore.similarity_search(question, k=3)
57
  context = "\n".join([doc.page_content for doc in docs])
58
- return ask_gemini(question, context, api_key), docs
59
 
60
  def extract_website_text(url):
61
  try:
@@ -75,7 +76,7 @@ st.title("πŸ” RAG Assistant: Chat with PDF, CSV, or Website")
75
  # Sidebar
76
  with st.sidebar:
77
  data_source = st.selectbox("πŸ“‚ Select Input Type", ["PDF", "CSV", "Website URL"])
78
- gemini_api_key = st.text_input("πŸ”‘ Enter Gemini API Key", type="password")
79
 
80
  # === Logic by Data Source ===
81
  vectorstore = None
@@ -120,12 +121,12 @@ elif data_source == "Website URL":
120
  st.success("βœ… Website text extracted and indexed!")
121
 
122
  # === QA Section ===
123
- if vectorstore and gemini_api_key:
124
  st.subheader("❓ Ask a Question")
125
  question = st.text_input("πŸ’¬ Your question")
126
  if question:
127
  with st.spinner("πŸ” Thinking..."):
128
- answer, top_docs = generate_answer(vectorstore, question, gemini_api_key)
129
  st.success("🧠 Answer")
130
  st.write(answer)
131
 
@@ -135,6 +136,5 @@ if vectorstore and gemini_api_key:
135
 
136
  st.download_button("πŸ“€ Download Answer", answer, file_name="rag_answer.txt")
137
 
138
- elif not gemini_api_key:
139
- st.info("πŸ” Please enter your Gemini API key in the sidebar.")
140
-
 
8
  from sentence_transformers import SentenceTransformer
9
  from langchain_community.vectorstores import FAISS
10
  from langchain.embeddings.base import Embeddings
11
+ from transformers import pipeline
12
 
13
  # === Embeddings Wrapper ===
14
  class SentenceTransformerEmbeddings(Embeddings):
 
35
  start += chunk_size - overlap
36
  return chunks
37
 
38
+ def ask_mistral(question, context, hf_api_key):
39
+ # Load the Hugging Face Mistral model pipeline
40
+ nlp = pipeline("question-answering", model="mistral-7b", tokenizer="mistral-7b", use_auth_token=hf_api_key)
41
+
42
+ # Format the input
43
+ inputs = {
44
+ 'context': context,
45
+ 'question': question
46
+ }
47
+
48
+ # Generate the answer using Mistral
49
+ answer = nlp(inputs)
50
+ return answer['answer']
51
 
52
  def create_vectorstore(chunks):
53
  embeddings = SentenceTransformerEmbeddings()
54
  return FAISS.from_texts(chunks, embedding=embeddings)
55
 
56
+ def generate_answer(vectorstore, question, hf_api_key):
57
  docs = vectorstore.similarity_search(question, k=3)
58
  context = "\n".join([doc.page_content for doc in docs])
59
+ return ask_mistral(question, context, hf_api_key), docs
60
 
61
  def extract_website_text(url):
62
  try:
 
76
  # Sidebar
77
  with st.sidebar:
78
  data_source = st.selectbox("πŸ“‚ Select Input Type", ["PDF", "CSV", "Website URL"])
79
+ hf_api_key = st.text_input("πŸ”‘ Enter Hugging Face API Key", type="password")
80
 
81
  # === Logic by Data Source ===
82
  vectorstore = None
 
121
  st.success("βœ… Website text extracted and indexed!")
122
 
123
  # === QA Section ===
124
+ if vectorstore and hf_api_key:
125
  st.subheader("❓ Ask a Question")
126
  question = st.text_input("πŸ’¬ Your question")
127
  if question:
128
  with st.spinner("πŸ” Thinking..."):
129
+ answer, top_docs = generate_answer(vectorstore, question, hf_api_key)
130
  st.success("🧠 Answer")
131
  st.write(answer)
132
 
 
136
 
137
  st.download_button("πŸ“€ Download Answer", answer, file_name="rag_answer.txt")
138
 
139
+ elif not hf_api_key:
140
+ st.info("πŸ” Please enter your Hugging Face API key in the sidebar.")