Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,28 +10,27 @@ I request that you provide only the improved version of the text without any fur
|
|
10 |
|
11 |
PREFIX_PROMPT = "Please refine the following text in academic English:"
|
12 |
|
13 |
-
def submit(x, api, init_prompt, prefix_prompt, simple=False):
|
14 |
# reset api key everytime, so it won't save api unsafely...?
|
15 |
openai.api_key = api
|
16 |
# restart a new conversation.
|
17 |
if simple:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
)
|
32 |
total_tokens = results['usage']['total_tokens']
|
33 |
cost = (total_tokens / 1000) * 0.002 # in dollar
|
34 |
-
response = results['choices'][0]['message']['content']
|
35 |
return response, cost
|
36 |
|
37 |
print(f'[INFO] Starting Gradio APP...')
|
@@ -46,17 +45,18 @@ with gr.Blocks() as app:
|
|
46 |
init_prompt_input = gr.Textbox(label="Init Prompt", value=INIT_PROMPT)
|
47 |
prefix_prompt_input = gr.Textbox(label="Prefix Prompt", value=PREFIX_PROMPT)
|
48 |
|
49 |
-
|
|
|
50 |
|
51 |
with gr.Row():
|
52 |
|
53 |
-
text_input = gr.Textbox(label="Input", lines=
|
54 |
|
55 |
with gr.Column():
|
56 |
text_output = gr.Textbox(label="Output", lines=10)
|
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, simple_checkbox], outputs=[text_output, cost])
|
61 |
|
62 |
app.launch()
|
|
|
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.
|
17 |
if simple:
|
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 |
+
)
|
30 |
+
|
|
|
31 |
total_tokens = results['usage']['total_tokens']
|
32 |
cost = (total_tokens / 1000) * 0.002 # in dollar
|
33 |
+
response = results['choices'][0]['message']['content'].strip()
|
34 |
return response, cost
|
35 |
|
36 |
print(f'[INFO] Starting Gradio APP...')
|
|
|
45 |
init_prompt_input = gr.Textbox(label="Init Prompt", value=INIT_PROMPT)
|
46 |
prefix_prompt_input = gr.Textbox(label="Prefix Prompt", value=PREFIX_PROMPT)
|
47 |
|
48 |
+
temperature_input = gr.Number(value=1, label='temperature in [0, 2], the higher the more random.')
|
49 |
+
simple_checkbox = gr.Checkbox(value=False, label='simple mode (only send the prefix prompt, cheaper)')
|
50 |
|
51 |
with gr.Row():
|
52 |
|
53 |
+
text_input = gr.Textbox(label="Input", lines=14)
|
54 |
|
55 |
with gr.Column():
|
56 |
text_output = gr.Textbox(label="Output", lines=10)
|
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()
|