Danielrahmai1991 commited on
Commit
0112605
·
verified ·
1 Parent(s): bfc59d0

Update app.py

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