ubermenchh commited on
Commit
cab8144
1 Parent(s): 4053536

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_index.readers import TrafilaturaWebReader
2
+ from llama_index import VectorStoreIndex
3
+ from llama_index import ServiceContext
4
+ from langchain.llms import HuggingFaceHub
5
+ from llama_index.llms import LangChainLLM
6
+
7
+ repo_id = 'HuggingFaceH4/zephyr-7b-beta'
8
+
9
+ def loading_website(): return "Loading..."
10
+
11
+ def load_url(url):
12
+ documents = TrafilaturaWebReader().load_data([url])
13
+ llm = LangChainLLM(llm=HuggingFaceHub(repo_id=repo_id, model_kwargs={'temperature': 0.2, 'max_tokens': 4096, 'top_p': 0.95}))
14
+ service_context = ServiceContext.from_defaults(llm=llm, embed_model="local:BAAI/bge-small-en-v1.5")
15
+ index = VectorStoreIndex.from_documents(documents, service_context=service_context)
16
+ global query_engine
17
+ query_engine = index.as_query_engine()
18
+ return 'Ready'
19
+
20
+ # def chat(query):
21
+ # response = query_engine.query(query)
22
+ # return str(response)
23
+
24
+ def add_text(history, text):
25
+ history = history + [(text, None)]
26
+ return history, ''
27
+
28
+ def bot(history):
29
+ response = infer(history[-1][0])
30
+ history[-1][1] = response
31
+ return history
32
+
33
+ def infer(question):
34
+ response = query_engine.query(question)
35
+ return str(response)
36
+
37
+ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
38
+ with gr.Column():
39
+ chatbot = gr.Chatbot([], elem_id='chatbot')
40
+
41
+ with gr.Row():
42
+ paper_id = gr.Textbox(label='Web Address', placeholder='http://karpathy.github.io/2019/04/25/recipe/')
43
+ langchain_status = gr.Textbox(label='Status', placeholder='', interactive=False)
44
+ load_paper = gr.Button('Load Website')
45
+
46
+ with gr.Row():
47
+ question = gr.Textbox(label='Question', placeholder='Type your query...')
48
+ submit_btn = gr.Button('Submit')
49
+
50
+ load_paper.click(paper_changes, inputs=[paper_id], outputs=[langchain_status], queue=False)
51
+ question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
52
+ submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
53
+
54
+ demo.launch(share=True)