File size: 10,991 Bytes
72671db c79cee7 e3981d9 4a36a2e b8d849d 72671db 20b8359 72671db 20b8359 72671db c79cee7 e3981d9 4a36a2e 72671db 1a036c0 72671db 1d0a9b2 72671db f8ce916 72671db e3981d9 72671db e3981d9 22c5bcc 20b8359 22c5bcc e3981d9 72671db 20b8359 c3072e2 20b8359 72671db 92338ad 72671db 1d0a9b2 92338ad 72671db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
import requests
import json
import gradio as gr
# from concurrent.futures import ThreadPoolExecutor
import pdfplumber
import pandas as pd
import langchain
import time
from cnocr import CnOcr
import pinecone
import openai
from langchain.vectorstores import Pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
# from langchain.document_loaders import PyPDFLoader
from langchain.document_loaders import UnstructuredWordDocumentLoader
from langchain.document_loaders import UnstructuredPowerPointLoader
# from langchain.document_loaders.image import UnstructuredImageLoader
from langchain.chains.question_answering import load_qa_chain
from langchain import OpenAI
from sentence_transformers import SentenceTransformer, models, util
word_embedding_model = models.Transformer('sentence-transformers/all-MiniLM-L6-v2', do_lower_case=True)
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), pooling_mode='cls')
embedder = SentenceTransformer(modules=[word_embedding_model, pooling_model])
ocr = CnOcr()
# chat_url = 'https://Raghav001-API.hf.space/sale'
chat_url = 'https://Raghav001-API.hf.space/chatpdf'
chat_emd = 'https://Raghav001-API.hf.space/embedd'
headers = {
'Content-Type': 'application/json',
}
# thread_pool_executor = ThreadPoolExecutor(max_workers=4)
history_max_len = 500
all_max_len = 3000
# Initialize Pinecone client and create an index
pinecone.init(api_key="ffb1f594-0915-4ebf-835f-c1eaa62fdcdc",environment = "us-west4-gcp-free")
index = pinecone.Index(index_name="test")
def get_emb(text):
emb_url = 'https://Raghav001-API.hf.space/embeddings'
data = {"content": text}
try:
result = requests.post(url=emb_url,
data=json.dumps(data),
headers=headers
)
print("--------------------------------Embeddings-----------------------------------")
print(result.json()['data'][0]['embedding'])
return result.json()['data'][0]['embedding']
except Exception as e:
print('data', data, 'result json', result.json())
def doc_emb(doc: str):
texts = doc.split('\n')
# futures = []
emb_list = embedder.encode(texts)
print('emb_list',emb_list)
# for text in texts:
# futures.append(thread_pool_executor.submit(get_emb, text))
# for f in futures:
# emb_list.append(f.result())
print('\n'.join(texts))
pine(doc)
gr.Textbox.update(value="")
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
value="""success ! Let's talk"""), gr.Chatbot.update(visible=True)
def get_response(msg, bot, doc_text_list, doc_embeddings):
# future = thread_pool_executor.submit(get_emb, msg)
gr.Textbox.update(value="")
now_len = len(msg)
req_json = {'question': msg}
his_bg = -1
for i in range(len(bot) - 1, -1, -1):
if now_len + len(bot[i][0]) + len(bot[i][1]) > history_max_len:
break
now_len += len(bot[i][0]) + len(bot[i][1])
his_bg = i
req_json['history'] = [] if his_bg == -1 else bot[his_bg:]
# query_embedding = future.result()
query_embedding = embedder.encode([msg])
cos_scores = util.cos_sim(query_embedding, doc_embeddings)[0]
score_index = [[score, index] for score, index in zip(cos_scores, [i for i in range(len(cos_scores))])]
score_index.sort(key=lambda x: x[0], reverse=True)
print('score_index:\n', score_index)
print('doc_emb_state', doc_emb_state)
index_set, sub_doc_list = set(), []
for s_i in score_index:
doc = doc_text_list[s_i[1]]
if now_len + len(doc) > all_max_len:
break
index_set.add(s_i[1])
now_len += len(doc)
# Maybe the paragraph is truncated wrong, so add the upper and lower paragraphs
if s_i[1] > 0 and s_i[1] -1 not in index_set:
doc = doc_text_list[s_i[1]-1]
if now_len + len(doc) > all_max_len:
break
index_set.add(s_i[1]-1)
now_len += len(doc)
if s_i[1] + 1 < len(doc_text_list) and s_i[1] + 1 not in index_set:
doc = doc_text_list[s_i[1]+1]
if now_len + len(doc) > all_max_len:
break
index_set.add(s_i[1]+1)
now_len += len(doc)
index_list = list(index_set)
index_list.sort()
for i in index_list:
sub_doc_list.append(doc_text_list[i])
req_json['doc'] = '' if len(sub_doc_list) == 0 else '\n'.join(sub_doc_list)
data = {"content": json.dumps(req_json)}
print('data:\n', req_json)
result = requests.post(url=chat_url,
data=json.dumps(data),
headers=headers
)
res = result.json()['content']
bot.append([msg, res])
return bot[max(0, len(bot) - 3):]
def up_file(fls):
doc_text_list = []
names = []
print(names)
for i in fls:
names.append(str(i.name))
pdf = []
docs = []
pptx = []
for i in names:
if i[-3:] == "pdf":
pdf.append(i)
elif i[-4:] == "docx":
docs.append(i)
else:
pptx.append(i)
#Pdf Extracting
for idx, file in enumerate(pdf):
print("11111")
#print(file.name)
with pdfplumber.open(file) as pdf:
for i in range(len(pdf.pages)):
# Read page i+1 of a PDF document
page = pdf.pages[i]
res_list = page.extract_text().split('\n')[:-1]
for j in range(len(page.images)):
# Get the binary stream of the image
img = page.images[j]
file_name = '{}-{}-{}.png'.format(str(time.time()), str(i), str(j))
with open(file_name, mode='wb') as f:
f.write(img['stream'].get_data())
try:
res = ocr.ocr(file_name)
# res = PyPDFLoader(file_name)
except Exception as e:
res = []
if len(res) > 0:
res_list.append(' '.join([re['text'] for re in res]))
tables = page.extract_tables()
for table in tables:
# The first column is used as the header
df = pd.DataFrame(table[1:], columns=table[0])
try:
records = json.loads(df.to_json(orient="records", force_ascii=False))
for rec in records:
res_list.append(json.dumps(rec, ensure_ascii=False))
except Exception as e:
res_list.append(str(df))
doc_text_list += res_list
#pptx Extracting
for i in pptx:
loader = UnstructuredPowerPointLoader(i)
data = loader.load()
# content = str(data).split("'")
# cnt = content[1]
# # c = cnt.split('\\n\\n')
# # final = "".join(c)
# c = cnt.replace('\\n\\n',"").replace("<PAGE BREAK>","").replace("\t","")
doc_text_list.append(data)
#Doc Extracting
for i in docs:
loader = UnstructuredWordDocumentLoader(i)
data = loader.load()
# content = str(data).split("'")
# cnt = content[1]
# # c = cnt.split('\\n\\n')
# # final = "".join(c)
# c = cnt.replace('\\n\\n',"").replace("<PAGE BREAK>","").replace("\t","")
doc_text_list.append(data)
# #Image Extraction
# for i in jpg:
# loader = UnstructuredImageLoader(i)
# data = loader.load()
# # content = str(data).split("'")
# # cnt = content[1]
# # # c = cnt.split('\\n\\n')
# # # final = "".join(c)
# # c = cnt.replace('\\n\\n',"").replace("<PAGE BREAK>","").replace("\t","")
# doc_text_list.append(data)
doc_text_list = [str(text).strip() for text in doc_text_list if len(str(text).strip()) > 0]
# print(doc_text_list)
return gr.Textbox.update(value='\n'.join(doc_text_list), visible=True), gr.Button.update(
visible=True), gr.Markdown.update(
value="Processing")
def pine(data):
char_text_spliter = CharacterTextSplitter(chunk_size = 1000, chunk_overlap=0)
# doc_text = char_text_spliter.split_documents(data)
doc_spilt = []
data = data.split(" ")
# print(len(data))
c = 0
check = 0
for i in data:
# print(i)
if c == 350:
text = " ".join(data[check: check + c])
print(text)
print(check)
doc_spilt.append(text)
check = check + c
c = 0
else:
c = c+1
Embedding_model = "text-embedding-ada-002"
embeddings = OpenAIEmbeddings(openai_api_key="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")
print(requests.post(url = chat_emd))
# embeddings = requests.post(url=chat_emd,
# data=json.dumps(data),
# headers=headers
# )
pinecone.init(api_key = "ffb1f594-0915-4ebf-835f-c1eaa62fdcdc",
environment = "us-west4-gcp-free"
)
index_name = "test"
docstore = Pinecone.from_texts([d for d in doc_spilt],embeddings,index_name = index_name,namespace='a1')
return ''
def get_answer(query_live):
llm = OpenAI(temperature=0, openai='aaa')
qa_chain = load_qa_chain(llm,chain_type='stuff')
query = query_live
docs = docstore.similarity_search(query)
qa_chain.run(input_documents = docs, question = query)
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
file = gr.File(file_types=['.pptx','.docx','.pdf'], label='Click to upload Document', file_count='multiple')
doc_bu = gr.Button(value='Submit', visible=False)
txt = gr.Textbox(label='result', visible=False)
doc_text_state = gr.State([])
doc_emb_state = gr.State([])
with gr.Column():
md = gr.Markdown("Please Upload the PDF")
chat_bot = gr.Chatbot(visible=False)
msg_txt = gr.Textbox(visible = False)
chat_bu = gr.Button(value='Clear', visible=False)
file.change(up_file, [file], [txt, doc_bu, md]) #hiding the text
doc_bu.click(doc_emb, [txt], [doc_text_state, doc_emb_state, msg_txt, chat_bu, md, chat_bot])
msg_txt.submit(get_response, [msg_txt, chat_bot,doc_text_state, doc_emb_state], [chat_bot],queue=False)
chat_bu.click(lambda: None, None, chat_bot, queue=False)
if __name__ == "__main__":
demo.queue().launch(show_api=False)
# demo.queue().launch(share=False, server_name='172.22.2.54', server_port=9191) |