96 lines
2.2 KiB
Python
96 lines
2.2 KiB
Python
import nextcord as nc
|
|
from nextcord.ext import commands
|
|
from typing import Optional
|
|
import asyncio
|
|
import io
|
|
import os
|
|
import requests as req
|
|
from bs4 import BeautifulSoup as soup
|
|
import pdftotext
|
|
import sys
|
|
import openai
|
|
|
|
openai.api_key = os.getenv("OPENAI_KEY")
|
|
|
|
def eprint(*args, **kwargs):
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
bot = commands.Bot()
|
|
|
|
async def search(txt):
|
|
url = "https://www.sci-hub.st/"
|
|
resp = req.post(url, data={
|
|
'request':txt
|
|
})
|
|
|
|
doc = soup(resp.text, 'html.parser')
|
|
|
|
if 'not found' in doc.find('title').get_text():
|
|
return None
|
|
|
|
ref = doc.find('div', {'id': 'citation'}).get_text()
|
|
pdf = doc.find('embed', {'id': 'pdf'})['src']
|
|
|
|
pdf = pdf[:(pdf.find(".pdf")+4)]
|
|
|
|
return {
|
|
'ref': ref,
|
|
'pdf': f"https:{pdf}"
|
|
}
|
|
|
|
async def getPDF(url):
|
|
eprint(f"Fetching PDF: {url}")
|
|
resp = req.get(url, stream=True)
|
|
pdf = io.BytesIO(resp.content)
|
|
pages = pdftotext.PDF(pdf)
|
|
return "\n\n".join(pages)
|
|
|
|
async def summarize(pdf):
|
|
completion = openai.ChatCompletion.create(
|
|
model="gpt-3.5-turbo",
|
|
messages=[
|
|
{"role": "user", "content": f"""Please summarize the following research paper:
|
|
---
|
|
{pdf}
|
|
"""}
|
|
]
|
|
)
|
|
return completion
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
print(f'We have logged in as {bot.user}')
|
|
|
|
@bot.slash_command(name="summarize", description="Summarize all or part of an article", dm_permission=True)
|
|
async def summarize(
|
|
interaction: nc.Interaction,
|
|
article: str,
|
|
part: Optional[str] = nc.SlashOption(required=False)
|
|
):
|
|
await interaction.response.defer(ephemeral=False, with_message=True)
|
|
|
|
resp = await search(article)
|
|
|
|
if resp is None:
|
|
await interaction.followup.send(f"Unable to find article: {article}")
|
|
return
|
|
|
|
msg = await interaction.followup.send(f"""Article Found: {resp['ref']}
|
|
Processing PDF...""")
|
|
pdf = await getPDF(resp['pdf'])
|
|
|
|
await msg.edit(f"""Article Found: {resp['ref']}
|
|
```
|
|
{pdf[:1000]}
|
|
```""")
|
|
|
|
def main():
|
|
# TODO: Import bot token from env
|
|
bot.run(os.environ["DISCORD_TOKEN"])
|
|
|
|
if __name__ == "__main__":
|
|
print("Handy Helper has Begun!")
|
|
main()
|
|
|
|
|