Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pandas as pd | |
import json | |
from langchain.document_loaders import DataFrameLoader | |
from langchain.text_splitter import CharacterTextSplitter | |
from langchain.llms import HuggingFaceHub | |
from langchain.embeddings import HuggingFaceHubEmbeddings | |
from langchain.vectorstores import Chroma | |
from langchain.chains import RetrievalQA | |
from trafilatura import fetch_url, extract | |
from trafilatura.spider import focused_crawler | |
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") | |
results_df = pd.DataFrame() | |
for url in links: | |
downloaded = fetch_url(url) | |
if downloaded: | |
result = extract(downloaded, output_format='json') | |
result = json.loads(result) | |
results_df = pd.concat([results_df, pd.DataFrame.from_records([result])]) | |
loader = DataFrameLoader(results_df, page_content_column="text") | |
documents = loader.load() | |
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200) | |
texts = text_splitter.split_documents(documents) | |
embeddings = HuggingFaceHubEmbeddings() | |
db = Chroma.from_documents(texts, embeddings) | |
retriever = db.as_retriever() | |
llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0.1, "max_new_tokens":250}) | |
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 = """ | |
<div style="text-align: center;max-width: 700px;"> | |
<h1>Chat with your website</h1> | |
<p style="text-align: center;">Enter target URL, click the "Load website to LangChain" button</p> | |
</div> | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Column(elem_id="col-container"): | |
gr.HTML(title) | |
with gr.Column(): | |
target_url = gr.Textbox(label="Load URL", placeholder="Enter target URL here. EX: https://www.penta.co.kr/") | |
#pdf_doc = gr.File(label="Load URL", file_types=['.pdf'], type="file") | |
repo_id = gr.Dropdown(label="LLM", choices=["google/flan-ul2", "OpenAssistant/oasst-sft-1-pythia-12b", "beomi/KoAlpaca-Polyglot-12.8B"], value="google/flan-ul2") | |
with gr.Row(): | |
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False) | |
load_pdf = gr.Button("Load website to langchain") | |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350) | |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ") | |
submit_btn = gr.Button("Send message") | |
#load_pdf.click(loading_pdf, None, langchain_status, queue=False) | |
repo_id.change(url_changes, inputs=[target_url, gr.Number(value=5, visible=False), gr.Number(value=50, visible=False), repo_id], outputs=[langchain_status], queue=False) | |
load_pdf.click(url_changes, inputs=[target_url, gr.Number(value=5, visible=False), gr.Number(value=50, visible=False), repo_id], outputs=[langchain_status], queue=False) | |
question.submit(add_text, [chatbot, question], [chatbot, question]).then( | |
bot, chatbot, chatbot | |
) | |
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then( | |
bot, chatbot, chatbot | |
) | |
demo.launch() |