Danielrahmai1991 commited on
Commit
eb306a5
·
verified ·
1 Parent(s): 4c7c561

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py CHANGED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_community.llms import LlamaCpp
3
+ from langchain.prompts import PromptTemplate
4
+ from langchain.chains import LLMChain
5
+ from langchain_core.callbacks import StreamingStdOutCallbackHandler
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.memory import ConversationBufferMemory
8
+ from langchain import PromptTemplate
9
+ from langchain.retrievers import TFIDFRetriever
10
+
11
+ callbacks = [StreamingStdOutCallbackHandler()]
12
+ print("creating llm started")
13
+ llm = LlamaCpp(
14
+ model_path='unsloth.Q5_K_M.gguf',
15
+ temperature=0.75,
16
+ max_tokens=30,
17
+ top_p=4,
18
+ callback_manager=callbacks,
19
+ verbose=True, # Verbose is required to pass to the callback manager
20
+ )
21
+ print("creating llm ended")
22
+
23
+ def with_memory(llm):
24
+ retriever = TFIDFRetriever.from_texts(
25
+ ["Finatial AI"])
26
+
27
+
28
+ template = """You are the Finiantial expert:
29
+ {history}
30
+ {context}
31
+ ### Instruction:
32
+ {question}
33
+
34
+ ### Input:
35
+
36
+
37
+ ### Response:
38
+ """
39
+
40
+ prompt1 = PromptTemplate(
41
+ input_variables=["history", "context", "question"],
42
+ template=template,
43
+ )
44
+
45
+ qa = RetrievalQA.from_chain_type(
46
+ llm=llm,
47
+ chain_type='stuff',
48
+ retriever=retriever,
49
+ verbose=False,
50
+ chain_type_kwargs={
51
+ "verbose": False,
52
+ "prompt": prompt1,
53
+ "memory": ConversationBufferMemory(
54
+ memory_key="history",
55
+ input_key="question"),
56
+ }
57
+ )
58
+ return qa
59
+
60
+ def without_memory(llm):
61
+ template = """You are the Finiantial expert:
62
+ ### Instruction:
63
+ {question}
64
+ ### Input:
65
+ ### Response:
66
+ """
67
+
68
+ prompt = PromptTemplate(template=template, input_variables=["question"])
69
+
70
+ llm_chain_model = LLMChain(prompt=prompt, llm=llm)
71
+ print("creating model created")
72
+ return llm_chain_model
73
+
74
+
75
+ def main():
76
+
77
+ """Build a streamlit layout"""
78
+ # Wide mode
79
+ st.set_page_config(layout="wide")
80
+
81
+ llm_models = {
82
+
83
+ "Base": "unsloth.Q5_K_M.gguf",
84
+ "Cerebras": "cerebras_Llama3-DocChat-1.0-8B_Base_adapt_basic_model_16bit.gguf",
85
+ "Bavest": "bavest_fin_llama_33b_adapt_basic_model_16bit.gguf",
86
+ "Aliyasir": "aliyasir_Llama-3-8B-Instruct-Finance-RAG_adapt_basic_model_16bit.gguf",
87
+ "Basic Adapt": "adapt-unsloth.Q5_K_M.gguf",
88
+ "adapt llm": "AdaptLLM_finance-LLM-13B_adapt_basic_model_16bit.gguf",
89
+ "Fibro" : "finbro-v0.1.0-llama-3-8B-instruct-1m.gguf",
90
+ }
91
+ # Designing the interface
92
+ st.title("Financial LLM test")
93
+ # For newline
94
+ st.write("\n")
95
+ # Instructions
96
+ st.markdown("*Hint: you can select the LLM model and write your prompt")
97
+ # Set the columns
98
+
99
+ col1, col2 = st.columns(2)
100
+ col1.subheader("Prompt Section")
101
+ col2.subheader("Model Output")
102
+
103
+ llm_qa = without_memory(llm)
104
+ # Model selection
105
+ st.sidebar.title("Model selection")
106
+ det_arch = st.sidebar.selectbox("LLM model", ['With Memory', 'Without Memory'])
107
+
108
+ # For newline
109
+ st.sidebar.write("\n")
110
+
111
+
112
+ if st.sidebar.button("Select LLM"):
113
+ with st.spinner("Loading model..."):
114
+ if det_arch == 'Without Memory':
115
+ llm_qa = without_memory(llm)
116
+ else:
117
+ llm_qa = with_memory(llm)
118
+ # llm_qa = get_model(llm_models.get(det_arch))
119
+ # load the model TODO
120
+
121
+ text_input = ''
122
+ with col1:
123
+ text_input_temp = st.text_input(
124
+ "Please, type your question and submit.",
125
+ "Write Your Prompt",
126
+ key="placeholder",
127
+ )
128
+
129
+ if st.button("Submit"):
130
+ text_input = text_input_temp
131
+
132
+ with col2:
133
+ if text_input != '':
134
+ with st.spinner("Analyzing..."):
135
+ out_gen = llm_qa.run(question)
136
+ st.write("LLM Response: ", out_gen)
137
+ text_input = ''
138
+
139
+
140
+
141
+ if __name__ == "__main__":
142
+ main()