Danielrahmai1991 commited on
Commit
9eb3567
1 Parent(s): 146db4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -113
app.py CHANGED
@@ -1,94 +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
- # 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
- # 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
@@ -100,11 +9,11 @@ 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,
@@ -114,7 +23,7 @@ llm = ChatLlamaCpp(
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
 
@@ -122,29 +31,120 @@ llm = ChatLlamaCpp(
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)