Dmitry Kirsanov commited on
Commit
097e81a
·
1 Parent(s): a88398a

bare bones llm, understander

Browse files
Files changed (5) hide show
  1. app.py +2 -1
  2. llm.py +87 -0
  3. parse.py +35 -2
  4. requirements.txt +4 -1
  5. understand.py +35 -0
app.py CHANGED
@@ -1,10 +1,11 @@
1
  import gradio as gr
2
 
3
  import parse
 
4
 
5
 
6
  def process(filename, model):
7
- return parse.parse(filename)
8
 
9
  gradio_app = gr.Interface(
10
  fn=process,
 
1
  import gradio as gr
2
 
3
  import parse
4
+ import understand
5
 
6
 
7
  def process(filename, model):
8
+ return understand.understand(filename)
9
 
10
  gradio_app = gr.Interface(
11
  fn=process,
llm.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import anthropic
4
+ import google.generativeai as palm
5
+ import openai
6
+ import tiktoken
7
+
8
+ from dotenv import load_dotenv
9
+ load_dotenv()
10
+
11
+ class LLM:
12
+ def __init__(self, context_length, api_endpoint, max_output_tokens):
13
+ self.context_length = context_length
14
+ self.api_endpoint = api_endpoint
15
+ self.max_output_tokens = max_output_tokens
16
+
17
+ def send_question(self, question):
18
+ raise NotImplementedError("Subclass must implement send_question method")
19
+
20
+ def count_tokens(self, text):
21
+ raise NotImplementedError("Subclass must implement count_tokens method")
22
+
23
+ class Claude3Opus(LLM):
24
+ def __init__(self):
25
+ super().__init__(context_length=200000, api_endpoint="https://api.anthropic.com", max_output_tokens=4096)
26
+ anthropic.api_key = "YOUR_ANTHROPIC_API_KEY"
27
+
28
+ def send_question(self, question):
29
+ num_tokens = self.count_tokens(question)
30
+ if num_tokens > self.context_length - self.max_output_tokens:
31
+ raise ValueError(f"Question exceeds context length limit of {self.context_length} tokens")
32
+
33
+ response = anthropic.completions.create(
34
+ prompt=f"{anthropic.HUMAN_PROMPT} {question} {anthropic.AI_PROMPT}",
35
+ model="claude-3-opus",
36
+ max_tokens_to_sample=self.max_output_tokens,
37
+ )
38
+ output_truncated = len(response.completion) == self.max_output_tokens
39
+ return response.completion, output_truncated
40
+
41
+ def count_tokens(self, text):
42
+ encoding = tiktoken.get_encoding("cl100k_base") # FIXME: Claude didn't release its tokenizer; consider using https://github.com/javirandor/anthropic-tokenizer
43
+ return len(encoding.encode(text))
44
+
45
+ class Gemini15(LLM):
46
+ def __init__(self):
47
+ super().__init__(context_length=256000, api_endpoint="https://generativelanguage.googleapis.com", max_output_tokens=8192)
48
+ palm.configure(api_key="YOUR_GOOGLE_API_KEY")
49
+
50
+ def send_question(self, question):
51
+ num_tokens = self.count_tokens(question)
52
+ if num_tokens > self.context_length - self.max_output_tokens:
53
+ raise ValueError(f"Question exceeds context length limit of {self.context_length} tokens")
54
+
55
+ response = palm.generate_text(
56
+ model="models/text-bison-001",
57
+ prompt=question,
58
+ max_output_tokens=self.max_output_tokens,
59
+ )
60
+ output_truncated = len(response.result) == self.max_output_tokens
61
+ return response.result, output_truncated
62
+
63
+ def count_tokens(self, text):
64
+ encoding = tiktoken.get_encoding("p50k_base") # FIXME: Gemini didn't release its tokenizer; as of apr 2024, the only thing i could google is https://www.googlecloudcommunity.com/gc/AI-ML/Please-share-Gemini-tokenize-information/m-p/709495
65
+ return len(encoding.encode(text))
66
+
67
+ class GPT4Turbo(LLM):
68
+ def __init__(self):
69
+ super().__init__(context_length=128000, api_endpoint="https://api.openai.com/v1/chat/completions", max_output_tokens=4096)
70
+ self.client = openai.OpenAI(api_key = os.environ.get("OPENAI_API_KEY")) # stored in .env file
71
+
72
+ def send_question(self, question):
73
+ num_tokens = self.count_tokens(question)
74
+ if num_tokens > self.context_length - self.max_output_tokens:
75
+ raise ValueError(f"Question exceeds context length limit of {self.context_length} tokens")
76
+
77
+ response = self.client.chat.completions.create(
78
+ model="gpt-4-turbo",
79
+ messages=[{"role": "user", "content": question}],
80
+ max_tokens=self.max_output_tokens,
81
+ )
82
+ output_truncated = len(response.choices[0].message.content) == self.max_output_tokens
83
+ return response.choices[0].message.content, output_truncated
84
+
85
+ def count_tokens(self, text):
86
+ encoding = tiktoken.get_encoding("cl100k_base") # GPT-4 uses the cl100k_base encoding https://github.com/openai/tiktoken
87
+ return len(encoding.encode(text))
parse.py CHANGED
@@ -4,9 +4,42 @@ from transformers.utils import logging
4
 
5
  logger = logging.get_logger("transformers")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def parse(filepath):
8
- soup = BeautifulSoup(open(filepath).read(), 'html.parser')
9
  logger.debug(f"Parsed {filepath}")
10
- return soup.prettify()
11
 
12
 
 
4
 
5
  logger = logging.get_logger("transformers")
6
 
7
+ def cleanup(soup):
8
+
9
+ # Remove all HTML comments
10
+ for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
11
+ comment.extract()
12
+
13
+ # Remove unnecessary <meta> tags from the <head>
14
+ for meta in soup.select('head meta'):
15
+ if not meta.has_attr('charset') and not meta.has_attr('http-equiv'):
16
+ meta.decompose()
17
+
18
+ # Remove all inline CSS styles
19
+ for tag in soup.find_all(True):
20
+ if tag.has_attr('style'):
21
+ del tag['style']
22
+
23
+ # Remove any empty <b>, <i>, <u>, <sup> elements
24
+ for tag in soup.select('b:empty, i:empty, u:empty, sup:empty'):
25
+ tag.decompose()
26
+
27
+ # Remove unnecessary attributes like valign and align from tags
28
+ for tag in soup.find_all(True):
29
+ for attr in ['valign', 'align']:
30
+ if tag.has_attr(attr):
31
+ del tag[attr]
32
+
33
+ # Remove all <span class="page"> elements that just contain page numbers
34
+ for span in soup.select('span.page'):
35
+ if span.get_text(strip=True).isdigit():
36
+ span.decompose()
37
+
38
+ return soup
39
+
40
  def parse(filepath):
41
+ soup = cleanup(BeautifulSoup(open(filepath).read(), 'html.parser'))
42
  logger.debug(f"Parsed {filepath}")
43
+ return soup()
44
 
45
 
requirements.txt CHANGED
@@ -1,4 +1,7 @@
1
  python-dotenv
2
  openai
3
  bs4
4
- transformers
 
 
 
 
1
  python-dotenv
2
  openai
3
  bs4
4
+ transformers
5
+ anthropic
6
+ google-generativeai
7
+ tiktoken
understand.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import parse
2
+ import llm
3
+
4
+ LLM = llm.GPT4Turbo()
5
+
6
+ class Document():
7
+
8
+ def __init__(self) -> None:
9
+ self.documentSoup = None # beautifulSoup
10
+ self.fad = None # string
11
+ self.doctype = None
12
+ self.summary = None
13
+
14
+ def query_doctype(self):
15
+ try:
16
+ answer, truncated = LLM.send_question(f"""Please read this document carefully, remember all you know about the concepts it touches, and give a brief description of what this document is. Examples: "Textbook about interpersonal psychology", "State standard on oil rig equipment", "User manual for a smartphone app". Write just one line with the description.
17
+
18
+ <document>{self.fad}</document>""")
19
+
20
+ if truncated:
21
+ print ("Document too long, needs compression")
22
+ return None
23
+ else:
24
+ return answer.strip()
25
+
26
+ except Exception as e:
27
+ print (e.message, e.args)
28
+ return None
29
+
30
+ def understand(self, filename):
31
+ self.documentSoup = parse.parse(filename)
32
+ self.fad = self.documentSoup.get_text()
33
+ self.doctype = self.query_doctype()
34
+ return self.doctype
35
+