tahirsher commited on
Commit
8df49ab
β€’
1 Parent(s): e3d0b8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -1,24 +1,20 @@
1
-
2
  import os
3
  from PyPDF2 import PdfReader
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
- from langchain.vectorstores import FAISS
6
- from transformers import pipeline
7
  import streamlit as st
8
  import requests
9
  from io import BytesIO
 
10
 
11
- # Use HF_TOKEN for Hugging Face model access
12
- #HF_TOKEN = os.getenv("HF_TOKEN") # Ensure this environment variable is set
13
-
14
- # Replace `HF_TOKEN` in the pipeline instantiation directly with your token string (for testing purposes only).
15
- pipe = pipeline("text-generation", model="meta-llama/Llama-Guard-3-8B-INT8", use_auth_token="HF_TOKEN")
16
-
17
 
18
  # List of GitHub PDF URLs
19
  PDF_URLS = [
20
- "https://github.com/TahirSher/GenAI_Lawyers_Guide/blob/main/bi%20pat%20graphs.pdf",
21
  "https://github.com/TahirSher/GenAI_Lawyers_Guide/blob/main/bi-partite.pdf",
 
22
  # Add more document links as needed
23
  ]
24
 
@@ -49,19 +45,20 @@ def load_or_create_vector_store(text_chunks):
49
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
50
  return vector_store
51
 
52
- def generate_answer(user_question, context_text):
53
- # Format the input message for the pipeline
54
- messages = [
55
- {"role": "user", "content": f"Context: {context_text}\nQuestion: {user_question}"}
56
- ]
57
- # Generate response using the pipeline
58
- response = pipe(messages, max_length=250, do_sample=True)
59
- return response[0]['generated_text'][:250] # Limit response to 250 characters
 
60
 
61
  def user_input(user_question, vector_store):
62
  docs = vector_store.similarity_search(user_question)
63
  context_text = " ".join([doc.page_content for doc in docs])
64
- return generate_answer(user_question, context_text)
65
 
66
  def main():
67
  st.set_page_config(page_title="RAG-based PDF Chat", layout="centered", page_icon="πŸ“„")
 
 
1
  import os
2
  from PyPDF2 import PdfReader
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_community.vectorstores import FAISS
 
5
  import streamlit as st
6
  import requests
7
  from io import BytesIO
8
+ import groq.client as client # Ensure Groq client is properly installed
9
 
10
+ # Set up Groq API key
11
+ GROQ_API_KEY = os.getenv("Groq_Api_Key")
12
+ client.configure(api_key=GROQ_API_KEY)
 
 
 
13
 
14
  # List of GitHub PDF URLs
15
  PDF_URLS = [
 
16
  "https://github.com/TahirSher/GenAI_Lawyers_Guide/blob/main/bi-partite.pdf",
17
+ "https://github.com/TahirSher/GenAI_Lawyers_Guide/blob/main/bi%20pat%20graphs.pdf",
18
  # Add more document links as needed
19
  ]
20
 
 
45
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
46
  return vector_store
47
 
48
+ # Call Groq API for generating summary based on the query and retrieved text
49
+ def generate_summary_with_groq(query, retrieved_text):
50
+ chat_completion = client.chat.completions.create(
51
+ messages=[
52
+ {"role": "user", "content": f"{query}\n\nRelated information:\n{retrieved_text}"}
53
+ ],
54
+ model="llama3-8b-8192",
55
+ )
56
+ return chat_completion.choices[0].message.content
57
 
58
  def user_input(user_question, vector_store):
59
  docs = vector_store.similarity_search(user_question)
60
  context_text = " ".join([doc.page_content for doc in docs])
61
+ return generate_summary_with_groq(user_question, context_text)
62
 
63
  def main():
64
  st.set_page_config(page_title="RAG-based PDF Chat", layout="centered", page_icon="πŸ“„")