Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
# from langchain.llms import GooglePalm
|
| 4 |
-
|
| 5 |
-
# from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
-
# from langchain.vectorstores import FAISS
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from langchain_community.llms import GooglePalm
|
| 10 |
-
from langchain_community.document_loaders import CSVLoader
|
| 11 |
-
from langchain_community.vectorstores import FAISS
|
| 12 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 13 |
|
| 14 |
|
| 15 |
api_key = "AIzaSyCdM_aAIsW_nPbjarOF83mbX1_z1cVX2_M"
|
| 16 |
|
| 17 |
-
llm =
|
| 18 |
-
|
| 19 |
|
| 20 |
loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
|
| 21 |
data = loader.load()
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
retriever = vectordb.as_retriever()
|
| 28 |
|
|
@@ -58,6 +63,67 @@ def chatresponse(message, history):
|
|
| 58 |
gr.ChatInterface(chatresponse).launch()
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# import gradio as gr
|
| 62 |
# from langchain.llms import GooglePalm
|
| 63 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
# from langchain.llms import GooglePalm
|
| 3 |
+
from langchain_google_genai import GoogleGenerativeAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
api_key = "AIzaSyCdM_aAIsW_nPbjarOF83mbX1_z1cVX2_M"
|
| 7 |
|
| 8 |
+
llm = GoogleGenerativeAI(model="models/text-bison-001", google_api_key=api_key)
|
| 9 |
+
# llm = GooglePalm(google_api_key = api_key, temperature=0.7)
|
| 10 |
|
| 11 |
loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
|
| 12 |
data = loader.load()
|
| 13 |
|
| 14 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 15 |
+
from langchain.vectorstores import FAISS
|
| 16 |
+
import warnings
|
| 17 |
+
|
| 18 |
+
# Suppress specific warnings if they are not critical
|
| 19 |
+
warnings.filterwarnings("ignore", category=UserWarning, message="TypedStorage is deprecated")
|
| 20 |
+
warnings.filterwarnings("ignore", category=FutureWarning, message="`resume_download` is deprecated")
|
| 21 |
+
|
| 22 |
+
# Define the embedding model
|
| 23 |
+
# Using a smaller model for demonstration purposes; adjust according to your needs
|
| 24 |
+
model_name = "BAAI/bge-m3"
|
| 25 |
|
| 26 |
+
# Initialize HuggingFace embeddings
|
| 27 |
+
instructor_embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 28 |
+
|
| 29 |
+
# Create FAISS vector store from documents
|
| 30 |
+
vectordb = FAISS.from_documents(documents=data, embedding=instructor_embeddings)
|
| 31 |
|
| 32 |
retriever = vectordb.as_retriever()
|
| 33 |
|
|
|
|
| 63 |
gr.ChatInterface(chatresponse).launch()
|
| 64 |
|
| 65 |
|
| 66 |
+
|
| 67 |
+
# import gradio as gr
|
| 68 |
+
|
| 69 |
+
# # from langchain.llms import GooglePalm
|
| 70 |
+
# # from langchain.document_loaders.csv_loader import CSVLoader
|
| 71 |
+
# # from langchain_huggingface import HuggingFaceEmbeddings
|
| 72 |
+
# # from langchain.vectorstores import FAISS
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# from langchain_community.llms import GooglePalm
|
| 76 |
+
# from langchain_community.document_loaders import CSVLoader
|
| 77 |
+
# from langchain_community.vectorstores import FAISS
|
| 78 |
+
# from langchain_huggingface import HuggingFaceEmbeddings
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# api_key = "AIzaSyCdM_aAIsW_nPbjarOF83mbX1_z1cVX2_M"
|
| 82 |
+
|
| 83 |
+
# llm = GooglePalm(google_api_key = api_key, temperature=0.7)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
|
| 87 |
+
# data = loader.load()
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# instructor_embeddings = HuggingFaceEmbeddings(model_name = "BAAI/bge-m3")
|
| 91 |
+
# vectordb = FAISS.from_documents(documents = data, embedding = instructor_embeddings)
|
| 92 |
+
|
| 93 |
+
# retriever = vectordb.as_retriever()
|
| 94 |
+
|
| 95 |
+
# from langchain.prompts import PromptTemplate
|
| 96 |
+
|
| 97 |
+
# prompt_template = """Given the following context and a question, generate an answer based on the context only.
|
| 98 |
+
|
| 99 |
+
# In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
| 100 |
+
# If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
|
| 101 |
+
# If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at rishi@aiotsmartlabs.com" Don't try to make up an answer.
|
| 102 |
+
|
| 103 |
+
# CONTEXT: {context}
|
| 104 |
+
|
| 105 |
+
# QUESTION: {question}"""
|
| 106 |
+
|
| 107 |
+
# PROMPT = PromptTemplate(
|
| 108 |
+
# template = prompt_template, input_variables = ["context", "question"]
|
| 109 |
+
# )
|
| 110 |
+
|
| 111 |
+
# from langchain.chains import RetrievalQA
|
| 112 |
+
|
| 113 |
+
# chain = RetrievalQA.from_chain_type(llm = llm,
|
| 114 |
+
# chain_type="stuff",
|
| 115 |
+
# retriever=retriever,
|
| 116 |
+
# input_key="query",
|
| 117 |
+
# return_source_documents=True,
|
| 118 |
+
# chain_type_kwargs = {"prompt": PROMPT})
|
| 119 |
+
|
| 120 |
+
# def chatresponse(message, history):
|
| 121 |
+
# output = chain(message)
|
| 122 |
+
# return output['result']
|
| 123 |
+
|
| 124 |
+
# gr.ChatInterface(chatresponse).launch()
|
| 125 |
+
|
| 126 |
+
|
| 127 |
# import gradio as gr
|
| 128 |
# from langchain.llms import GooglePalm
|
| 129 |
|