Spaces:
Sleeping
Sleeping
Danielrahmai1991
commited on
Commit
•
f19ac67
1
Parent(s):
4eeec68
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,4 @@
|
|
1 |
-
# import gradio as gr
|
2 |
|
3 |
-
# from langchain_community.llms import LlamaCpp
|
4 |
-
# from langchain.prompts import PromptTemplate
|
5 |
-
# from langchain.chains import LLMChain
|
6 |
-
# from langchain_core.callbacks import StreamingStdOutCallbackHandler
|
7 |
-
# from langchain.retrievers import TFIDFRetriever
|
8 |
-
# from langchain.chains import RetrievalQA
|
9 |
-
# from langchain.memory import ConversationBufferMemory
|
10 |
-
# from langchain_community.chat_models import ChatLlamaCpp
|
11 |
-
|
12 |
-
|
13 |
-
# callbacks = [StreamingStdOutCallbackHandler()]
|
14 |
-
# print("creating ll started")
|
15 |
-
# llm = ChatLlamaCpp(
|
16 |
-
# model_path="finbro-v0.1.0-llama-3-8B-instruct-1m.gguf",
|
17 |
-
# n_batch=8,
|
18 |
-
# temperature=0.85,
|
19 |
-
# max_tokens=256,
|
20 |
-
# top_p=0.95,
|
21 |
-
# top_k = 10,
|
22 |
-
# callback_manager=callbacks,
|
23 |
-
# n_ctx=2048,
|
24 |
-
# verbose=True, # Verbose is required to pass to the callback manager
|
25 |
-
# )
|
26 |
-
# print("creating llm ended")
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
# def greet(question, model_type):
|
34 |
-
# print(f"question is {question}")
|
35 |
-
# out_gen = "testsetestestetsetsets"
|
36 |
-
# return out_gen
|
37 |
-
|
38 |
-
# demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|
39 |
-
# ["With memory", "Without memory"], label="Memory status", info="With using memory, the output will be slow but strong"
|
40 |
-
# ),], outputs="text")
|
41 |
-
# demo.launch(debug=True, share=True)
|
42 |
|
43 |
|
44 |
import gradio as gr
|
@@ -51,6 +11,11 @@ from langchain.retrievers import TFIDFRetriever
|
|
51 |
from langchain.chains import RetrievalQA
|
52 |
from langchain.memory import ConversationBufferMemory
|
53 |
from langchain_community.chat_models import ChatLlamaCpp
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
callbacks = [StreamingStdOutCallbackHandler()]
|
56 |
print("creating ll started")
|
@@ -66,34 +31,80 @@ llm = ChatLlamaCpp(
|
|
66 |
n_ctx=2048,
|
67 |
verbose=True, # Verbose is required to pass to the callback manager
|
68 |
)
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
def greet(question, model_type):
|
77 |
-
|
78 |
print(f"question is {question}")
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
"""
|
89 |
-
print("test1")
|
90 |
-
prompt = PromptTemplate(template=template, input_variables=["question"])
|
91 |
-
print("test2")
|
92 |
-
llm_chain_model = LLMChain(prompt=prompt, llm=llm)
|
93 |
-
print("test3")
|
94 |
-
out_gen = llm_chain_model.run(question)
|
95 |
-
print("test4")
|
96 |
-
print(f"out is: {out_gen}")
|
97 |
return out_gen
|
98 |
|
99 |
demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|
|
|
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
import gradio as gr
|
|
|
11 |
from langchain.chains import RetrievalQA
|
12 |
from langchain.memory import ConversationBufferMemory
|
13 |
from langchain_community.chat_models import ChatLlamaCpp
|
14 |
+
from langchain_core.output_parsers import StrOutputParser
|
15 |
+
from langchain_core.runnables import RunnablePassthrough
|
16 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
17 |
+
from langchain_community.vectorstores import Chroma
|
18 |
+
from langchain_core.prompts import PromptTemplate
|
19 |
|
20 |
callbacks = [StreamingStdOutCallbackHandler()]
|
21 |
print("creating ll started")
|
|
|
31 |
n_ctx=2048,
|
32 |
verbose=True, # Verbose is required to pass to the callback manager
|
33 |
)
|
34 |
+
print("creating ll ended")
|
35 |
+
|
36 |
+
# for without memory
|
37 |
+
template = """You are the Finiantial expert:
|
38 |
+
|
39 |
+
### Instruction:
|
40 |
+
{question}
|
41 |
+
|
42 |
+
### Input:
|
43 |
|
44 |
|
45 |
+
### Response:
|
46 |
+
"""
|
47 |
+
|
48 |
+
prompt = PromptTemplate(template=template, input_variables=["question"])
|
49 |
+
print("test2")
|
50 |
+
llm_chain_model = LLMChain(prompt=prompt, llm=llm)
|
51 |
+
|
52 |
+
# for retriver
|
53 |
+
|
54 |
+
|
55 |
+
def format_docs(docs):
|
56 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
57 |
+
|
58 |
+
model_name = "BAAI/bge-base-en-v1.5"
|
59 |
+
model_kwargs = {"device":'cpu'}
|
60 |
+
encode_kwargs = {'normalize_embeddings':True}
|
61 |
+
|
62 |
+
hf = HuggingFaceEmbeddings(
|
63 |
+
model_name = model_name,
|
64 |
+
model_kwargs = model_kwargs,
|
65 |
+
encode_kwargs = encode_kwargs
|
66 |
+
)
|
67 |
+
|
68 |
+
|
69 |
+
vectorstore = Chroma(
|
70 |
+
collection_name="example_collection",
|
71 |
+
embedding_function=hf,
|
72 |
+
persist_directory="./chroma_langchain_db", # Where to save data locally, remove if not neccesary
|
73 |
+
)
|
74 |
+
|
75 |
+
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 6})
|
76 |
+
template = """you are the financial ai assistant
|
77 |
+
|
78 |
+
{context}
|
79 |
+
|
80 |
+
Question: {question}
|
81 |
+
|
82 |
+
Helpful Answer:"""
|
83 |
+
custom_rag_prompt = PromptTemplate.from_template(template)
|
84 |
+
|
85 |
+
rag_chain = (
|
86 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
87 |
+
| custom_rag_prompt
|
88 |
+
| llm
|
89 |
+
| StrOutputParser()
|
90 |
+
)
|
91 |
+
print("retriver done")
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
def greet(question, model_type):
|
97 |
+
|
98 |
print(f"question is {question}")
|
99 |
+
if model_type == "With memory":
|
100 |
+
out_gen = rag_chain.invoke("give me suggestion for investment")
|
101 |
+
print("test5")
|
102 |
+
print(f"out is: {out_gen}")
|
103 |
+
else:
|
104 |
+
print("test3")
|
105 |
+
out_gen = llm_chain_model.run(question)
|
106 |
+
print("test4")
|
107 |
+
print(f"out is: {out_gen}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
return out_gen
|
109 |
|
110 |
demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|