gera commited on
Commit
e46e576
·
1 Parent(s): 3a09f3c

added questions to first prompt to save money.

Browse files
Files changed (1) hide show
  1. app.py +43 -22
app.py CHANGED
@@ -13,12 +13,18 @@ LIMIT = 125000 # some space for answer
13
  api_key = os_getenv("OPENAI_APIKEY")
14
  client = OpenAI(api_key=api_key)
15
 
 
 
 
 
 
16
  def get_prompt(books, question = None):
17
  prompt = (
18
  f"Read the following books.\n" +
19
  f"Each book may have some pages at the beggining with data about the book, an index, or table of content, etc. " +
20
  f"Pages may have a header and/or a footer. Consider all this maybe present." +
21
- f"Please answer, for each book, all below in the suggested format, in the language of the book:\n"+
 
22
  f"**Title**: ...\n"
23
  f"**Author**: ...\n"
24
  f"**Chapter Names**: ...\n"
@@ -29,24 +35,37 @@ def get_prompt(books, question = None):
29
 
30
  return prompt
31
 
32
- def chat(message, history, files):
33
  history_openai_format = []
34
 
35
- if len(history) == 0:
36
- raise gr.Error("Primero hay que subir un libro")
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- if len(history) == 1:
39
- if message:
40
- raise gr.Error("First message must be empty")
41
- message = history[0][0]
42
  else:
43
- for human, assistant in history:
44
- if human:
45
- history_openai_format.append({"role": "user", "content": human })
46
- if assistant:
47
- history_openai_format.append({"role": "assistant", "content":assistant})
 
 
48
 
49
- history_openai_format.append({"role": "user", "content": message})
 
50
 
51
  response = client.chat.completions.create(
52
  model=MODEL,
@@ -70,7 +89,7 @@ def get_text(filename):
70
  answer = open(filename).read()
71
  return answer
72
 
73
- def files_ready(filenames):
74
  encoder = encoding = tiktoken.encoding_for_model('gpt-4-turbo')
75
  books = ''
76
  for i, name in enumerate(filenames):
@@ -80,18 +99,20 @@ def files_ready(filenames):
80
  prompt = get_prompt(books)
81
  tokens = len(encoder.encode(prompt))
82
  cost = tokens * PRICE_PER_M / 1000000 * 2 # * 2 is too much for an answer
 
83
 
84
  if tokens > LIMIT:
85
  raise gr.Error(f"Book is too long. It's {tokens} tokens long and can't be more than {LIMIT}.")
86
- return len(prompt), tokens, f"${cost}", [[prompt, None]]
87
 
88
- def files_changed(filenames):
89
  if filenames:
90
- return "-", "-"
91
  else:
92
- return 0, 0, "$0"
93
 
94
  with gr.Blocks(title="Book summarization and more") as demo:
 
95
  with gr.Row():
96
  files = gr.Files(file_types=["txt","doc","docx","pdf"] )
97
  with gr.Column():
@@ -102,12 +123,12 @@ with gr.Blocks(title="Book summarization and more") as demo:
102
  chat = gr.ChatInterface(
103
  fn=chat,
104
  title="Summarization and more",
105
- additional_inputs=[files],
106
  multimodal=False)
107
 
108
  other = gr.Button(interactive=False)
109
- files.upload(files_ready, [files], [letters, tokens, cost, chat.chatbot_state])
110
- files.change(files_changed, files, [letters, tokens, cost])
111
 
112
 
113
  auth=os_getenv("APP_USERS", "null")
 
13
  api_key = os_getenv("OPENAI_APIKEY")
14
  client = OpenAI(api_key=api_key)
15
 
16
+ def new_state():
17
+ return gr.State({
18
+ "prompt": "",
19
+ })
20
+
21
  def get_prompt(books, question = None):
22
  prompt = (
23
  f"Read the following books.\n" +
24
  f"Each book may have some pages at the beggining with data about the book, an index, or table of content, etc. " +
25
  f"Pages may have a header and/or a footer. Consider all this maybe present." +
26
+ f"For each book, please answer, all below in the suggested format and also answer all the questions at the end in detail, if present.\n"
27
+ f"Answer in the language of the book:\n"+
28
  f"**Title**: ...\n"
29
  f"**Author**: ...\n"
30
  f"**Chapter Names**: ...\n"
 
35
 
36
  return prompt
37
 
38
+ def chat(message, history, files, state):
39
  history_openai_format = []
40
 
41
+ prompt = state["prompt"]
42
+
43
+ if not message:
44
+ if len(history) > 0:
45
+ gr.Error("You sent an empty question. It's expensive, don't do it")
46
+ return ''
47
+
48
+ if not prompt:
49
+ gr.Error("First upload a book")
50
+ return ''
51
+
52
+ if (not history) and message:
53
+ prompt += f"**Questions**:{message}"
54
+ state["prompt"] = prompt
55
 
56
+ if history:
57
+ history[0] = (prompt, history[0][1])
 
 
58
  else:
59
+ message = prompt
60
+
61
+ for human, assistant in history:
62
+ if human:
63
+ history_openai_format.append({"role": "user", "content": human })
64
+ if assistant:
65
+ history_openai_format.append({"role": "assistant", "content":assistant})
66
 
67
+ if message:
68
+ history_openai_format.append({"role": "user", "content": message})
69
 
70
  response = client.chat.completions.create(
71
  model=MODEL,
 
89
  answer = open(filename).read()
90
  return answer
91
 
92
+ def files_ready(filenames, state):
93
  encoder = encoding = tiktoken.encoding_for_model('gpt-4-turbo')
94
  books = ''
95
  for i, name in enumerate(filenames):
 
99
  prompt = get_prompt(books)
100
  tokens = len(encoder.encode(prompt))
101
  cost = tokens * PRICE_PER_M / 1000000 * 2 # * 2 is too much for an answer
102
+ state["prompt"] = prompt
103
 
104
  if tokens > LIMIT:
105
  raise gr.Error(f"Book is too long. It's {tokens} tokens long and can't be more than {LIMIT}.")
106
+ return len(prompt), tokens, f"${cost}", state
107
 
108
+ def files_changed(filenames, state):
109
  if filenames:
110
+ return "-", "-", "-", state
111
  else:
112
+ return 0, 0, "$0", new_state()
113
 
114
  with gr.Blocks(title="Book summarization and more") as demo:
115
+ state = new_state()
116
  with gr.Row():
117
  files = gr.Files(file_types=["txt","doc","docx","pdf"] )
118
  with gr.Column():
 
123
  chat = gr.ChatInterface(
124
  fn=chat,
125
  title="Summarization and more",
126
+ additional_inputs=[files, state],
127
  multimodal=False)
128
 
129
  other = gr.Button(interactive=False)
130
+ files.upload(files_ready, [files, state], [letters, tokens, cost, state])
131
+ files.change(files_changed, [files, state], [letters, tokens, cost, state])
132
 
133
 
134
  auth=os_getenv("APP_USERS", "null")