timep12345 commited on
Commit
ae87366
·
1 Parent(s): 971b34a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -4
app.py CHANGED
@@ -1,7 +1,97 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import json
4
 
5
+ from langchain.document_loaders import DataFrameLoader
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain.llms import HuggingFaceHub
8
+ from langchain.embeddings import HuggingFaceHubEmbeddings
9
+ from langchain.vectorstores import Chroma
10
+ from langchain.chains import RetrievalQA
11
+ from trafilatura import fetch_url, extract
12
+ from trafilatura.spider import focused_crawler
13
 
14
+
15
+
16
+ def loading_website():
17
+ return "Loading..."
18
+
19
+ def url_changes(url, pages_to_visit, urls_to_scrape, repo_id):
20
+ to_visit, links = focused_crawler(url, max_seen_urls=pages_to_visit, max_known_urls=urls_to_scrape)
21
+ print(f"{len(links)} to be crawled")
22
+
23
+ results_df = pd.DataFrame()
24
+ for url in links:
25
+ downloaded = fetch_url(url)
26
+ if downloaded:
27
+ result = extract(downloaded, output_format='json')
28
+ result = json.loads(result)
29
+
30
+ results_df = pd.concat([results_df, pd.DataFrame.from_records([result])])
31
+
32
+ loader = DataFrameLoader(results_df, page_content_column="text")
33
+ documents = loader.load()
34
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
35
+ texts = text_splitter.split_documents(documents)
36
+ embeddings = HuggingFaceHubEmbeddings()
37
+ db = Chroma.from_documents(texts, embeddings)
38
+ retriever = db.as_retriever()
39
+ llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0.1, "max_new_tokens":250})
40
+ global qa
41
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)
42
+ return "Ready"
43
+
44
+ def add_text(history, text):
45
+ history = history + [(text, None)]
46
+ return history, ""
47
+
48
+ def bot(history):
49
+ response = infer(history[-1][0])
50
+ history[-1][1] = response['result']
51
+ return history
52
+
53
+ def infer(question):
54
+
55
+ query = question
56
+ result = qa({"query": query})
57
+
58
+ return result
59
+
60
+ css="""
61
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
62
+ """
63
+
64
+ title = """
65
+ <div style="text-align: center;max-width: 700px;">
66
+ <h1>Chat with your website</h1>
67
+ <p style="text-align: center;">Enter target URL, click the "Load website to LangChain" button</p>
68
+ </div>
69
+ """
70
+
71
+
72
+ with gr.Blocks(css=css) as demo:
73
+ with gr.Column(elem_id="col-container"):
74
+ gr.HTML(title)
75
+
76
+ with gr.Column():
77
+ target_url = gr.Textbox(label="Load URL", placeholder="Enter target URL here. EX: https://www.penta.co.kr/")
78
+ #pdf_doc = gr.File(label="Load URL", file_types=['.pdf'], type="file")
79
+ 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")
80
+ with gr.Row():
81
+ langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
82
+ load_pdf = gr.Button("Load website to langchain")
83
+
84
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
85
+ question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
86
+ submit_btn = gr.Button("Send message")
87
+ #load_pdf.click(loading_pdf, None, langchain_status, queue=False)
88
+ repo_id.change(url_changes, inputs=[target_url, gr.Number(value=20, visible=False), gr.Number(value=200, visible=False), repo_id], outputs=[langchain_status], queue=False)
89
+ load_pdf.click(url_changes, inputs=[target_url, gr.Number(value=20, visible=False), gr.Number(value=200, visible=False), repo_id], outputs=[langchain_status], queue=False)
90
+ question.submit(add_text, [chatbot, question], [chatbot, question]).then(
91
+ bot, chatbot, chatbot
92
+ )
93
+ submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
94
+ bot, chatbot, chatbot
95
+ )
96
+
97
+ demo.launch()