wuchangsheng951 commited on
Commit ·
99fb858
1
Parent(s): 15026db
Add application file
Browse files14 unmodified lines1516171819202122232425263 unmodified lines3031323334359 unmodified lines45464748495051525354555657584 unmodified lines636465666768691 unmodified line717273747576121 unmodified lines1981992002012022032046 unmodified lines21121221321421521621 unmodified lines2382392402412422432442452462472482492503 unmodified lines25425525625725825913 unmodified lines27327427527627727827918 unmodified lines29829930030130230330430530630730830914 unmodified linesfrom PIL import Imageimport osfrom langchain.indexes.vectorstore import VectorstoreIndexCreatorfrom langchain.chains import VectorDBQAfrom langchain import OpenAIfrom langchain.document_loaders import UnstructuredPDFLoaderfrom langchain.vectorstores.faiss import FAISSfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom flask import send_fileclass REPOGPT:3 unmodified linesself.api_key = Nonedef init_agent(self, api_key, repo_link = None, load_vectorstore = None):self.repo_link = repo_linkself.api_key = api_keyself.load_vectorstore = load_vectorstore9 unmodified linesos.environ["OPENAI_API_KEY"] = self.api_keyif self.load_vectorstore == None:loader = UnstructuredPDFLoader( self.create_repo_pdf(self.repo_link,image_included = image_included))pages = loader.load_and_split()self.index = VectorstoreIndexCreator(vectorstore_cls = FAISS).from_loaders([loader])self.vectorstore = self.index.vectorstoreelse:embeddings = OpenAIEmbeddings()self.vectorstore = FAISS.load_local('asd.json',embeddings =embeddings)self.qa = VectorDBQA.from_chain_type(llm =OpenAI(temperature=0, model_name="gpt-3.5-turbo"), chain_type = "stuff",vectorstore = self.vectorstore )4 unmodified linesdef download_repo_zip(self, link, output_folder = "main.zip"):username = link.split('/')[3]repo = link.split('/')[4]# zip_url = f"https://github.com/{username}/{repo}/archive/refs/heads/main.zip"zip_url = f"https://github.com/{username}/{repo}/archive/refs/heads/master.zip"self.zip_url = zip_urlresponse = requests.get(zip_url)1 unmodified line#down load the zip filewith open('main.zip', 'wb') as f:f.write(response.content)# return BytesIO(response.content)def extract_zip(self, zip_file, destination_folder):121 unmodified linesdef create_repo_pdf(self, repo_link, image_included = False, merged_pdf = "merged.pdf"):self.merged_pdf_path = merged_pdfself.download_repo_zip(repo_link)folder_name = self.extract_zip('./main.zip', './')6 unmodified linesingnore_list.append('.bmp')ingnore_list.append('.tiff')pdf_files = []for root, dirs, files in os.walk(folder_name):for file in files:21 unmodified linesshutil.rmtree(folder_name)shutil.rmtree("temp")return merged_pdfdef Answer_quetsion(self, question):return self.qa.run(question)repogpt = REPOGPT()def call_output(string = 'REPOGPT Initializing'):3 unmodified lines# filename = repogpt.get_pdf()return send_file(filename, as_attachment=True)with gr.Blocks() as demo:with gr.Row():gr.Markdown("<h3><center>REPOGPT</center></h3>")13 unmodified lineswith gr.Row():repo_link = gr.Textbox(placeholder="Paste your repo_link and press Enter ↵️",label = 'Repo_link, Like: https://github.com/wuchangsheng951/RepoGPT ',show_label=True,lines=1,18 unmodified linesgr.Examples(examples=["Whats the name of this repo?","Whats this repo for?","How can I use this Example code ? Step by step","how can I use this Experiment trackers ? Step by step","how can I Performing gradient accumulation with Accelerate? Step by step?","the models.py ? can you show me the code ? step by step","model initialization ? can you show me the code ? step by step","Whats the learning rate of this model?",],inputs=txt)14 unmodified lines151617181920212223242526273 unmodified lines313233343536373839409 unmodified lines5051525354555657585960616263644 unmodified lines6970717273741 unmodified line7677787980818283121 unmodified lines2052062072082092102116 unmodified lines21821922022122222322421 unmodified lines2462472482492502512522532542552562572582592603 unmodified lines26426526626726826927027127227313 unmodified lines28728828929029129229318 unmodified lines31231331431531631731831932032132232332432514 unmodified linesfrom PIL import Imageimport osfrom langchain.indexes.vectorstore import VectorstoreIndexCreatorfrom langchain.chains import VectorDBQA,VectorDBQAWithSourcesChainfrom langchain import OpenAIfrom langchain.document_loaders import UnstructuredPDFLoaderfrom langchain.vectorstores.faiss import FAISSfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom flask import send_filefrom IPython.display import Markdown, displayclass REPOGPT:3 unmodified linesself.api_key = Nonedef init_agent(self, api_key, repo_link = None, load_vectorstore = None):try:os.remove('merged.pdf')except:passself.repo_link = repo_linkself.api_key = api_keyself.load_vectorstore = load_vectorstore9 unmodified linesos.environ["OPENAI_API_KEY"] = self.api_keyif self.load_vectorstore == None:loader = UnstructuredPDFLoader( self.create_repo_pdf(self.repo_link,image_included = image_included))# pages = loader.load_and_split()self.index = VectorstoreIndexCreator(vectorstore_cls = FAISS).from_loaders([loader])self.vectorstore = self.index.vectorstoreprint(' vectorstore created')else:embeddings = OpenAIEmbeddings()self.vectorstore = FAISS.load_local(self.load_vectorstore,embeddings =embeddings)print(' vectorstore loaded')self.qa = VectorDBQA.from_chain_type(llm =OpenAI(temperature=0, model_name="gpt-3.5-turbo"), chain_type = "stuff",vectorstore = self.vectorstore )4 unmodified linesdef download_repo_zip(self, link, output_folder = "main.zip"):username = link.split('/')[3]repo = link.split('/')[4]zip_url = f"https://github.com/{username}/{repo}/archive/refs/heads/master.zip"self.zip_url = zip_urlresponse = requests.get(zip_url)1 unmodified line#down load the zip filewith open('main.zip', 'wb') as f:f.write(response.content)# return the name of the extracted folder# return self.extract_zip("main.zip", output_folder)# return BytesIO(response.content)def extract_zip(self, zip_file, destination_folder):121 unmodified linesdef create_repo_pdf(self, repo_link, image_included = False, merged_pdf = "temp_merged.pdf"):self.merged_pdf_path = merged_pdfself.download_repo_zip(repo_link)folder_name = self.extract_zip('./main.zip', './')6 unmodified linesingnore_list.append('.bmp')ingnore_list.append('.tiff')print('folder_name: ', folder_name)pdf_files = []for root, dirs, files in os.walk(folder_name):for file in files:21 unmodified linesshutil.rmtree(folder_name)shutil.rmtree("temp")return self.merged_pdf_pathdef Answer_quetsion(self, question):return self.qa.run(question)def Answer_quetsion_with_source(self, question):return self.qa({"question": question}, return_only_outputs = True)def call_output(string = 'REPOGPT Initializing'):3 unmodified lines# filename = repogpt.get_pdf()return send_file(filename, as_attachment=True)repogpt = REPOGPT()with gr.Blocks() as demo:with gr.Row():gr.Markdown("<h3><center>REPOGPT</center></h3>")13 unmodified lineswith gr.Row():repo_link = gr.Textbox(placeholder="Paste your repo_link and press Enter ↵️",label = 'repo_link',show_label=True,lines=1,18 unmodified linesgr.Examples(examples=["Whats the name of this repo?","Whats this repo for?","How can I use this. Example code ? Step by step","how can I use this Experiment trackers ? Step by step","how can I Performing gradient accumulation with Accelerate? Step by step?","Make it like water-color painting","What is the background color","Describe this image","please detect the depth of this image","Can you use this depth image to generate a cute dog",],inputs=txt)
app.py+29-13