Spaces:
Sleeping
Sleeping
File size: 8,900 Bytes
36d6a64 2d63152 36d6a64 2d63152 36d6a64 2d63152 0b58b3e 839ea83 0b58b3e 361be6b 0b58b3e 36d6a64 7367161 36d6a64 7367161 36d6a64 08afbf6 8fc5ad4 08afbf6 36d6a64 8fc5ad4 839ea83 8fc5ad4 839ea83 36d6a64 839ea83 36d6a64 361be6b 36d6a64 361be6b 98b8fe1 361be6b 36d6a64 361be6b 36d6a64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# Import the necessary libraries
import subprocess
import sys
# Function to install a package using pip
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
# Install required packages
try:
install("gradio")
install("openai==1.23.2")
install("tiktoken==0.6.0")
install("pypdf==4.0.1")
install("langchain==0.1.1")
install("langchain-community==0.0.13")
install("chromadb==0.4.22")
install("sentence-transformers==2.3.1")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
import gradio as gr
import os
import uuid
import json
import pandas as pd
import subprocess
from openai import OpenAI
from huggingface_hub import HfApi
from huggingface_hub import CommitScheduler
from huggingface_hub import hf_hub_download
import zipfile
# Define your repository and file path
repo_id = "kgauvin603/rag-10k"
file_path = "dataset.zip"
# Download the file
downloaded_file = hf_hub_download(repo_id, file_path)
# Print the path to the downloaded file
print(f"Downloaded file is located at: {downloaded_file}")
from langchain_community.embeddings.sentence_transformer import (
SentenceTransformerEmbeddings
)
from langchain_community.vectorstores import Chroma
#from google.colab import userdata, drive
from pathlib import Path
from langchain.document_loaders import PyPDFDirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import json
import tiktoken
import pandas as pd
import tiktoken
# Define the embedding model and the vectorstore
embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
# If dataset directory exixts, remove it and all of the contents within
#if os.path.exists('dataset'):
# !rm -rf dataset
# If collection_db exists, remove it and all of the contents within
#if os.path.exists('collection_db'):
# !rm -rf dataset
#Mount the Google Drive
#drive.mount('/content/drive')
#Upload Dataset-10k.zip and unzip it dataset folder using -d option
#!unzip Dataset-10k.zip -d dataset
import subprocess
# Command to unzip the file
#command = "unzip kgauvin603/rag-10k-analysis/Dataset-10k.zip -d dataset"
command = "pip install transformers huggingface_hub requests"
# Execute the command
try:
subprocess.run(command, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
from huggingface_hub import hf_hub_download
import zipfile
import os
import requests
# Provide pdf_folder_location
repo_id = "kgauvin603/rag-10k"
file_path = "dataset.zip"
# Get the URL for the file in the repository
file_url = f"https://huggingface.co/{repo_id}/resolve/main/{file_path}"
# Download the file into memory
response = requests.get(file_url)
response.raise_for_status() # Ensure the request was successful
# Open the zip file in memory
with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
# List the files in the zip archive
zip_file_list = zip_ref.namelist()
print(f"Files in the zip archive: {zip_file_list}")
# Extract specific files or work with them directly in memory
# For example, reading a specific file
with zip_ref.open('dataset/some_file.txt') as file:
file_content = file.read()
print(file_content.decode('utf-8'))
# Define the extraction path
#extraction_path = "./extracted_files"
# Create the directory if it doesn't exist
#os.makedirs(extraction_path, exist_ok=True)
# Extract the contents of the zip file
#with zipfile.ZipFile(downloaded_file, 'r') as zip_ref:
# zip_ref.extractall(extraction_path)
# List the files in the extraction path
#extracted_files = os.listdir(extraction_path)
#print(f"Extracted files: {extracted_files}")
# Load the directory to pdf_loader
pdf_loader = PyPDFDirectoryLoader(pdf_folder_location)
# Create text_splitter using recursive splitter
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
encoding_name='cl100k_base',
chunk_size=512,
chunk_overlap=16
)
# Create chunks
report_chunks = pdf_loader.load_and_split(text_splitter)
#Create a Colelction Name
collection_name = 'collection'
# Create the vector Database
vectorstore = Chroma.from_documents(
report_chunks,
embedding_model,
collection_name=collection_name,
persist_directory='./collection_db'
)
# Persist the DB
vectorstore.persist()
vectorstore_persisted = Chroma(
collection_name=collection_name,
persist_directory='./collection_db',
embedding_function=embedding_model
)
retriever = vectorstore_persisted.as_retriever(
search_type='similarity',
search_kwargs={'k': 5}
)
#Mount the Google Drive
#drive.mount('/content/drive')
#Copy the persisted database to your drive
#command = "!cp -r collection_db /content/drive/MyDrive/"
# Execute the command
#try:
# subprocess.run(command, check=True, shell=True)
#except subprocess.CalledProcessError as e:
# print(f"An error occurred: {e}")
# Get anyscale api key
anyscale_api_key = userdata.get('dev-work')
# Initialise the client
client = OpenAI(
base_url="https://api.endpoints.anyscale.com/v1",
api_key=anyscale_api_key
)
#Provide the model name
model_name = 'mlabonne/NeuralHermes-2.5-Mistral-7B'
# Initialise the embedding model
embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
# Load the persisted DB
persisted_vectordb_location = './collection_db'
#Create a Colelction Name
collection_name = 'collection'
# Load the persisted DB
vectorstore_persisted = Chroma(
collection_name=collection_name,
persist_directory=persisted_vectordb_location,
embedding_function=embedding_model
)
# Prepare the logging functionality
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
log_folder = log_file.parent
scheduler = CommitScheduler(
repo_id="kgauvin603/rag-10k-analysis",
repo_type="dataset",
folder_path=log_folder,
path_in_repo="data",
every=2,
token=hf_token
)
# Define the Q&A system message
qna_system_message = """You are an assistant to a financial services firm who answers user queries on annual reports.
User input will have the context required by you to answer user questions.
This context will begin with the token: ###Context.
The context contains references to specific portions of a document relevant to the user query.
User questions will begin with the token: ###Question.
Please answer only using the context provided in the input. Do not mention anything about the context in your final answer.
If the answer is not found in the context, respond "I don't know".
"""
# Create a message template
qna_user_message_template = """
###Context
Here are some documents that are relevant to the question mentioned below.
{context}
###Question
{question}
"""
# Define the predict function that runs when 'Submit' is clicked or when an API request is made
def predict(user_input, company):
filter = "dataset/" + company + "-10-k-2023.pdf"
relevant_document_chunks = vectorstore_persisted.similarity_search(user_input, k=5, filter={"source": filter})
# Create context_for_query
context_list = [d.page_content for d in relevant_document_chunks]
context_for_query = ". ".join(context_list)
# Create messages
prompt = [
{'role': 'system', 'content': qna_system_message},
{'role': 'user', 'content': qna_user_message_template.format(
context=context_for_query,
question=user_input
)}
]
try:
response = client.chat.completions.create(
model=model_name,
messages=prompt,
temperature=0
)
prediction = response.choices[0].message.content.strip()
except Exception as e:
prediction = f'Sorry, I encountered the following error: \n{e}'
# Log both the inputs and outputs to a local log file
# Ensure that the commit scheduler is locked to avoid parallel access
with scheduler.lock:
with log_file.open("a") as f:
f.write(json.dumps(
{
'user_input': user_input,
'retrieved_context': context_for_query,
'model_response': prediction
}
))
f.write("\n")
return prediction
# Set up the Gradio UI
# Add text box and radio button to the interface
# The radio button is used to select the company 10k report in which the context needs to be retrieved.
textbox = gr.Textbox(label="User Input")
#company = gr.List(label="Select Company", choices=["IBM", "Meta", "aws", "google","msft"])
company = gr.Dropdown(label="Select Company", choices=["IBM", "Meta", "aws", "google","msft"])
# Create the interface
# For the inputs parameter of Interface provide [textbox, company]
demo = gr.Interface(fn=predict, inputs=[textbox, company], outputs="text")
demo.queue()
demo.launch(share=True) |