Spaces:
Sleeping
Sleeping
Rename qa_bot2.py to qa_bot_chatgpt.py
Browse files- qa_bot2.py → qa_bot_chatgpt.py +15 -38
qa_bot2.py → qa_bot_chatgpt.py
RENAMED
@@ -6,28 +6,12 @@ import os
|
|
6 |
import torch
|
7 |
from huggingface_hub import login
|
8 |
import ast
|
9 |
-
|
10 |
class QAInfer:
|
11 |
def __init__(self):
|
12 |
torch.cuda.empty_cache()
|
13 |
-
|
14 |
-
|
15 |
-
load_in_4bit=True,
|
16 |
-
bnb_4bit_quant_type="nf4",
|
17 |
-
bnb_4bit_compute_dtype="float16",
|
18 |
-
bnb_4bit_use_double_quant=False,
|
19 |
-
)
|
20 |
-
self.qa_tokenizer = AutoTokenizer.from_pretrained(qa_model_name)
|
21 |
-
self.qa_tokenizer.pad_token = self.qa_tokenizer.eos_token
|
22 |
-
|
23 |
-
self.qa_model = AutoModelForCausalLM.from_pretrained(
|
24 |
-
qa_model_name,
|
25 |
-
quantization_config=bnb_config,
|
26 |
-
torch_dtype=torch.bfloat16,
|
27 |
-
device_map="auto",
|
28 |
-
trust_remote_code=True,
|
29 |
-
cache_dir="cache/mistral"
|
30 |
-
)
|
31 |
|
32 |
def extract_text_from_pdf(self, pdf_path):
|
33 |
"""Extract text from a PDF file."""
|
@@ -103,27 +87,20 @@ class QAInfer:
|
|
103 |
{pdf_text}
|
104 |
|
105 |
=== Response ===
|
106 |
-
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
prompt,
|
118 |
-
do_sample=True,
|
119 |
-
max_new_tokens=200,
|
120 |
-
temperature=0.7,
|
121 |
-
top_k=50,
|
122 |
-
top_p=0.95,
|
123 |
-
num_return_sequences=1,
|
124 |
)
|
125 |
-
|
126 |
-
|
127 |
|
128 |
if __name__ == '__main__':
|
129 |
qa_infer = QAInfer()
|
|
|
6 |
import torch
|
7 |
from huggingface_hub import login
|
8 |
import ast
|
9 |
+
from openai import OpenAI
|
10 |
class QAInfer:
|
11 |
def __init__(self):
|
12 |
torch.cuda.empty_cache()
|
13 |
+
|
14 |
+
self.chatgpt_client = OpenAI(api_key="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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def extract_text_from_pdf(self, pdf_path):
|
17 |
"""Extract text from a PDF file."""
|
|
|
87 |
{pdf_text}
|
88 |
|
89 |
=== Response ===
|
90 |
+
If related answer is not found return 'Information not present in the pdf' and below it provide something related to the question"""
|
91 |
|
92 |
+
|
93 |
+
|
94 |
+
print(prompt)
|
95 |
+
completion = self.chatgpt_client.chat.completions.create(
|
96 |
+
model="gpt-3.5-turbo",
|
97 |
+
messages=[
|
98 |
+
{"role": "system", "content": "You are a expert PDF parser , go through the pdf and answer the question properly , if related answer is not found return 'Information not present in the pdf' and below it provide something related to the question"},
|
99 |
+
{"role": "user", "content": prompt }
|
100 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
)
|
102 |
+
return completion.choices[0].message.content
|
103 |
+
|
104 |
|
105 |
if __name__ == '__main__':
|
106 |
qa_infer = QAInfer()
|