Spaces:
Build error
Build error
Initial commit
Browse files- app.py +75 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import datetime
|
| 4 |
+
|
| 5 |
+
initial_prompt = "List the following in a HTML table with an appropriate number of rows in an appropriate order, giving the table an appropriate caption. Each non-numeric cell in the table, regardless of column, contains a link whenever possible to an appropriate Wikipedia page that opens in a new tab. No other links besides Wikipedia pages are permitted. The table contains around 5 columns with appropriate attributes: "
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def set_openai_api_key(api_key, openai_api_key):
|
| 9 |
+
if api_key:
|
| 10 |
+
openai_api_key = api_key
|
| 11 |
+
return openai_api_key
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def openai_create(prompt, openai_api_key, temperature):
|
| 15 |
+
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
|
| 16 |
+
print("prompt: " + prompt)
|
| 17 |
+
print("temperature: ", temperature)
|
| 18 |
+
|
| 19 |
+
openai.api_key = openai_api_key
|
| 20 |
+
response = openai.Completion.create(
|
| 21 |
+
model="text-davinci-003",
|
| 22 |
+
prompt=prompt,
|
| 23 |
+
temperature=temperature,
|
| 24 |
+
max_tokens=2000,
|
| 25 |
+
top_p=1,
|
| 26 |
+
frequency_penalty=0,
|
| 27 |
+
presence_penalty=0
|
| 28 |
+
)
|
| 29 |
+
return response.choices[0].text
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def desc2sheet(desc, openai_api_key, temperature):
|
| 33 |
+
if not openai_api_key or openai_api_key == "":
|
| 34 |
+
return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
|
| 35 |
+
|
| 36 |
+
html = openai_create(initial_prompt + desc + '\n', openai_api_key, temperature)
|
| 37 |
+
return html
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def update_temperature(temp_slider, temp_state):
|
| 41 |
+
if temp_slider:
|
| 42 |
+
temp_state = temp_slider
|
| 43 |
+
return temp_state
|
| 44 |
+
|
| 45 |
+
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
| 46 |
+
|
| 47 |
+
with block:
|
| 48 |
+
with gr.Row():
|
| 49 |
+
temperature_slider = gr.Slider(label="GPT Temperature", value=0.0, minimum=0.0, maximum=1.0, step=0.1)
|
| 50 |
+
title = gr.Markdown("""<h3><center>GPT-3.5 Table-inator</center></h3>""")
|
| 51 |
+
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key",
|
| 52 |
+
show_label=False, lines=1, type='password')
|
| 53 |
+
|
| 54 |
+
table = gr.Markdown("")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
request = gr.Textbox(label="GPT prompt: " + initial_prompt,
|
| 57 |
+
placeholder="Ex: Computer languages")
|
| 58 |
+
|
| 59 |
+
submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
|
| 60 |
+
|
| 61 |
+
openai_api_key_state = gr.State()
|
| 62 |
+
temperature_state = gr.State(0.0)
|
| 63 |
+
|
| 64 |
+
submit.click(desc2sheet, inputs=[request, openai_api_key_state, temperature_state], outputs=[table])
|
| 65 |
+
request.submit(desc2sheet, inputs=[request, openai_api_key_state, temperature_state], outputs=[table])
|
| 66 |
+
|
| 67 |
+
openai_api_key_textbox.change(set_openai_api_key,
|
| 68 |
+
inputs=[openai_api_key_textbox, openai_api_key_state],
|
| 69 |
+
outputs=[openai_api_key_state])
|
| 70 |
+
|
| 71 |
+
temperature_slider.change(update_temperature,
|
| 72 |
+
inputs=[temperature_slider, temperature_state],
|
| 73 |
+
outputs=[temperature_state])
|
| 74 |
+
|
| 75 |
+
block.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|