Spaces:
Sleeping
Sleeping
Danielrahmai1991
commited on
Commit
•
146db4f
1
Parent(s):
dfa17d9
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,3 @@
|
|
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 |
-
|
11 |
-
|
12 |
-
callbacks = [StreamingStdOutCallbackHandler()]
|
13 |
-
print("creating ll started")
|
14 |
-
llm = LlamaCpp(
|
15 |
-
model_path="finbrov1.gguf",
|
16 |
-
temperature=0.75,
|
17 |
-
max_tokens=100,
|
18 |
-
top_p=4,
|
19 |
-
callback_manager=callbacks,
|
20 |
-
verbose=True, # Verbose is required to pass to the callback manager
|
21 |
-
)
|
22 |
-
print("creating ll ended")
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
def greet(question, model_type):
|
30 |
-
print(f"question is {question}")
|
31 |
-
if model_type == "With memory":
|
32 |
-
retriever = TFIDFRetriever.from_texts(
|
33 |
-
["Finatial AI"])
|
34 |
-
|
35 |
-
|
36 |
-
template = """You are the Finiantial expert:
|
37 |
-
{history}
|
38 |
-
{context}
|
39 |
-
### Instruction:
|
40 |
-
{question}
|
41 |
-
|
42 |
-
### Input:
|
43 |
-
|
44 |
-
|
45 |
-
### Response:
|
46 |
-
"""
|
47 |
-
|
48 |
-
prompt1 = PromptTemplate(
|
49 |
-
input_variables=["history", "context", "question"],
|
50 |
-
template=template,
|
51 |
-
)
|
52 |
-
|
53 |
-
llm_chain_model = RetrievalQA.from_chain_type(
|
54 |
-
llm=llm,
|
55 |
-
chain_type='stuff',
|
56 |
-
retriever=retriever,
|
57 |
-
verbose=False,
|
58 |
-
chain_type_kwargs={
|
59 |
-
"verbose": False,
|
60 |
-
"prompt": prompt1,
|
61 |
-
"memory": ConversationBufferMemory(
|
62 |
-
memory_key="history",
|
63 |
-
input_key="question"),
|
64 |
-
}
|
65 |
-
)
|
66 |
-
print("creating model created")
|
67 |
-
else:
|
68 |
-
template = """You are the Finiantial expert:
|
69 |
-
### Instruction:
|
70 |
-
{question}
|
71 |
-
### Input:
|
72 |
-
### Response:
|
73 |
-
"""
|
74 |
-
|
75 |
-
prompt = PromptTemplate(template=template, input_variables=["question"])
|
76 |
-
|
77 |
-
llm_chain_model = LLMChain(prompt=prompt, llm=llm)
|
78 |
-
out_gen = llm_chain_model.run(question)
|
79 |
-
print(f"out is: {out_gen}")
|
80 |
-
return out_gen
|
81 |
-
|
82 |
-
demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|
83 |
-
["With memory", "Without memory"], label="Memory status", info="With using memory, the output will be slow but strong"
|
84 |
-
),], outputs="text")
|
85 |
-
demo.launch(debug=True, share=True)
|
86 |
-
|
87 |
-
|
88 |
# import gradio as gr
|
89 |
|
90 |
# from langchain_community.llms import LlamaCpp
|
@@ -96,11 +9,11 @@ demo.launch(debug=True, share=True)
|
|
96 |
# from langchain.memory import ConversationBufferMemory
|
97 |
# from langchain_community.chat_models import ChatLlamaCpp
|
98 |
|
|
|
99 |
# callbacks = [StreamingStdOutCallbackHandler()]
|
100 |
# print("creating ll started")
|
101 |
-
#
|
102 |
-
#
|
103 |
-
# model_path=M_NAME,
|
104 |
# n_batch=8,
|
105 |
# temperature=0.85,
|
106 |
# max_tokens=256,
|
@@ -110,7 +23,7 @@ demo.launch(debug=True, share=True)
|
|
110 |
# n_ctx=2048,
|
111 |
# verbose=True, # Verbose is required to pass to the callback manager
|
112 |
# )
|
113 |
-
#
|
114 |
|
115 |
|
116 |
|
@@ -118,29 +31,120 @@ demo.launch(debug=True, share=True)
|
|
118 |
|
119 |
|
120 |
# def greet(question, model_type):
|
121 |
-
# print("prompt started ")
|
122 |
# print(f"question is {question}")
|
123 |
-
#
|
124 |
-
|
125 |
-
#
|
126 |
-
|
127 |
-
|
128 |
-
#
|
129 |
-
|
130 |
-
|
131 |
-
#
|
132 |
-
#
|
133 |
-
|
134 |
-
#
|
135 |
-
|
136 |
-
|
137 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
# out_gen = llm_chain_model.run(question)
|
139 |
-
# print("test4")
|
140 |
# print(f"out is: {out_gen}")
|
141 |
# return out_gen
|
142 |
|
143 |
# demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|
144 |
-
# ["
|
145 |
# ),], outputs="text")
|
146 |
-
# demo.launch(debug=True, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# import gradio as gr
|
2 |
|
3 |
# from langchain_community.llms import LlamaCpp
|
|
|
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,
|
|
|
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 |
|
|
|
31 |
|
32 |
|
33 |
# def greet(question, model_type):
|
|
|
34 |
# print(f"question is {question}")
|
35 |
+
# if model_type == "With memory":
|
36 |
+
# retriever = TFIDFRetriever.from_texts(
|
37 |
+
# ["Finatial AI"])
|
38 |
+
|
39 |
+
|
40 |
+
# template = """You are the Finiantial expert:
|
41 |
+
# {history}
|
42 |
+
# {context}
|
43 |
+
# ### Instruction:
|
44 |
+
# {question}
|
45 |
+
|
46 |
+
# ### Input:
|
47 |
+
|
48 |
+
|
49 |
+
# ### Response:
|
50 |
+
# """
|
51 |
+
|
52 |
+
# prompt1 = PromptTemplate(
|
53 |
+
# input_variables=["history", "context", "question"],
|
54 |
+
# template=template,
|
55 |
+
# )
|
56 |
+
|
57 |
+
# llm_chain_model = RetrievalQA.from_chain_type(
|
58 |
+
# llm=llm,
|
59 |
+
# chain_type='stuff',
|
60 |
+
# retriever=retriever,
|
61 |
+
# verbose=False,
|
62 |
+
# chain_type_kwargs={
|
63 |
+
# "verbose": False,
|
64 |
+
# "prompt": prompt1,
|
65 |
+
# "memory": ConversationBufferMemory(
|
66 |
+
# memory_key="history",
|
67 |
+
# input_key="question"),
|
68 |
+
# }
|
69 |
+
# )
|
70 |
+
# print("creating model created")
|
71 |
+
# else:
|
72 |
+
# template = """You are the Finiantial expert:
|
73 |
+
# ### Instruction:
|
74 |
+
# {question}
|
75 |
+
# ### Input:
|
76 |
+
# ### Response:
|
77 |
+
# """
|
78 |
+
|
79 |
+
# prompt = PromptTemplate(template=template, input_variables=["question"])
|
80 |
+
|
81 |
+
# llm_chain_model = LLMChain(prompt=prompt, llm=llm)
|
82 |
# out_gen = llm_chain_model.run(question)
|
|
|
83 |
# print(f"out is: {out_gen}")
|
84 |
# return out_gen
|
85 |
|
86 |
# demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|
87 |
+
# ["With memory", "Without memory"], label="Memory status", info="With using memory, the output will be slow but strong"
|
88 |
# ),], outputs="text")
|
89 |
+
# demo.launch(debug=True, share=True)
|
90 |
+
|
91 |
+
|
92 |
+
import gradio as gr
|
93 |
+
|
94 |
+
from langchain_community.llms import LlamaCpp
|
95 |
+
from langchain.prompts import PromptTemplate
|
96 |
+
from langchain.chains import LLMChain
|
97 |
+
from langchain_core.callbacks import StreamingStdOutCallbackHandler
|
98 |
+
from langchain.retrievers import TFIDFRetriever
|
99 |
+
from langchain.chains import RetrievalQA
|
100 |
+
from langchain.memory import ConversationBufferMemory
|
101 |
+
from langchain_community.chat_models import ChatLlamaCpp
|
102 |
+
|
103 |
+
callbacks = [StreamingStdOutCallbackHandler()]
|
104 |
+
print("creating ll started")
|
105 |
+
M_NAME = "finbro-v0.1.0-llama-3-8B-instruct-1m.gguf"
|
106 |
+
llm = ChatLlamaCpp(
|
107 |
+
model_path=M_NAME,
|
108 |
+
n_batch=8,
|
109 |
+
temperature=0.85,
|
110 |
+
max_tokens=256,
|
111 |
+
top_p=0.95,
|
112 |
+
top_k = 10,
|
113 |
+
callback_manager=callbacks,
|
114 |
+
n_ctx=2048,
|
115 |
+
verbose=True, # Verbose is required to pass to the callback manager
|
116 |
+
)
|
117 |
+
# print("creating ll ended")
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
def greet(question, model_type):
|
125 |
+
print("prompt started ")
|
126 |
+
print(f"question is {question}")
|
127 |
+
template = """You are the Finiantial expert:
|
128 |
+
|
129 |
+
### Instruction:
|
130 |
+
{question}
|
131 |
+
|
132 |
+
### Input:
|
133 |
+
|
134 |
+
|
135 |
+
### Response:
|
136 |
+
"""
|
137 |
+
print("test1")
|
138 |
+
prompt = PromptTemplate(template=template, input_variables=["question"])
|
139 |
+
print("test2")
|
140 |
+
llm_chain_model = LLMChain(prompt=prompt, llm=llm)
|
141 |
+
print("test3")
|
142 |
+
out_gen = llm_chain_model.run(question)
|
143 |
+
print("test4")
|
144 |
+
print(f"out is: {out_gen}")
|
145 |
+
return out_gen
|
146 |
+
|
147 |
+
demo = gr.Interface(fn=greet, inputs=["text", gr.Dropdown(
|
148 |
+
["Without memory", "With memory"], label="Memory status", info="With using memory, the output will be slow but strong"
|
149 |
+
),], outputs="text")
|
150 |
+
demo.launch(debug=True, share=True)
|