danishjameel003 commited on
Commit
160fbe1
·
verified ·
1 Parent(s): 3455401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -73
app.py CHANGED
@@ -1,12 +1,9 @@
 
1
  import os
2
- import torch
3
  import streamlit as st
4
- from langchain_community.embeddings import HuggingFaceEmbeddings
5
- from langchain_community.vectorstores import FAISS
6
- from langchain_core.prompts import PromptTemplate
7
- from langchain.chains import LLMChain
8
- from langchain_community.llms import HuggingFacePipeline
9
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
10
  from dotenv import load_dotenv
11
 
12
  # Set Streamlit page configuration
@@ -15,54 +12,28 @@ st.set_page_config(page_title="Chat with Notes and AI", page_icon=":books:", lay
15
  # Load environment variables
16
  load_dotenv()
17
 
18
- # Dolly-v2-3b model pipeline
19
- @st.cache_resource
20
- def load_pipeline():
21
- model_name = "databricks/dolly-v2-3b"
22
-
23
- # Load tokenizer
24
- tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left", trust_remote_code=True)
25
-
26
- # Load model with offload folder for disk storage of weights
27
- model = AutoModelForCausalLM.from_pretrained(
28
- model_name,
29
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32, # Use float16 for GPU, float32 for CPU
30
- device_map="auto", # Automatically map model to available devices (e.g., GPU if available)
31
- trust_remote_code=True,
32
- offload_folder="./offload_weights" # Folder to store offloaded weights
33
- )
34
-
35
- # Return text-generation pipeline
36
- return pipeline(
37
- task="text-generation",
38
- model=model,
39
- tokenizer=tokenizer,
40
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
41
- device_map="auto",
42
- return_full_text=True
43
- )
44
-
45
- # Initialize Dolly pipeline
46
- generate_text = load_pipeline()
47
-
48
- # Create a HuggingFace pipeline wrapper for LangChain
49
- hf_pipeline = HuggingFacePipeline(pipeline=generate_text)
50
 
51
  # Template for instruction-only prompts
52
- prompt = PromptTemplate(
53
- input_variables=["instruction"],
54
- template="{instruction}"
55
- )
56
-
57
- # Template for prompts with context
58
- prompt_with_context = PromptTemplate(
59
- input_variables=["instruction", "context"],
60
- template="{instruction}\n\nInput:\n{context}"
61
- )
62
-
63
- # Create LLM chains
64
- llm_chain = LLMChain(llm=hf_pipeline, prompt=prompt)
65
- llm_context_chain = LLMChain(llm=hf_pipeline, prompt=prompt_with_context)
 
 
 
66
 
67
  # Extracting text from .txt files
68
  def get_text_files_content(folder):
@@ -75,41 +46,31 @@ def get_text_files_content(folder):
75
 
76
  # Converting text to chunks
77
  def get_chunks(raw_text):
78
- from langchain.text_splitter import CharacterTextSplitter
79
  text_splitter = CharacterTextSplitter(
80
  separator="\n",
81
  chunk_size=1000, # Reduced chunk size for faster processing
82
  chunk_overlap=200, # Smaller overlap for efficiency
83
  length_function=len
84
  )
85
- chunks = text_splitter.split_text(raw_text)
86
- return chunks
87
 
88
- # Using Hugging Face embeddings model and FAISS to create vectorstore
89
  def get_vectorstore(chunks):
90
- embeddings = HuggingFaceEmbeddings(
91
- model_name="sentence-transformers/all-MiniLM-L6-v2",
92
- model_kwargs={'device': 'cpu'} # Ensure embeddings use CPU
93
- )
94
  vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
95
  return vectorstore
96
 
97
  # Generating response from user queries
98
  def handle_question(question, vectorstore=None):
99
  if vectorstore:
100
- # Reduce the number of retrieved chunks for faster processing
101
  documents = vectorstore.similarity_search(question, k=2)
102
  context = "\n".join([doc.page_content for doc in documents])
103
-
104
- # Limit context to 1000 characters to speed up model inference
105
- context = context[:1000]
106
-
107
- if context:
108
- result_with_context = llm_context_chain.invoke({"instruction": question, "context": context})
109
- return result_with_context
110
-
111
- # Fallback to instruction-only chain if no context is found
112
- return llm_chain.invoke({"instruction": question})
113
 
114
  def main():
115
  st.title("Chat with Notes :books:")
@@ -171,7 +132,7 @@ def main():
171
  if st.session_state.vectorstore:
172
  response = handle_question(question, st.session_state.vectorstore)
173
  st.subheader("Answer:")
174
- st.write(response.get("text", "No response found."))
175
  else:
176
  st.warning("Please load the content for the selected subject before asking a question.")
177
 
 
1
+ import openai
2
  import os
 
3
  import streamlit as st
4
+ from langchain.text_splitter import CharacterTextSplitter
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.embeddings import OpenAIEmbeddings
 
 
 
7
  from dotenv import load_dotenv
8
 
9
  # Set Streamlit page configuration
 
12
  # Load environment variables
13
  load_dotenv()
14
 
15
+ # OpenAI API Key (set in .env or directly in your environment)
16
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "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")
17
+ openai.api_key = OPENAI_API_KEY
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Template for instruction-only prompts
20
+ def generate_openai_response(instruction, context=None):
21
+ try:
22
+ messages = [
23
+ {"role": "system", "content": "You are a helpful assistant."},
24
+ {"role": "user", "content": instruction},
25
+ ]
26
+ if context:
27
+ messages.append({"role": "user", "content": f"Context: {context}"})
28
+ response = openai.ChatCompletion.create(
29
+ model="gpt-4",
30
+ messages=messages,
31
+ max_tokens=1200,
32
+ temperature=0.7
33
+ )
34
+ return response["choices"][0]["message"]["content"]
35
+ except Exception as e:
36
+ return f"Error: {str(e)}"
37
 
38
  # Extracting text from .txt files
39
  def get_text_files_content(folder):
 
46
 
47
  # Converting text to chunks
48
  def get_chunks(raw_text):
 
49
  text_splitter = CharacterTextSplitter(
50
  separator="\n",
51
  chunk_size=1000, # Reduced chunk size for faster processing
52
  chunk_overlap=200, # Smaller overlap for efficiency
53
  length_function=len
54
  )
55
+ return text_splitter.split_text(raw_text)
 
56
 
57
+ # Using OpenAI embeddings model and FAISS to create vectorstore
58
  def get_vectorstore(chunks):
59
+ embeddings = OpenAIEmbeddings() # Uses OpenAI Embeddings
 
 
 
60
  vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
61
  return vectorstore
62
 
63
  # Generating response from user queries
64
  def handle_question(question, vectorstore=None):
65
  if vectorstore:
66
+ # Retrieve relevant chunks using similarity search
67
  documents = vectorstore.similarity_search(question, k=2)
68
  context = "\n".join([doc.page_content for doc in documents])
69
+ context = context[:1000] # Limit context size for faster processing
70
+ return generate_openai_response(question, context)
71
+ else:
72
+ # Fallback to instruction-only prompt if no context is found
73
+ return generate_openai_response(question)
 
 
 
 
 
74
 
75
  def main():
76
  st.title("Chat with Notes :books:")
 
132
  if st.session_state.vectorstore:
133
  response = handle_question(question, st.session_state.vectorstore)
134
  st.subheader("Answer:")
135
+ st.write(response)
136
  else:
137
  st.warning("Please load the content for the selected subject before asking a question.")
138