Spaces:
Sleeping
Sleeping
Danielrahmai1991
commited on
Commit
•
f28abbd
1
Parent(s):
645e0da
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
91 |
+
# from langchain.prompts import PromptTemplate
|
92 |
+
# from langchain.chains import LLMChain
|
93 |
+
# from langchain_core.callbacks import StreamingStdOutCallbackHandler
|
94 |
+
# from langchain.retrievers import TFIDFRetriever
|
95 |
+
# from langchain.chains import RetrievalQA
|
96 |
+
# from langchain.memory import ConversationBufferMemory
|
97 |
+
# from langchain_community.chat_models import ChatLlamaCpp
|
98 |
+
|
99 |
+
# callbacks = [StreamingStdOutCallbackHandler()]
|
100 |
+
# print("creating ll started")
|
101 |
+
# M_NAME = "taddeusb90_finbro-v0.1.0-dolphin-2.9-llama-3-8B-instruct-131k_adapt_basic_model_16bit.gguf"
|
102 |
+
# llm = LlamaCpp(
|
103 |
+
# model_path=M_NAME,
|
104 |
+
# n_batch=8,
|
105 |
+
# temperature=0.85,
|
106 |
+
# max_tokens=256,
|
107 |
+
# top_p=0.95,
|
108 |
+
# top_k = 10,
|
109 |
+
# callback_manager=callbacks,
|
110 |
+
# n_ctx=2048,
|
111 |
+
# verbose=True, # Verbose is required to pass to the callback manager
|
112 |
+
# )
|
113 |
+
# # print("creating ll ended")
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
# def greet(question, model_type):
|
121 |
+
# print("prompt started ")
|
122 |
+
# print(f"question is {question}")
|
123 |
+
# template = """You are the Finiantial expert:
|
124 |
+
|
125 |
+
# ### Instruction:
|
126 |
+
# {question}
|
127 |
+
|
128 |
+
# ### Input:
|
129 |
+
|
130 |
+
|
131 |
+
# ### Response:
|
132 |
+
# """
|
133 |
+
# print("test1")
|
134 |
+
# prompt = PromptTemplate(template=template, input_variables=["question"])
|
135 |
+
# print("test2")
|
136 |
+
# llm_chain_model = LLMChain(prompt=prompt, llm=llm)
|
137 |
+
# print("test3")
|
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 |
+
# ["Without memory", "With memory"], label="Memory status", info="With using memory, the output will be slow but strong"
|
145 |
+
# ),], outputs="text")
|
146 |
+
# demo.launch(debug=True, share=True)
|