VoidZeroe/scouter
0
1 2import gradio as gr3import random4import time5import os6import requests7from langchain.document_loaders import TextLoader #for textfiles8from langchain.text_splitter import CharacterTextSplitter #text splitter9from langchain.embeddings.openai import OpenAIEmbeddings #for using HugginFace models10from langchain.vectorstores import FAISS #facebook vectorizationfrom langchain.chains.question_answering import load_qa_chain11from langchain.chains.question_answering import load_qa_chain12from langchain import OpenAI13from langchain.document_loaders import UnstructuredPDFLoader #load pdf14from langchain.indexes import VectorstoreIndexCreator #vectorize db index with chromadb15from langchain.chains import RetrievalQAWithSourcesChain16from langchain.document_loaders import DirectoryLoader17from langchain.document_loaders import UnstructuredFileLoader18 19from langchain.document_loaders.csv_loader import CSVLoader20from langchain.document_loaders import TextLoader21from langchain.document_loaders import Docx2txtLoader22 23from langchain.vectorstores import FAISS24 25from langchain.callbacks.base import BaseCallbackHandler26 27from openai.error import APIError28 29from langchain.embeddings.openai import OpenAIEmbeddings30from langchain.chat_models import ChatOpenAI31from langchain.vectorstores import FAISS32from langchain.chains import LLMChain33from langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPT34from langchain.prompts import MessagesPlaceholder35from langchain.callbacks.base import BaseCallbackHandler36 37from langchain.prompts import PromptTemplate38from langchain.prompts import ChatPromptTemplate39 40 41openAI_embeddings = OpenAIEmbeddings(openai_api_key = "sk***2T")42 43 44 45 46loader = CSVLoader(file_path='survey.csv', csv_args={47 'delimiter': ',',48 'quotechar': '"',49 'fieldnames': ['Question', 'Answer']50})51 52docs = loader.load()53char_text_splitter = CharacterTextSplitter(chunk_size = 500, chunk_overlap = 15)54docs = char_text_splitter.split_documents(docs)55surveydb = FAISS.from_documents(docs, openAI_embeddings)56 57 58 59loader = TextLoader("transcript.txt")60 61 62docs = loader.load()63char_text_splitter = CharacterTextSplitter(chunk_size = 500, chunk_overlap = 15)64docs = char_text_splitter.split_documents(docs)65transcriptdb = FAISS.from_documents(docs, openAI_embeddings)66 67loader = TextLoader("virscout.txt")68 69docs = loader.load()70char_text_splitter = CharacterTextSplitter(chunk_size = 500, chunk_overlap = 15)71docs = char_text_splitter.split_documents(docs)72virscoutdb = FAISS.from_documents(docs, openAI_embeddings)73 74loader = TextLoader("evaluation.txt")75 76docs = loader.load()77char_text_splitter = CharacterTextSplitter(chunk_size = 500, chunk_overlap = 15)78docs = char_text_splitter.split_documents(docs)79evaluatedb = FAISS.from_documents(docs, openAI_embeddings)80 81loader = Docx2txtLoader("note.docx")82 83docs = loader.load()84char_text_splitter = CharacterTextSplitter(chunk_size = 500, chunk_overlap = 15)85docs = char_text_splitter.split_documents(docs)86notedb = FAISS.from_documents(docs, openAI_embeddings)87 88from queue import Queue, Empty89 90class QueueCallback(BaseCallbackHandler):91 """Callback handler for streaming LLM responses to a queue."""92 93 def __init__(self, q):94 self.q = q95 96 def on_llm_new_token(self, token: str, **kwargs) -> None:97 self.q.put(token)98 99 def on_llm_end(self, *args, **kwargs) -> None:100 return self.q.empty()101 102 103def stream_docs(input_text,104 apikey, 105 chat_history = [], 106 role_grade = 7, 107 survey_vectorstore = surveydb, 108 note_vectorstore = notedb, 109 evaluation_vectorstore = evaluatedb, 110 virscout_vectorstore = virscoutdb, 111 interview_vectorstore = transcriptdb):112 113 # Create a Queue114 q = Queue()115 job_done = object()116 117 system_message = """Use the information from the below sources to answer any questions.118 Role: You are a scout that helps in deciding if a player will be a good fit for a team.119 120 The provided data is about a baseball player to be drafted.121 122 Survey: Survey of medical questions and responses and general knowledge about the player123 <survey>124 {survey}125 </survey>126 127 Scouts' Note: Notes about the player128 The note is in this format:129 The date, followed by the scout name.130 The next line is the scout title131 The third line is the scout note about the player132 <scouts note>133 {scouts_note}134 </scouts note>135 136 Scouts' Evaluation Note: Evaluation Notes about the player137 The note is in this format:138 The first line is the scout last name139 The second line is the scout evaluation of the player140 <evaluation note>141 {evaluation_note}142 </evaluation note>143 144 VirScout: Virscout Interview Notes and Mindset data about the player145 The note is in this format:146 Multiple interview question answer pair147 Then followed by mindset data148 The mindset data contains a key called Pro Sim, this measures the similarity of the player to an ideal pro player, it is a value that ranges from -1 to 1149 <virscout>150 {virscout}151 </virscout>152 153 Scout Interview: Transcript of the player interview with a scout.154 This transcript consists only of words uttered by the player.155 <interview>156 {interview}157 </interview>158 159 Role Grade: The role grade given to the player by different scouts160 The text is in this format:161 Scout Name: Role grade162 163 Use the following definitions to interprete the Role grade in the form `Role grade: Interpretation`164 8: Hall of Fame / Elite165 7: Consistent All-Star166 6: All-Star167 5.5: Solid Everyday168 5: Everyday169 4.5: Platoon / Bench170 4: Up /Down171 3: Org Player172 2: Non-Prospect173 <role_grade>174 {role_grade}175 </role_grade>176 177 Use the following piece of context, if you don't know the answer, simply say I don't know. Always give the answer in Markdown.178 179 {context}180 """181 182 llm = ChatOpenAI(temperature=0, model_name='gpt-4', openai_api_key = apikey)183 184 llm_chain = LLMChain(185 llm=llm,186 prompt=CONDENSE_QUESTION_PROMPT187 )188 189 standalone_question = llm_chain(190 {"chat_history": chat_history, "question": input_text})['text']191 192 qa_prompt = ChatPromptTemplate.from_messages(193 [("system", system_message), ("human", "{question}")])194 195 llm = ChatOpenAI(streaming=True, callbacks=[QueueCallback(196 q)], temperature=0, model_name='gpt-3.5-turbo', 197 openai_api_key = apikey)198 199 # Initialize the LLM we'll be using200 full_chain = {201 "survey": (lambda x: x['question']) | survey_vectorstore.as_retriever(),202 "interview": (lambda x: x['question']) | interview_vectorstore.as_retriever(),203 "scouts_note": (lambda x: x['question']) | note_vectorstore.as_retriever(),204 "virscout": (lambda x: x['question']) | virscout_vectorstore.as_retriever(),205 "evaluation_note": (lambda x: x['question']) | evaluation_vectorstore.as_retriever(),206 "role_grade": (lambda x: x['role_grade']),207 "question": lambda x: x['question'],208 "context": lambda x: x['chat_history']209 } | qa_prompt | llm210 211 # Create a function to call - this will run in a thread212 return(full_chain.invoke({"question": standalone_question,213 "chat_history": chat_history,214 "role_grade":role_grade}))215 216 217 218 219with gr.Blocks(theme= gr.themes.Monochrome()) as demo:220 with gr.Group():221 gr.Label("ScoutBot", show_label=False)222 apikey = gr.Textbox(show_label=False, type="password", placeholder="OpenAI Key")223 chatbot = gr.Chatbot(layout="bubble",bubble_full_width=False, label = "Test Suite")224 msg = gr.Textbox(show_label = False, placeholder = "Type a message")225 with gr.Row():226 submit = gr.Button("Submit")227 clear = gr.ClearButton([msg, chatbot])228 229 def respond(message, chat_history, apikey):230 try:231 bot_message = stream_docs(message, apikey, chat_history=chat_history).content232 except:233 bot_message = "please verify your api key above"234 chat_history.append((message, bot_message))235 #time.sleep(2)236 return "", chat_history237 238 msg.submit(respond, [msg, chatbot, apikey], [msg, chatbot])239 submit.click(respond, [msg, chatbot, apikey], [msg, chatbot])240demo.launch(share = True)241 