import gradio as gr import pandas as pd import json from langchain.document_loaders import DataFrameLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings from langchain.vectorstores import Chroma from langchain.chains import RetrievalQA from langchain import HuggingFacePipeline from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline from trafilatura import fetch_url, extract from trafilatura.spider import focused_crawler from trafilatura.settings import use_config def loading_website(): return "Loading..." def url_changes(url, pages_to_visit, urls_to_scrape, repo_id): to_visit, links = focused_crawler(url, max_seen_urls=pages_to_visit, max_known_urls=urls_to_scrape) print(f"{len(links)} to be crawled") config = use_config() config.set("DEFAULT", "EXTRACTION_TIMEOUT", "0") results_df = pd.DataFrame() for url in links: downloaded = fetch_url(url) if downloaded: result = extract(downloaded, output_format='json', config=config) result = json.loads(result) results_df = pd.concat([results_df, pd.DataFrame.from_records([result])]) results_df.to_csv("./data.csv") df = pd.read_csv("./data.csv") loader = DataFrameLoader(df, page_content_column="text") documents = loader.load() print(f"{len(documents)} documents loaded") text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) texts = text_splitter.split_documents(documents) print(f"documents splitted into {len(texts)} chunks") embeddings = SentenceTransformerEmbeddings(model_name="jhgan/ko-sroberta-multitask") persist_directory = './vector_db' db = Chroma.from_documents(texts, embeddings, persist_directory=persist_directory) retriever = db.as_retriever() MODEL = 'beomi/KoAlpaca-Polyglot-5.8B' model = AutoModelForCausalLM.from_pretrained( MODEL, torch_dtype="auto", ) model.eval() pipe = pipeline( 'text-generation', model=model, tokenizer=MODEL, max_length=512, temperature=0, top_p=0.95, repetition_penalty=1.15 ) llm = HuggingFacePipeline(pipeline=pipe) global qa qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True) return "Ready" def add_text(history, text): history = history + [(text, None)] return history, "" def bot(history): response = infer(history[-1][0]) history[-1][1] = response['result'] return history def infer(question): query = question result = qa({"query": query}) return result css=""" #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} """ title = """
Enter target URL, click the "Load website to LangChain" button