ashawkey commited on
Commit
feaf947
1 Parent(s): a80d39e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -5,12 +5,18 @@ import openai
5
 
6
  INIT_PROMPT = """I would like to engage your services as an academic writing consultant to improve my writing.
7
  I will provide you with text that requires refinement, and you will enhance it with more academic language and sentence structures.
8
- The essence of the text should remain unaltered, including any LaTeX commands.
9
- I request that you provide only the improved version of the text without any further explanations."""
10
 
11
- PREFIX_PROMPT = "Please refine the following text in academic English:"
12
 
13
- def submit(x, api, init_prompt, prefix_prompt, temperature, simple=False):
 
 
 
 
 
 
 
14
  # reset api key everytime, so it won't save api unsafely...?
15
  openai.api_key = api
16
  # restart a new conversation.
@@ -18,12 +24,12 @@ def submit(x, api, init_prompt, prefix_prompt, temperature, simple=False):
18
  messages=[{"role": "user", "content": prefix_prompt + x.strip()},]
19
  else:
20
  messages=[
21
- # chatgpt doesn't pay much attention to system content.
22
- {"role": "user", "content": init_prompt + prefix_prompt + x.strip()},
23
  ]
24
 
25
  results = openai.ChatCompletion.create(
26
- model="gpt-3.5-turbo-0301",
27
  temperature=temperature,
28
  messages=messages,
29
  )
@@ -40,6 +46,7 @@ with gr.Blocks() as app:
40
 
41
  # allow setting API key in gui
42
  api_input = gr.Textbox(label="OPENAI_API_KEY", value=openai.api_key, lines=1)
 
43
 
44
  # allow changing prompts
45
  init_prompt_input = gr.Textbox(label="Init Prompt", value=INIT_PROMPT)
@@ -57,6 +64,6 @@ with gr.Blocks() as app:
57
  cost = gr.Number(label='cost of this query ($)')
58
 
59
  text_button = gr.Button("Submit")
60
- text_button.click(submit, inputs=[text_input, api_input, init_prompt_input, prefix_prompt_input, temperature_input, simple_checkbox], outputs=[text_output, cost])
61
 
62
  app.launch()
 
5
 
6
  INIT_PROMPT = """I would like to engage your services as an academic writing consultant to improve my writing.
7
  I will provide you with text that requires refinement, and you will enhance it with more academic language and sentence structures.
8
+ The essence of the text should remain unaltered, including any LaTeX commands."""
 
9
 
10
+ PREFIX_PROMPT = "Please provide the improved version of the following text without any further explanations: \n"
11
 
12
+ print(f'[INFO] Loading API key from env...')
13
+
14
+ openai.api_key = os.getenv("OPENAI_API_KEY")
15
+
16
+ if openai.api_key is None:
17
+ print('[WARN] OPENAI_API_KEY key not found in env')
18
+
19
+ def submit(x, api, model, init_prompt, prefix_prompt, temperature, simple=False):
20
  # reset api key everytime, so it won't save api unsafely...?
21
  openai.api_key = api
22
  # restart a new conversation.
 
24
  messages=[{"role": "user", "content": prefix_prompt + x.strip()},]
25
  else:
26
  messages=[
27
+ {"role": "system", "content": init_prompt},
28
+ {"role": "user", "content": prefix_prompt + x.strip()},
29
  ]
30
 
31
  results = openai.ChatCompletion.create(
32
+ model=model,
33
  temperature=temperature,
34
  messages=messages,
35
  )
 
46
 
47
  # allow setting API key in gui
48
  api_input = gr.Textbox(label="OPENAI_API_KEY", value=openai.api_key, lines=1)
49
+ model_input = gr.Textbox(label="Model", value="gpt-3.5-turbo", lines=1)
50
 
51
  # allow changing prompts
52
  init_prompt_input = gr.Textbox(label="Init Prompt", value=INIT_PROMPT)
 
64
  cost = gr.Number(label='cost of this query ($)')
65
 
66
  text_button = gr.Button("Submit")
67
+ text_button.click(submit, inputs=[text_input, api_input, model_input, init_prompt_input, prefix_prompt_input, temperature_input, simple_checkbox], outputs=[text_output, cost])
68
 
69
  app.launch()