Spaces:
Running
Running
Qifan Zhang
commited on
Commit
·
806d7c6
1
Parent(s):
17719d2
complete version 1.0
Browse files- .gitignore +1 -0
- app.py +17 -0
- requirements.txt +4 -0
- utils/chatgpt.py +44 -0
- utils/read_pdf.py +15 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.idea/
|
app.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from utils.chatgpt import ChatGPTAPI
|
4 |
+
from utils.read_pdf import read_pdf
|
5 |
+
|
6 |
+
|
7 |
+
def process(api_key: str = '', prompt: str = '', file=None) -> str:
|
8 |
+
chatgpt = ChatGPTAPI(api_key, max_input_length=1024)
|
9 |
+
|
10 |
+
pdf_contents = read_pdf(file.name)
|
11 |
+
pdf_str = '\n'.join(pdf_contents)
|
12 |
+
content = prompt + '\n' + pdf_str
|
13 |
+
response = chatgpt(content)
|
14 |
+
return response
|
15 |
+
|
16 |
+
|
17 |
+
gr.Interface(fn=process, inputs=["text", "text", "file"], outputs="text").launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|
3 |
+
pypdf
|
4 |
+
|
utils/chatgpt.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import tiktoken
|
3 |
+
|
4 |
+
|
5 |
+
class ChatGPTAPI:
|
6 |
+
def __init__(self, api_key='', max_input_length=1024):
|
7 |
+
if not api_key:
|
8 |
+
try:
|
9 |
+
api_key = open('data/api_key.txt', 'r').read()
|
10 |
+
except Exception as e:
|
11 |
+
raise Exception(f'ChatGPT Error: No API key provided {e}')
|
12 |
+
|
13 |
+
openai.api_key = api_key
|
14 |
+
self.max_input_length = max_input_length
|
15 |
+
self.encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
|
16 |
+
|
17 |
+
def truncate_string(self, s):
|
18 |
+
e = self.encoding.encode(s)[:self.max_input_length]
|
19 |
+
s = self.encoding.decode(e)
|
20 |
+
return s
|
21 |
+
|
22 |
+
def __call__(self, content: str):
|
23 |
+
assert isinstance(content, str), 'ChatGPT Error: content must be a string'
|
24 |
+
content = content.strip()
|
25 |
+
content = self.truncate_string(content)
|
26 |
+
messages = [{'role': 'user', 'content': content}]
|
27 |
+
try:
|
28 |
+
resp = openai.ChatCompletion.create(
|
29 |
+
model="gpt-3.5-turbo",
|
30 |
+
messages=messages
|
31 |
+
)
|
32 |
+
output: str = resp['choices'][0]['message']['content']
|
33 |
+
output = output.strip()
|
34 |
+
except Exception as e:
|
35 |
+
raise Exception(f'ChatGPT Error: {e}')
|
36 |
+
return output
|
37 |
+
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
chatgpt = ChatGPTAPI()
|
41 |
+
r = chatgpt.truncate_string('how are you ' * 10000)
|
42 |
+
r_list = r.split(' ')
|
43 |
+
# response = chatgpt('Hello, how are you?')
|
44 |
+
# print(response)
|
utils/read_pdf.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pypdf
|
2 |
+
|
3 |
+
|
4 |
+
def read_pdf(filepath) -> list[str]:
|
5 |
+
outputs = []
|
6 |
+
with open(filepath, 'rb') as f:
|
7 |
+
pdf_reader = pypdf.PdfReader(f)
|
8 |
+
for page in pdf_reader.pages:
|
9 |
+
outputs.append(page.extract_text())
|
10 |
+
return outputs
|
11 |
+
|
12 |
+
|
13 |
+
if __name__ == '__main__':
|
14 |
+
r = read_pdf('data/109-411-2-PB.pdf')
|
15 |
+
print(r)
|