Samarth991 commited on
Commit
c88baef
1 Parent(s): 9c51c0d

adding notebbok and debugging main

Browse files
Files changed (3) hide show
  1. app.py +3 -3
  2. llm_ops.py +23 -13
  3. whisper_app.ipynb +222 -0
app.py CHANGED
@@ -56,7 +56,7 @@ def process_documents(documents,data_chunk=1500,chunk_overlap=100):
56
  texts = text_splitter.split_documents(documents)
57
  return texts
58
 
59
- def audio_processor(wav_file,API_key,wav_model='small',llm='HuggingFace',temperature=0.1,max_tokens=4096,duration=5):
60
  device='cpu'
61
  logger.info("Audio File Name :",wav_file.name)
62
 
@@ -78,7 +78,7 @@ def audio_processor(wav_file,API_key,wav_model='small',llm='HuggingFace',tempera
78
  global qa
79
 
80
  if llm == 'HuggingFace':
81
- chat = llm_ops.get_llama_model()
82
 
83
  chain_type_kwargs = {"prompt": create_prompt()}
84
  qa = RetrievalQA.from_chain_type(llm=chat,
@@ -176,7 +176,7 @@ with gr.Blocks(css=css) as demo:
176
  load_audio = gr.Button("Upload Audio File")
177
  if audio_file:
178
  load_audio.click(loading_file, None, langchain_status, queue=False)
179
- load_audio.click(audio_processor, inputs=[audio_file,API_key,wav_model,LLM_option,temperature,max_new_tokens], outputs=[langchain_status], queue=False)
180
  clean_chat_btn.click(clear_chat, [], chatbot)
181
  question.submit(add_text, inputs=[chatbot, question], outputs=[chatbot, question]).then(bot, chatbot, chatbot)
182
  submit_btn.click(add_text, inputs=[chatbot, question], outputs=[chatbot, question]).then(bot, chatbot, chatbot)
 
56
  texts = text_splitter.split_documents(documents)
57
  return texts
58
 
59
+ def audio_processor(wav_file,API_key,wav_model='small',llm='HuggingFace',temperature=0.1,duration=5):
60
  device='cpu'
61
  logger.info("Audio File Name :",wav_file.name)
62
 
 
78
  global qa
79
 
80
  if llm == 'HuggingFace':
81
+ chat = llm_ops.get_model_from_hub(API_key,model_id='tiiuae/falcon-7b-instruct')
82
 
83
  chain_type_kwargs = {"prompt": create_prompt()}
84
  qa = RetrievalQA.from_chain_type(llm=chat,
 
176
  load_audio = gr.Button("Upload Audio File")
177
  if audio_file:
178
  load_audio.click(loading_file, None, langchain_status, queue=False)
179
+ load_audio.click(audio_processor, inputs=[audio_file,API_key,wav_model,LLM_option,temperature], outputs=[langchain_status], queue=False)
180
  clean_chat_btn.click(clear_chat, [], chatbot)
181
  question.submit(add_text, inputs=[chatbot, question], outputs=[chatbot, question]).then(bot, chatbot, chatbot)
182
  submit_btn.click(add_text, inputs=[chatbot, question], outputs=[chatbot, question]).then(bot, chatbot, chatbot)
llm_ops.py CHANGED
@@ -3,6 +3,7 @@ import torch
3
  from langchain import HuggingFacePipeline
4
  from transformers import AutoTokenizer
5
  import transformers
 
6
 
7
  def get_openai_chat_model(API_key):
8
  try:
@@ -14,19 +15,28 @@ def get_openai_chat_model(API_key):
14
  return llm
15
 
16
 
17
- def get_llama_model():
18
- model = "meta-llama/Llama-2-7b-chat-hf"
 
 
 
19
 
20
- tokenizer = AutoTokenizer.from_pretrained(model)
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- pipeline = transformers.pipeline("text-generation",
23
- model=model,
24
- tokenizer=tokenizer,
25
- torch_dtype=torch.bfloat16,
26
- trust_remote_code=True,
27
- device_map="auto",
28
- max_length=1000,
29
- eos_token_id=tokenizer.eos_token_id
30
- )
31
- llm = HuggingFacePipeline(pipeline = pipeline, model_kwargs = {'temperature':0})
32
  return llm
 
3
  from langchain import HuggingFacePipeline
4
  from transformers import AutoTokenizer
5
  import transformers
6
+ from langchain import HuggingFaceHub
7
 
8
  def get_openai_chat_model(API_key):
9
  try:
 
15
  return llm
16
 
17
 
18
+ def get_llama_model(temperature=0,api_key=None,max_tokens=2048):
19
+ model_id = "meta-llama/Llama-2-7b-chat-hf"
20
+ llm = None
21
+ try:
22
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
23
 
24
+ pipeline = transformers.pipeline("text-generation",
25
+ model=model_id,
26
+ tokenizer=tokenizer,
27
+ torch_dtype=torch.bfloat16,
28
+ trust_remote_code=True,
29
+ device_map="auto",
30
+ max_length=1000,
31
+ eos_token_id=tokenizer.eos_token_id
32
+ )
33
+ llm = HuggingFacePipeline(pipeline = pipeline, model_kwargs = {'temperature':temperature})
34
+ except:
35
+ raise "User not autorized to access the Model"
36
+ return llm
37
 
38
+ def get_model_from_hub(api_key,temperature=0.1,max_tokens=2048,model_id="meta-llama/Llama-2-7b-chat-hf"):
39
+ llm = HuggingFaceHub(huggingfacehub_api_token=api_key,
40
+ repo_id=model_id,
41
+ model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens})
 
 
 
 
 
 
42
  return llm
whisper_app.ipynb ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stderr",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "/home/samartht/miniconda3/envs/langchain/lib/python3.10/site-packages/whisper/timing.py:58: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\n",
13
+ " def backtrace(trace: np.ndarray):\n"
14
+ ]
15
+ }
16
+ ],
17
+ "source": [
18
+ "import numpy as np \n",
19
+ "import whisper_app\n",
20
+ "from glob import glob \n",
21
+ "import llm_ops\n",
22
+ "from langchain.text_splitter import CharacterTextSplitter\n",
23
+ "from langchain.docstore.document import Document\n",
24
+ "from langchain.embeddings import SentenceTransformerEmbeddings\n",
25
+ "from langchain.vectorstores import FAISS\n",
26
+ "from langchain.chains import RetrievalQA"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 2,
32
+ "metadata": {},
33
+ "outputs": [
34
+ {
35
+ "name": "stdout",
36
+ "output_type": "stream",
37
+ "text": [
38
+ "total number of audio files : 150\n"
39
+ ]
40
+ }
41
+ ],
42
+ "source": [
43
+ "audio_processor = whisper_app.WHISPERModel(device='cuda')\n",
44
+ "path = \"../../Conversational_GPT/data/Audio/*.mp3\"\n",
45
+ "\n",
46
+ "audio_files = glob(path)\n",
47
+ "print(\"total number of audio files :\",len(audio_files))"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": 3,
53
+ "metadata": {},
54
+ "outputs": [
55
+ {
56
+ "name": "stdout",
57
+ "output_type": "stream",
58
+ "text": [
59
+ " Hello. Hello, good morning sir. Good morning. My name is Sarvinder and I'm calling from PRIP for services sir, how are you? Okay. Sir, I believe you're in an inquiry regarding mortgage. Are you looking for a mortgage services sir? For service? Service? Like mortgage, are you looking for a mortgage? Marked mortgage, home loan, home loan. Home loan. Yes. No, no, no. I am talking with one, no, no, yesterday one lady called me from Arab Bank. Okay. I am making, I need the loan for home loan but by bank, Arab Bank. Yes. We can give you the bank sir, we can give you only from the bank only but we can give you the less interest rate if you want sir. I don't know, this is your company what? We are a PRIPCO company sir, we are tired with the multiple banks. Okay. We can tell you from which bank you can, what interest rate you get, you can get sir, what interest rate they are offering you sir. But I need to ask you, who give you my number, that's all in the beginning. This lady, I have the name lady yesterday she called, what is the name of the lady sir? I have the name. Can you tell me the name? Stay with me, stay with me. Yeah, yeah, yeah. Hello, you are staying with me? Yes sir, it's Awan, the name, name Madame Sara. Yeah, she's working in agents which Awan real estate broker, I got from there, your number is from the broker. Okay, now I have a plan to buy one house, the budget near 500,000 dirhams. Okay. And she told me we need to go to take it from Arab Bank because Arab Bank is better for the payment of tax. It's not like that, what interest they are giving you sir, that's what I want to know sir, how much they are offering you. I will tell you something, this is your number now? Yes. This is your number, what's up? I can send you high on WhatsApp sir. Send me my WhatsApp, I will send you which apartment I need, okay. And after we are selling the apartment, we give on the mortgage. No, no, no. Like loan? Yeah, loan, correct loan. So I need some few details, okay. So like, you know, are you residents in UAE or non-residents sir? Yeah, but one second, one second. I don't need to take loan. I need to buy one apartment, buy my bank or another bank. I need to buy this, okay, I have deposits from me, 10% cash, I can deposit, the balance, I need to take it from the bank. I don't need loan, personal loan. If you are taking from the bank, so you need mortgage only sir, that's called mortgage. So that's why I am saying for that, yeah, for that I need some details like, you know, are you residents in UAE or non-residents? Yes, yes, I have everything, all the papers, I can send these to you. What's your age sir? 56. 56, okay, that's good. Okay, and you know, are you a seller or you have your own company? No, no sir, I have 30,000 per month. 30,000 per month. Okay. And since how long you are working in the same company? Same company, 8 years. 8 years. Yes. 8 years, okay, 8 years. Okay, and how many credit cards you have? I have now 2 credit cards. 2 credit cards, okay. Yes. Okay, do you have any personal loan, auto loan? Already I take one loan from Mushrik Bank. Mushrik Bank, one loan. One loan only. Bank loan. Okay, how much you are paying monthly to the bank? 4,900, taxes. 4,900. Yes. Okay, and may I know what's your nationality? Lebanese, I am from Lebanon. Lebanese. Lebanese. Okay, and looking for an off plan or are you looking for ready to move in? For ready to move. Ready to move. Ready to move in. Yes. Okay. And how much loan you are looking for? Like how much you want from the bank? How much money? I have my plan now. I saw one room, yes, 500. I need from the bank 450, near. 450, yes. Okay, and this is going to be your first property in UAE? First property need to buy, yes. Okay, and when you are planning to buy this property, as soon as possible? As soon as possible, yes. Everything is okay, no problem. Okay. So what I will do, I will just send this details to the bank and the market, the person, the loan person will give you a call and he will give you the information. Okay. Okay, okay, great. Thank you so much. Thank you. Thank you. Thank you. Thank you so much. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.\n"
60
+ ]
61
+ }
62
+ ],
63
+ "source": [
64
+ "text_data = audio_processor.speech_to_text(audio_path=audio_files[2])\n",
65
+ "print(text_data['text'])"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "markdown",
70
+ "metadata": {},
71
+ "source": [
72
+ "### Laama Model "
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": 4,
78
+ "metadata": {},
79
+ "outputs": [],
80
+ "source": [
81
+ "llm = llm_ops.get_model_from_hub(model_id='tiiuae/falcon-7b',api_key='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')"
82
+ ]
83
+ },
84
+ {
85
+ "cell_type": "markdown",
86
+ "metadata": {},
87
+ "source": [
88
+ "### Create Document "
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": 5,
94
+ "metadata": {},
95
+ "outputs": [],
96
+ "source": [
97
+ "def process_documents(documents,data_chunk=1500,chunk_overlap=100):\n",
98
+ " text_splitter = CharacterTextSplitter(chunk_size=data_chunk, chunk_overlap=chunk_overlap,separator='\\n')\n",
99
+ " texts = text_splitter.split_documents(documents)\n",
100
+ " return texts\n",
101
+ "\n",
102
+ "metadata = {\"source\": f\"{audio_files[1]}\",\"duration\":text_data['duration'],\"language\":text_data['language']}\n",
103
+ "document = [Document(page_content=text_data['text'], metadata=metadata)]\n",
104
+ "splited_doc = process_documents(documents=document)\n"
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "execution_count": 6,
110
+ "metadata": {},
111
+ "outputs": [],
112
+ "source": [
113
+ "from langchain.prompts import PromptTemplate\n",
114
+ "\n",
115
+ "def create_prompt():\n",
116
+ " prompt_template = \"\"\"You are a chatbot that answers questions regarding the conversation in given context . \n",
117
+ " Use the following context to answer in sentences and points. \n",
118
+ " If you don't know the answer, just say I don't know. \n",
119
+ "\n",
120
+ " {context}\n",
121
+ "\n",
122
+ " Question: {question}\n",
123
+ " Answer :\"\"\"\n",
124
+ " prompt = PromptTemplate(\n",
125
+ " template=prompt_template, input_variables=[\"context\", \"question\"]\n",
126
+ " )\n",
127
+ " return prompt"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "markdown",
132
+ "metadata": {},
133
+ "source": [
134
+ "## Create Embeddings "
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": 7,
140
+ "metadata": {},
141
+ "outputs": [],
142
+ "source": [
143
+ "import torch\n",
144
+ "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
145
+ "embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large',model_kwargs={\"device\": device})"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": null,
151
+ "metadata": {},
152
+ "outputs": [],
153
+ "source": [
154
+ "vector_db = FAISS.from_documents(documents=splited_doc, embedding= embedding_model)\n",
155
+ "chain_type_kwargs = {\"prompt\": create_prompt()}\n",
156
+ "\n",
157
+ "qa = RetrievalQA.from_chain_type(llm=llm,\n",
158
+ " chain_type='stuff',\n",
159
+ " retriever=vector_db.as_retriever(),\n",
160
+ " chain_type_kwargs=chain_type_kwargs,\n",
161
+ " return_source_documents=True\n",
162
+ " )"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "markdown",
167
+ "metadata": {},
168
+ "source": [
169
+ "### Q&A"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "metadata": {},
176
+ "outputs": [],
177
+ "source": [
178
+ "question = \"How much loan customer is asking for\"\n",
179
+ "result = qa({\"query\": question})\n",
180
+ "matching_docs_score = vector_db.similarity_search_with_score(question)"
181
+ ]
182
+ },
183
+ {
184
+ "cell_type": "code",
185
+ "execution_count": null,
186
+ "metadata": {},
187
+ "outputs": [],
188
+ "source": [
189
+ "matching_docs_score"
190
+ ]
191
+ },
192
+ {
193
+ "cell_type": "code",
194
+ "execution_count": null,
195
+ "metadata": {},
196
+ "outputs": [],
197
+ "source": []
198
+ }
199
+ ],
200
+ "metadata": {
201
+ "kernelspec": {
202
+ "display_name": "pytf",
203
+ "language": "python",
204
+ "name": "python3"
205
+ },
206
+ "language_info": {
207
+ "codemirror_mode": {
208
+ "name": "ipython",
209
+ "version": 3
210
+ },
211
+ "file_extension": ".py",
212
+ "mimetype": "text/x-python",
213
+ "name": "python",
214
+ "nbconvert_exporter": "python",
215
+ "pygments_lexer": "ipython3",
216
+ "version": "3.10.12"
217
+ },
218
+ "orig_nbformat": 4
219
+ },
220
+ "nbformat": 4,
221
+ "nbformat_minor": 2
222
+ }