Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- .ipynb_checkpoints/app-checkpoint.py +99 -0
- .ipynb_checkpoints/requirements-checkpoint.txt +13 -0
- README.md +1 -7
- app.py +99 -0
- requirements.txt +13 -0
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import shutil
|
2 |
+
import requests
|
3 |
+
import sys
|
4 |
+
from typing import Optional, List, Tuple
|
5 |
+
import json
|
6 |
+
from langchain_community.llms import HuggingFaceHub
|
7 |
+
|
8 |
+
|
9 |
+
##Loading the Model to answer questions
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
12 |
+
from peft import PeftModel, PeftConfig
|
13 |
+
|
14 |
+
|
15 |
+
peft_model_id = "Ubaidbhat/zephr_finance_finetuned"
|
16 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
17 |
+
print(config.base_model_name_or_path)
|
18 |
+
bnb_config = BitsAndBytesConfig(
|
19 |
+
load_in_4bit = True,
|
20 |
+
bnb_4bit_use_double_quant=True,
|
21 |
+
bnb_4bit_quant_type="nf4",
|
22 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
23 |
+
)
|
24 |
+
|
25 |
+
d_map = {"": torch.cuda.current_device()} if torch.cuda.is_available() else None
|
26 |
+
|
27 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, quantization_config=bnb_config, device_map=d_map)
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
29 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
30 |
+
model = model.merge_and_unload()
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
##Creating base Model Chain
|
37 |
+
from langchain.llms import HuggingFacePipeline
|
38 |
+
from langchain.prompts import PromptTemplate
|
39 |
+
from transformers import pipeline
|
40 |
+
from langchain_core.output_parsers import StrOutputParser
|
41 |
+
from langchain.chains import LLMChain
|
42 |
+
|
43 |
+
text_generation_pipeline = pipeline(
|
44 |
+
model=model,
|
45 |
+
tokenizer=tokenizer,
|
46 |
+
task="text-generation",
|
47 |
+
temperature=0.2,
|
48 |
+
do_sample=True,
|
49 |
+
repetition_penalty=1.1,
|
50 |
+
return_full_text=True,
|
51 |
+
max_new_tokens=400,
|
52 |
+
pad_token_id=tokenizer.eos_token_id,
|
53 |
+
)
|
54 |
+
|
55 |
+
llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
|
56 |
+
|
57 |
+
prompt_template = """
|
58 |
+
<|system|>
|
59 |
+
Answer the question based on your knowledge.
|
60 |
+
</s>
|
61 |
+
<|user|>
|
62 |
+
{question}
|
63 |
+
</s>
|
64 |
+
<|assistant|>
|
65 |
+
"""
|
66 |
+
|
67 |
+
prompt = PromptTemplate(
|
68 |
+
input_variables=["question"],
|
69 |
+
template=prompt_template,
|
70 |
+
)
|
71 |
+
|
72 |
+
llm_chain = prompt | llm | StrOutputParser()
|
73 |
+
|
74 |
+
def inference(question):
|
75 |
+
llmAnswer = llm_chain.invoke({"question": question})
|
76 |
+
llmAnswer = llmAnswer.rstrip()
|
77 |
+
return llmAnswer
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
import gradio as gr
|
82 |
+
from langchain_core.runnables import RunnablePassthrough
|
83 |
+
|
84 |
+
def predict(question):
|
85 |
+
return inference(question)
|
86 |
+
|
87 |
+
pred = gr.Interface(
|
88 |
+
fn=predict,
|
89 |
+
inputs=[
|
90 |
+
gr.Textbox(label="Question"),
|
91 |
+
],
|
92 |
+
outputs="text",
|
93 |
+
title="Finetuned Zephr Model in the Finance Domain."
|
94 |
+
)
|
95 |
+
|
96 |
+
pred.launch(share=True)
|
97 |
+
|
98 |
+
|
99 |
+
|
.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
bitsandbytes
|
3 |
+
transformers
|
4 |
+
peft
|
5 |
+
accelerate
|
6 |
+
faiss-gpu
|
7 |
+
datasets
|
8 |
+
trl
|
9 |
+
gradio
|
10 |
+
langchain
|
11 |
+
sentence-transformers
|
12 |
+
pandas
|
13 |
+
langchain-community
|
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
title: FinanceTuned
|
3 |
-
|
4 |
-
colorFrom: pink
|
5 |
-
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.21.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: FinanceTuned
|
3 |
+
app_file: app.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.21.0
|
|
|
|
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import shutil
|
2 |
+
import requests
|
3 |
+
import sys
|
4 |
+
from typing import Optional, List, Tuple
|
5 |
+
import json
|
6 |
+
from langchain_community.llms import HuggingFaceHub
|
7 |
+
|
8 |
+
|
9 |
+
##Loading the Model to answer questions
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
12 |
+
from peft import PeftModel, PeftConfig
|
13 |
+
|
14 |
+
|
15 |
+
peft_model_id = "Ubaidbhat/zephr_finance_finetuned"
|
16 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
17 |
+
print(config.base_model_name_or_path)
|
18 |
+
bnb_config = BitsAndBytesConfig(
|
19 |
+
load_in_4bit = True,
|
20 |
+
bnb_4bit_use_double_quant=True,
|
21 |
+
bnb_4bit_quant_type="nf4",
|
22 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
23 |
+
)
|
24 |
+
|
25 |
+
d_map = {"": torch.cuda.current_device()} if torch.cuda.is_available() else None
|
26 |
+
|
27 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, quantization_config=bnb_config, device_map=d_map)
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
29 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
30 |
+
model = model.merge_and_unload()
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
##Creating base Model Chain
|
37 |
+
from langchain.llms import HuggingFacePipeline
|
38 |
+
from langchain.prompts import PromptTemplate
|
39 |
+
from transformers import pipeline
|
40 |
+
from langchain_core.output_parsers import StrOutputParser
|
41 |
+
from langchain.chains import LLMChain
|
42 |
+
|
43 |
+
text_generation_pipeline = pipeline(
|
44 |
+
model=model,
|
45 |
+
tokenizer=tokenizer,
|
46 |
+
task="text-generation",
|
47 |
+
temperature=0.2,
|
48 |
+
do_sample=True,
|
49 |
+
repetition_penalty=1.1,
|
50 |
+
return_full_text=True,
|
51 |
+
max_new_tokens=400,
|
52 |
+
pad_token_id=tokenizer.eos_token_id,
|
53 |
+
)
|
54 |
+
|
55 |
+
llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
|
56 |
+
|
57 |
+
prompt_template = """
|
58 |
+
<|system|>
|
59 |
+
Answer the question based on your knowledge.
|
60 |
+
</s>
|
61 |
+
<|user|>
|
62 |
+
{question}
|
63 |
+
</s>
|
64 |
+
<|assistant|>
|
65 |
+
"""
|
66 |
+
|
67 |
+
prompt = PromptTemplate(
|
68 |
+
input_variables=["question"],
|
69 |
+
template=prompt_template,
|
70 |
+
)
|
71 |
+
|
72 |
+
llm_chain = prompt | llm | StrOutputParser()
|
73 |
+
|
74 |
+
def inference(question):
|
75 |
+
llmAnswer = llm_chain.invoke({"question": question})
|
76 |
+
llmAnswer = llmAnswer.rstrip()
|
77 |
+
return llmAnswer
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
import gradio as gr
|
82 |
+
from langchain_core.runnables import RunnablePassthrough
|
83 |
+
|
84 |
+
def predict(question):
|
85 |
+
return inference(question)
|
86 |
+
|
87 |
+
pred = gr.Interface(
|
88 |
+
fn=predict,
|
89 |
+
inputs=[
|
90 |
+
gr.Textbox(label="Question"),
|
91 |
+
],
|
92 |
+
outputs="text",
|
93 |
+
title="Finetuned Zephr Model in the Finance Domain."
|
94 |
+
)
|
95 |
+
|
96 |
+
pred.launch(share=True)
|
97 |
+
|
98 |
+
|
99 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
bitsandbytes
|
3 |
+
transformers
|
4 |
+
peft
|
5 |
+
accelerate
|
6 |
+
faiss-gpu
|
7 |
+
datasets
|
8 |
+
trl
|
9 |
+
gradio
|
10 |
+
langchain
|
11 |
+
sentence-transformers
|
12 |
+
pandas
|
13 |
+
langchain-community
|