Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="aliyasir_Llama-3-8B-Instruct-Finance-RAG_adapt_basic_model_16bit.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)
|