thejagstudio commited on
Commit
c6709ba
1 Parent(s): ed64697

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.callbacks.manager import CallbackManager
3
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
4
+ from langchain.prompts import PromptTemplate
5
+ from langchain_community.llms import LlamaCpp
6
+ from langchain_core.runnables import RunnablePassthrough
7
+ from langchain_core.output_parsers import StrOutputParser
8
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
9
+ from langchain_community.document_loaders import WebBaseLoader
10
+ from langchain_huggingface.embeddings import HuggingFaceEmbeddings
11
+ from langchain_community.vectorstores import Chroma
12
+ from langchain import hub
13
+
14
+ # Set up callback manager and model parameters
15
+ callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
16
+ n_gpu_layers = 0
17
+ n_batch = 512
18
+
19
+ llm = LlamaCpp(
20
+ model_path="./models/phi-2.Q2_K.gguf",
21
+ n_gpu_layers=n_gpu_layers, n_batch=n_batch,
22
+ n_ctx = 4096,
23
+ temperature=0.7,
24
+ max_tokens=4096,
25
+ top_p=1,
26
+ callback_manager=callback_manager,
27
+ verbose=False,
28
+ )
29
+
30
+ # Load the prompt
31
+ prompt = hub.pull("rlm/rag-prompt")
32
+
33
+ # Function to format documents
34
+ def format_docs(docs):
35
+ return "\n\n".join(doc.page_content for doc in docs)
36
+
37
+ # Main function to process the question and URL
38
+ def get_answer(question, url):
39
+ # Load data from the provided URL
40
+ loader = WebBaseLoader(url)
41
+ data = loader.load()
42
+
43
+ # Split the data into small chunks
44
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
45
+ all_splits = text_splitter.split_documents(data)
46
+
47
+ # Store the data in Vector Store
48
+ vectorstore = Chroma.from_documents(documents=all_splits, embedding=HuggingFaceEmbeddings())
49
+ retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 3})
50
+
51
+ retrieved_docs = retriever.invoke(question)
52
+
53
+ rag_chain = (
54
+ {"context": retriever | format_docs, "question": RunnablePassthrough()}
55
+ | prompt
56
+ | llm
57
+ | StrOutputParser()
58
+ )
59
+
60
+ answer = ""
61
+ for chunk in rag_chain.stream(question):
62
+ answer += chunk
63
+ yield answer
64
+
65
+ yield answer
66
+
67
+ # Create the Gradio interface
68
+ iface = gr.Interface(
69
+ fn=get_answer,
70
+ inputs=[gr.Textbox(lines=1, placeholder="Enter your question here..."),
71
+ gr.Textbox(lines=1, placeholder="Enter the website URL here...")],
72
+ outputs="text",
73
+ title="Web-based Question Answering System",
74
+ description="Ask a question about the content of a webpage and get an answer.",
75
+ examples=[
76
+ ["Which are the top 5 companies in the world with their revenue in table format?", "https://www.investopedia.com/biggest-companies-in-the-world-by-market-cap-5212784"]
77
+ ]
78
+ )
79
+
80
+ # Launch the app
81
+ iface.launch(share=True)