JorgeV20 commited on
Commit
9f3f2ba
·
verified ·
1 Parent(s): 799deb1

Upload 5 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ vectorstore/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,12 @@
1
- ---
2
- title: Flint FinanceBot
3
- emoji: 🐢
4
- colorFrom: indigo
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 4.40.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Flint FinanceBot
3
+ emoji: 🐢
4
+ colorFrom: indigo
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 4.40.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+
4
+ #chatbot
5
+ from langchain.llms import HuggingFacePipeline
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig, pipeline
7
+
8
+ from langchain.vectorstores import FAISS
9
+ from langchain.embeddings import HuggingFaceEmbeddings
10
+
11
+ from langchain.prompts import PromptTemplate
12
+ from langchain.chains import RetrievalQA
13
+
14
+ from textwrap import fill
15
+
16
+ DATA_PATH='data/'
17
+ DB_FAISS_PATH='vectorstore/db_faiss'
18
+
19
+ #Call of the model
20
+ model_name = "TheBloke/Llama-2-13b-Chat-GPTQ"
21
+
22
+ model = AutoModelForCausalLM.from_pretrained(model_name,
23
+ device_map="auto",
24
+ trust_remote_code=True)
25
+
26
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
27
+
28
+ gen_cfg = GenerationConfig.from_pretrained(model_name)
29
+ gen_cfg.max_new_tokens=512
30
+ gen_cfg.temperature=0.0000001 # 0.0
31
+ gen_cfg.return_full_text=True
32
+ gen_cfg.do_sample=True
33
+ gen_cfg.repetition_penalty=1.11
34
+
35
+ pipe=pipeline(
36
+ task="text-generation",
37
+ model=model,
38
+ tokenizer=tokenizer,
39
+ generation_config=gen_cfg
40
+ )
41
+
42
+
43
+ if gr.NO_RELOAD:
44
+ llm = HuggingFacePipeline(pipeline=pipe)
45
+ embeddings = HuggingFaceEmbeddings()
46
+ db = FAISS.load_local(DB_FAISS_PATH, embeddings)
47
+ print('todo ok')
48
+
49
+
50
+ #st.title('🦜🔗 Flint, your FinanceBot')
51
+ Description="""
52
+ ## Finance Bot: Get instant insights from Finance
53
+
54
+ This chatbot is built using the Retrieval-Augmented Generation (RAG) framework
55
+
56
+ """
57
+
58
+
59
+ #DB_FAISS_PATH = os.path.join(local_path, 'vectorstore_docs/db_faiss')
60
+
61
+ prompt_template = """Use the following pieces of information to answer the user's question.
62
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
63
+
64
+ Context: {context}
65
+ Question: {question}
66
+
67
+ Only return the helpful answer below and nothing else. Try to make it short. Maximum of 500 words.
68
+ Helpful answer:
69
+ """
70
+
71
+
72
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
73
+ Chain_pdf = RetrievalQA.from_chain_type(
74
+ llm=llm,
75
+ chain_type="stuff",
76
+ # retriever=db.as_retriever(search_type="similarity_score_threshold", search_kwargs={'k': 5, 'score_threshold': 0.8})
77
+ # Similarity Search is the default way to retrieve documents relevant to a query, but we can use MMR by setting search_type = "mmr"
78
+ # k defines how many documents are returned; defaults to 4.
79
+ # score_threshold allows to set a minimum relevance for documents returned by the retriever, if we are using the "similarity_score_threshold" search type.
80
+ # return_source_documents=True, # Optional parameter, returns the source documents used to answer the question
81
+ retriever=db.as_retriever(), # (search_kwargs={'k': 5, 'score_threshold': 0.8}),
82
+ chain_type_kwargs={"prompt": prompt},
83
+ )
84
+ #query = "When was the solar system formed?"
85
+ #result = Chain_pdf.invoke(query)
86
+ #print(fill(result['result'].strip(), width=100))
87
+
88
+
89
+ @spaces.GPU()
90
+ def final_result(query,history, Chain_pdf):
91
+ result = Chain_pdf.invoke(query)
92
+ print(fill(result['result'].strip(), width=100))
93
+ return result
94
+
95
+ with gr.Blocks() as demo:
96
+ system_prompt = gr.Textbox("You are helpful AI.", label="System Prompt")
97
+ slider = gr.Slider(10, 100, render=False)
98
+
99
+ gr.ChatInterface(
100
+ final_result, additional_inputs=[Chain_pdf]
101
+ )
102
+
103
+ demo.launch()
requirements.txt ADDED
Binary file (5.67 kB). View file
 
vectorstore/db_faiss/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:77fc19bf4803c3a8fc2f4a40f914431d612361d838464b3e6cb35bdc0b7c26a9
3
+ size 9008685
vectorstore/db_faiss/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:afe89076f2d8815f9bf4135cf61398589134f3454d964c5e99b672721c40d6fc
3
+ size 3155250