bainskarman commited on
Commit
15f5963
·
verified ·
1 Parent(s): c0a164f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -34
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import os
3
- from huggingface_hub import InferenceApi
4
  from PyPDF2 import PdfReader
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
@@ -11,10 +11,24 @@ from langdetect import detect
11
  token = os.environ.get("KEY2") # Replace "KEY2" with your secret key name
12
 
13
  # Initialize the Hugging Face Inference API
14
- def load_llm():
15
  model_name = "HuggingFaceH4/zephyr-7b-alpha" # Replace with your preferred model
16
- api = InferenceApi(repo_id=model_name, token=token)
17
- return api
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Extract text from PDF
20
  def extract_text_from_pdf(file):
@@ -44,7 +58,7 @@ def create_vector_store(chunks, indexing_method="multi-representation", **kwargs
44
  return vector_store
45
 
46
  # Query the PDF using the Hugging Face API
47
- def query_pdf(vector_store, query, api, query_method="multi-query", max_new_tokens=200, temperature=0.7, top_k=50):
48
  # Retrieve relevant chunks from the vector store
49
  docs = vector_store.similarity_search(query)
50
  context = " ".join([doc.page_content for doc in docs])
@@ -53,15 +67,8 @@ def query_pdf(vector_store, query, api, query_method="multi-query", max_new_toke
53
  prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
54
 
55
  # Query the Hugging Face API
56
- response = api(
57
- inputs=prompt,
58
- parameters={
59
- "max_new_tokens": max_new_tokens,
60
- "temperature": temperature,
61
- "top_k": top_k,
62
- },
63
- )
64
- return response[0]["generated_text"], docs
65
 
66
  # Detect language of the text
67
  def detect_language(text):
@@ -107,43 +114,33 @@ def main():
107
  st.session_state.vector_store = create_vector_store(st.session_state.chunks, indexing_method=indexing_method)
108
  st.success("Vector store created!")
109
 
110
- # Step 3: Load LLM (Hugging Face API)
111
- if "api" not in st.session_state:
112
- st.session_state.api = None
113
-
114
  if st.session_state.vector_store:
115
- st.subheader("LLM Parameters")
116
- temperature = st.slider("Temperature", 0.1, 1.0, 0.7, help="Controls randomness in the output.")
117
- top_k = st.slider("Top-k", 1, 100, 50, help="Limits sampling to the top-k tokens.")
118
- max_new_tokens = st.slider("Max New Tokens", 50, 500, 200, help="Maximum number of tokens to generate.")
119
- if st.button("Load LLM"):
120
- api = load_llm()
121
- st.session_state.api = api
122
- st.success("LLM loaded!")
123
-
124
- # Step 4: Query the PDF
125
- if st.session_state.api:
126
  st.subheader("Query Translation Options")
127
  query_method = st.selectbox(
128
  "Query Translation Method",
129
  ["multi-query", "rag-fusion", "decomposition", "step-back", "hyde"],
130
  help="Choose a method to improve query retrieval."
131
  )
 
 
 
 
132
  query = st.text_input("Ask a question about the PDF:")
133
  if query:
134
  answer, source_docs = query_pdf(
135
  st.session_state.vector_store,
136
  query,
137
- st.session_state.api,
138
  query_method=query_method,
139
  max_new_tokens=max_new_tokens,
140
  temperature=temperature,
141
  top_k=top_k,
142
  )
143
- st.write("**Answer:**", answer)
144
- st.write("**Source Text:**")
145
- for doc in source_docs:
146
- st.write(doc.page_content)
 
147
 
148
  if __name__ == "__main__":
149
  main()
 
1
  import streamlit as st
2
  import os
3
+ import requests
4
  from PyPDF2 import PdfReader
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
 
11
  token = os.environ.get("KEY2") # Replace "KEY2" with your secret key name
12
 
13
  # Initialize the Hugging Face Inference API
14
+ def query_huggingface_api(prompt, max_new_tokens=200, temperature=0.7, top_k=50):
15
  model_name = "HuggingFaceH4/zephyr-7b-alpha" # Replace with your preferred model
16
+ api_url = f"https://api-inference.huggingface.co/models/{model_name}"
17
+ headers = {"Authorization": f"Bearer {token}"}
18
+ payload = {
19
+ "inputs": prompt,
20
+ "parameters": {
21
+ "max_new_tokens": max_new_tokens,
22
+ "temperature": temperature,
23
+ "top_k": top_k,
24
+ },
25
+ }
26
+ response = requests.post(api_url, headers=headers, json=payload)
27
+ if response.status_code == 200:
28
+ return response.json()[0]["generated_text"]
29
+ else:
30
+ st.error(f"Error: {response.status_code} - {response.text}")
31
+ return None
32
 
33
  # Extract text from PDF
34
  def extract_text_from_pdf(file):
 
58
  return vector_store
59
 
60
  # Query the PDF using the Hugging Face API
61
+ def query_pdf(vector_store, query, query_method="multi-query", max_new_tokens=200, temperature=0.7, top_k=50):
62
  # Retrieve relevant chunks from the vector store
63
  docs = vector_store.similarity_search(query)
64
  context = " ".join([doc.page_content for doc in docs])
 
67
  prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
68
 
69
  # Query the Hugging Face API
70
+ answer = query_huggingface_api(prompt, max_new_tokens=max_new_tokens, temperature=temperature, top_k=top_k)
71
+ return answer, docs
 
 
 
 
 
 
 
72
 
73
  # Detect language of the text
74
  def detect_language(text):
 
114
  st.session_state.vector_store = create_vector_store(st.session_state.chunks, indexing_method=indexing_method)
115
  st.success("Vector store created!")
116
 
117
+ # Step 3: Query the PDF
 
 
 
118
  if st.session_state.vector_store:
 
 
 
 
 
 
 
 
 
 
 
119
  st.subheader("Query Translation Options")
120
  query_method = st.selectbox(
121
  "Query Translation Method",
122
  ["multi-query", "rag-fusion", "decomposition", "step-back", "hyde"],
123
  help="Choose a method to improve query retrieval."
124
  )
125
+ st.subheader("LLM Parameters")
126
+ temperature = st.slider("Temperature", 0.1, 1.0, 0.7, help="Controls randomness in the output.")
127
+ top_k = st.slider("Top-k", 1, 100, 50, help="Limits sampling to the top-k tokens.")
128
+ max_new_tokens = st.slider("Max New Tokens", 50, 500, 200, help="Maximum number of tokens to generate.")
129
  query = st.text_input("Ask a question about the PDF:")
130
  if query:
131
  answer, source_docs = query_pdf(
132
  st.session_state.vector_store,
133
  query,
 
134
  query_method=query_method,
135
  max_new_tokens=max_new_tokens,
136
  temperature=temperature,
137
  top_k=top_k,
138
  )
139
+ if answer:
140
+ st.write("**Answer:**", answer)
141
+ st.write("**Source Text:**")
142
+ for doc in source_docs:
143
+ st.write(doc.page_content)
144
 
145
  if __name__ == "__main__":
146
  main()