-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (71 loc) · 2.17 KB
/
main.py
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
import os
import openai
import wget
import pathlib
import pdfplumber
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from pydantic import BaseSettings, BaseModel
class Settings(BaseSettings):
openai.api_type = "azure"
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = "2022-06-01-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")
settings = Settings()
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Payload(BaseModel):
paper_url: str
async def get_paper(paper_url):
"""
Downloads a paper from url and returns
the local path to that file.
"""
downloaded_paper = wget.download(paper_url)
downloaded_paper_file_path = pathlib.Path(downloaded_paper)
return downloaded_paper_file_path
async def generate_tldr(content):
tldr_tag = "\n tl;dr:"
for page in content:
text = page.extract_text() + tldr_tag
response = openai.Completion.create(
engine="text-davinci-002",
prompt=text,
temperature=0.8,
max_tokens=140,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
print(f'GPT output: {response}')
return response["choices"][0]["text"]
@app.get("/health")
async def health():
return {"status": "Ok"}
# Redirect home to docs
@app.get("/")
async def docs_redirect():
return RedirectResponse(url='/docs')
# Generate a tldr for a pdf paper
@app.post("/tldr")
async def tldr(req: Payload):
if not req.paper_url:
print("No paper url provided")
return {"error": "No paper_url provided"}
# Download the paper using wget
paper_file_path = await get_paper(req.paper_url)
print(f'Paper file path: {paper_file_path}')
paper_content = pdfplumber.open(paper_file_path).pages
print(f'Paper content: {paper_content}')
paper_tldr = await generate_tldr(paper_content)
print(f'Paper tldr: {paper_tldr}')
return paper_tldr