YDTsai commited on
Commit
9fa6897
1 Parent(s): cb8fdae
Files changed (3) hide show
  1. src/app.py +61 -46
  2. src/suggest.py +8 -3
  3. src/utils.py +36 -26
src/app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  from suggest import Suggest
4
  from edit import Editor
5
  from config import configure_logging
 
6
 
7
 
8
  configure_logging()
@@ -10,59 +11,73 @@ configure_logging()
10
 
11
  with gr.Blocks() as demo:
12
 
13
- title = gr.Button("PaperGPT", interactive=False)
14
  key = gr.Textbox(label="openai_key", value=os.environ.get('OPENAI_API_KEY'))
15
 
16
- with gr.Tab("Edit"):
17
-
18
- handler = Editor()
19
- txt_in = gr.Textbox(label="Input", lines=11, max_lines=11, value=handler.sample_content)
20
- btn = gr.Button("Edit")
21
- txt_out = gr.Textbox(label="Output", lines=11, max_lines=11, value="GPT will serve as your editor and modify the paragraph for you.")
22
- btn.click(handler.generate, inputs=[txt_in, key], outputs=[txt_out])
23
-
24
- with gr.Tab("Suggest"):
25
-
26
- idea_list = []
27
- max_ideas = 20
28
- handler = Suggest(max_ideas)
29
 
30
- def select(name: str):
31
- global idea_list
32
- for i in idea_list:
33
- if i['title'] == name:
34
- return [
35
- gr.Textbox.update(value=i["thought"], label="thought", visible=True),
36
- gr.Textbox.update(value=i["action"], label="action", visible=True),
37
- gr.Textbox.update(value=i["original"], label="original", visible=True, max_lines=5, lines=5),
38
- gr.Textbox.update(value=i["improved"], label="improved", visible=True, max_lines=5, lines=5)
39
- ]
40
 
41
- with gr.Row().style(equal_height=True):
42
- with gr.Column(scale=0.95):
43
- txt_in = gr.Textbox(label="Input", lines=11, max_lines=11, value=handler.sample_content[2048+2048+256-45:])
44
- with gr.Column(scale=0.05):
45
- upload = gr.File(file_count="single", file_types=["tex", ".pdf"])
46
- btn = gr.Button("Analyze")
47
- upload.change(handler.read_file, inputs=upload, outputs=txt_in)
48
 
49
- textboxes = []
50
- sug = gr.Textbox("GPT will give suggestions and help you improve the paper quality.", interactive=False, show_label=False).style(text_align="center")
51
- with gr.Row():
52
- with gr.Column(scale=0.4):
53
- for i in range(max_ideas):
54
- t = gr.Button("", visible=False)
55
- textboxes.append(t)
56
- with gr.Column(scale=0.6):
57
- thought = gr.Textbox(label="thought", visible=False, interactive=False)
58
- action = gr.Textbox(label="action", visible=False, interactive=False)
59
- original = gr.Textbox(label="original", visible=False, max_lines=5, lines=5, interactive=False)
60
- improved = gr.Textbox(label="improved", visible=False, max_lines=5, lines=5, interactive=False)
 
 
 
 
 
 
 
 
 
61
 
62
- btn.click(handler.generate, inputs=[txt_in, key], outputs=[sug, btn, thought, action, original, improved] + textboxes)
63
- for i in textboxes:
64
- i.click(select, inputs=[i], outputs=[thought, action, original, improved])
65
 
 
 
 
 
 
66
 
67
  # demo.launch(server_name="0.0.0.0", server_port=7653, share=True, enable_queue=True)
68
  demo.launch(enable_queue=True)
 
3
  from suggest import Suggest
4
  from edit import Editor
5
  from config import configure_logging
6
+ from utils import diff_texts
7
 
8
 
9
  configure_logging()
 
11
 
12
  with gr.Blocks() as demo:
13
 
14
+ title = gr.Button("PaperGPT", interactive=True)
15
  key = gr.Textbox(label="openai_key", value=os.environ.get('OPENAI_API_KEY'))
16
 
17
+ with gr.Row():
18
+ with gr.Tab("Edit"):
19
+
20
+ handler = Editor()
21
+ txt_in = gr.Textbox(label="Input", lines=11, max_lines=11, value=handler.sample_content)
22
+ btn = gr.Button("Edit")
23
+ txt_out = gr.Textbox(label="Output", lines=11, max_lines=11, value="GPT will serve as your editor and modify the paragraph for you.")
24
+ btn.click(handler.generate, inputs=[txt_in, key], outputs=[txt_out])
25
+
26
+ with gr.Tab("Suggest"):
27
+
28
+ max_ideas = 5
29
+ handler = Suggest(max_ideas)
30
 
31
+ def select(name: str):
32
+ for i in handler.idea_list:
33
+ if i['title'] == name:
34
+ return [
35
+ gr.Textbox.update(value=i["thought"], label="thought", visible=True),
36
+ gr.Textbox.update(value=i["action"], label="action", visible=True),
37
+ gr.Textbox.update(value=i["original"], label="original", visible=True, max_lines=5, lines=5),
38
+ gr.Textbox.update(value=i["improved"], label="improved", visible=True, max_lines=5, lines=5),
39
+ gr.HighlightedText.update(value=diff_texts(i["original"], i["improved"]), visible=True)
40
+ ]
41
 
42
+ with gr.Row().style(equal_height=True):
43
+ with gr.Column(scale=0.95):
44
+ txt_in = gr.Textbox(label="Input", lines=11, max_lines=11, value=handler.sample_content[2048+2048+256-45:])
45
+ with gr.Column(scale=0.05):
46
+ upload = gr.File(file_count="single", file_types=["tex", ".pdf"])
47
+ btn = gr.Button("Analyze")
48
+ upload.change(handler.read_file, inputs=upload, outputs=txt_in)
49
 
50
+ textboxes = []
51
+ sug = gr.Textbox("GPT will give suggestions and help you improve the paper quality.", interactive=False, show_label=False, lines=11).style(text_align="center")
52
+ with gr.Row():
53
+ with gr.Column(scale=0.4):
54
+ for i in range(max_ideas):
55
+ t = gr.Button("", visible=False)
56
+ textboxes.append(t)
57
+ with gr.Column(scale=0.6):
58
+ thought = gr.Textbox(label="thought", visible=False, interactive=False)
59
+ action = gr.Textbox(label="action", visible=False, interactive=False)
60
+ original = gr.Textbox(label="original", visible=False, max_lines=5, lines=5, interactive=False)
61
+ improved = gr.Textbox(label="improved", visible=False, max_lines=5, lines=5, interactive=False)
62
+ diff = gr.HighlightedText(
63
+ label="Diff",
64
+ combine_adjacent=True,
65
+ show_legend=True,
66
+ visible=False,
67
+ max_lines=5,
68
+ lines=5,
69
+ interactive=False
70
+ ).style(color_map={"+": "green", "-": "red"})
71
 
72
+ btn.click(handler.generate, inputs=[txt_in, key], outputs=[sug, btn, thought, action, original, improved] + textboxes)
73
+ for i in textboxes:
74
+ i.click(select, inputs=[i], outputs=[thought, action, original, improved, diff])
75
 
76
+ with gr.Row():
77
+ with gr.Tab("Issue"):
78
+ gr.Textbox(show_label=False, value="https://github.com/j40903272/PaperGPT/issues", interactive=False)
79
+ with gr.Tab("Author"):
80
+ gr.JSON(show_label=False, value={'author': 'YDTsai', 'email': 'bb04902103@gmail.com', 'source': 'https://github.com/j40903272/PaperGPT'})
81
 
82
  # demo.launch(server_name="0.0.0.0", server_port=7653, share=True, enable_queue=True)
83
  demo.launch(enable_queue=True)
src/suggest.py CHANGED
@@ -52,6 +52,7 @@ class Suggest():
52
  self.max_ideas = max_ideas
53
  self.encoder = tiktoken.encoding_for_model(model)
54
  self.model = model
 
55
  with open("./sample/sample.tex", "r") as f:
56
  self.sample_content = f.read()
57
 
@@ -125,7 +126,11 @@ class Suggest():
125
  if len(ideas) >= self.max_ideas:
126
  break
127
  else:
128
- raise gr.Error(idea)
 
 
 
 
129
 
130
  logging.info('complete analysis')
131
  return ideas
@@ -149,8 +154,8 @@ class Suggest():
149
  raise gr.Error("Please provide openai key !")
150
 
151
  try:
152
- global idea_list
153
  idea_list = self.analyze(txt, openai_key, progress)
 
154
  k = min(len(idea_list), self.max_ideas)
155
 
156
  idea_buttons = [
@@ -165,7 +170,7 @@ class Suggest():
165
  gr.Textbox.update(value="", label="thought", visible=True),
166
  gr.Textbox.update(value="", label="action", visible=True),
167
  gr.Textbox.update(value="", label="original", visible=True, max_lines=5, lines=5),
168
- gr.Textbox.update(value="", label="improved", visible=True, max_lines=5, lines=5)
169
  ]
170
 
171
  return [
 
52
  self.max_ideas = max_ideas
53
  self.encoder = tiktoken.encoding_for_model(model)
54
  self.model = model
55
+ self.idea_list = []
56
  with open("./sample/sample.tex", "r") as f:
57
  self.sample_content = f.read()
58
 
 
126
  if len(ideas) >= self.max_ideas:
127
  break
128
  else:
129
+ # raise gr.Error(idea)
130
+ continue
131
+
132
+ if not ideas:
133
+ raise gr.Error('No suggestions generated.')
134
 
135
  logging.info('complete analysis')
136
  return ideas
 
154
  raise gr.Error("Please provide openai key !")
155
 
156
  try:
 
157
  idea_list = self.analyze(txt, openai_key, progress)
158
+ self.idea_list = idea_list
159
  k = min(len(idea_list), self.max_ideas)
160
 
161
  idea_buttons = [
 
170
  gr.Textbox.update(value="", label="thought", visible=True),
171
  gr.Textbox.update(value="", label="action", visible=True),
172
  gr.Textbox.update(value="", label="original", visible=True, max_lines=5, lines=5),
173
+ gr.Textbox.update(value="", label="improved", visible=True, max_lines=5, lines=5),
174
  ]
175
 
176
  return [
src/utils.py CHANGED
@@ -2,6 +2,7 @@ import json
2
  import requests
3
  import tiktoken
4
  import logging
 
5
 
6
 
7
  def json_validator(
@@ -18,33 +19,34 @@ def json_validator(
18
  return json.loads(text)
19
  except Exception:
20
 
21
- try:
22
- prompt = f"Modify the following into a valid json format:\n{text}"
23
- prompt_token_length = len(encoder.encode(prompt))
24
 
25
- data = {
26
- "model": model,
27
- "prompt": prompt,
28
- "max_tokens": 4097 - prompt_token_length - 64
29
- }
30
- headers = {
31
- "Content-Type": "application/json",
32
- "Authorization": f"Bearer {openai_key}"
33
- }
34
- for _ in range(retry):
35
- response = requests.post(
36
- 'https://api.openai.com/v1/completions',
37
- json=data,
38
- headers=headers,
39
- timeout=300
40
- )
41
- if response.status_code != 200:
42
- logging.warning(f'fetch openai chat retry: {response.text}')
43
- continue
44
- text = response.json()['choices'][0]['text']
45
- break
46
- except Exception:
47
- return response.json()['error']
 
48
 
49
  return text
50
 
@@ -77,3 +79,11 @@ def fetch_chat(
77
  return result
78
 
79
  return response.json()["error"]
 
 
 
 
 
 
 
 
 
2
  import requests
3
  import tiktoken
4
  import logging
5
+ from difflib import Differ
6
 
7
 
8
  def json_validator(
 
19
  return json.loads(text)
20
  except Exception:
21
 
22
+
23
+ prompt = f"Modify the following into a valid json format:\n{text}"
24
+ prompt_token_length = len(encoder.encode(prompt))
25
 
26
+ data = {
27
+ "model": model,
28
+ "prompt": prompt,
29
+ "max_tokens": 4097 - prompt_token_length - 64
30
+ }
31
+ headers = {
32
+ "Content-Type": "application/json",
33
+ "Authorization": f"Bearer {openai_key}"
34
+ }
35
+ for _ in range(retry):
36
+ response = requests.post(
37
+ 'https://api.openai.com/v1/completions',
38
+ json=data,
39
+ headers=headers,
40
+ timeout=300
41
+ )
42
+ if response.status_code != 200:
43
+ logging.warning(f'fetch openai chat retry: {response.text}')
44
+ continue
45
+ text = response.json()['choices'][0]['text']
46
+ break
47
+
48
+ if response.status_code != 200:
49
+ raise Exception(response.json()['error'])
50
 
51
  return text
52
 
 
79
  return result
80
 
81
  return response.json()["error"]
82
+
83
+
84
+ def diff_texts(text1: str, text2: str) -> list:
85
+ d = Differ()
86
+ return [
87
+ (token[2:], token[0] if token[0] != " " else None)
88
+ for token in d.compare(text1, text2)
89
+ ]