Spaces:
Sleeping
Sleeping
John Doe
commited on
Commit
·
1c24c7b
1
Parent(s):
e852070
some api troubleshooting needed
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import datetime
|
| 3 |
import os
|
| 4 |
|
|
@@ -7,24 +8,18 @@ import gradio as gr
|
|
| 7 |
import koil
|
| 8 |
|
| 9 |
|
|
|
|
| 10 |
import lm.lm.openai
|
| 11 |
import lm.log.arweaveditems
|
| 12 |
|
| 13 |
-
OPENAI_API_KEY = os.
|
| 14 |
|
| 15 |
MODEL = lm.lm.openai.DEFAULT_MODEL
|
| 16 |
|
| 17 |
-
async def apredict(input):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
except:
|
| 22 |
-
if model == lm.lm.openai.DEFAULT_MODEL:
|
| 23 |
-
model = 'gpt-4'
|
| 24 |
-
api = lm.lm.openai.openai(api_key = OPENAI_API_KEY, model = MODEL)
|
| 25 |
-
else:
|
| 26 |
-
raise
|
| 27 |
-
log = lm.lm.arweaveditems.arweaveditems()
|
| 28 |
async with api, log:
|
| 29 |
response = await api(input)
|
| 30 |
addr = await log(
|
|
@@ -38,8 +33,19 @@ async def apredict(input):
|
|
| 38 |
return [addr, response]
|
| 39 |
|
| 40 |
def predict(input):
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def reset_textbox():
|
| 45 |
return gr.update(value='')
|
|
@@ -65,8 +71,8 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=7):
|
| 67 |
b1 = gr.Button().style(full_width=True)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
#inputs, top_p, temperature, top_k, repetition_penalty
|
| 72 |
#with gr.Accordion("Parameters", open=False):
|
|
@@ -77,9 +83,9 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
| 77 |
#chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 78 |
|
| 79 |
#inputs.submit( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
|
| 80 |
-
inputs.submit(predict, [inputs], [chatbot])
|
| 81 |
#b1.click( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
|
| 82 |
-
b1.click(predict, [inputs], [chatbot])
|
| 83 |
b1.click(reset_textbox, [], [inputs])
|
| 84 |
inputs.submit(reset_textbox, [], [inputs])
|
| 85 |
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import base64
|
| 3 |
import datetime
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 8 |
import koil
|
| 9 |
|
| 10 |
|
| 11 |
+
import openai
|
| 12 |
import lm.lm.openai
|
| 13 |
import lm.log.arweaveditems
|
| 14 |
|
| 15 |
+
OPENAI_API_KEY = os.environ.setdefault('OPENAI_API_KEY', base64.b64decode(b'c2stVFFuc0NHZXh4bkpGT0ZSU255UDFUM0JsYmtGSkZjTXRXTXdEVExWWkl2RUtmdXZH').decode())
|
| 16 |
|
| 17 |
MODEL = lm.lm.openai.DEFAULT_MODEL
|
| 18 |
|
| 19 |
+
async def apredict(timestamp, input):
|
| 20 |
+
import pdb; pdb.set_trace()
|
| 21 |
+
api = lm.lm.openai.openai(api_key = OPENAI_API_KEY, model = MODEL)
|
| 22 |
+
log = lm.log.arweaveditems.arweaveditems()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
async with api, log:
|
| 24 |
response = await api(input)
|
| 25 |
addr = await log(
|
|
|
|
| 33 |
return [addr, response]
|
| 34 |
|
| 35 |
def predict(input):
|
| 36 |
+
try:
|
| 37 |
+
timestamp = datetime.datetime.now().isoformat()
|
| 38 |
+
with koil.Koil() as Koil:
|
| 39 |
+
try:
|
| 40 |
+
return 'success', koil.unkoil(apredict, timestamp, input)
|
| 41 |
+
except openai.error.InvalidRequestError:
|
| 42 |
+
global MODEL
|
| 43 |
+
if MODEL == lm.lm.openai.DEFAULT_MODEL:
|
| 44 |
+
MODEL = 'gpt-4'
|
| 45 |
+
return 'success', koil.unkoil(apredict, timestamp, input)
|
| 46 |
+
raise
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f'{type(e)} {str(e)}', []
|
| 49 |
|
| 50 |
def reset_textbox():
|
| 51 |
return gr.update(value='')
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column(scale=7):
|
| 73 |
b1 = gr.Button().style(full_width=True)
|
| 74 |
+
with gr.Column(scale=3):
|
| 75 |
+
status = gr.Textbox(label="Status", )
|
| 76 |
|
| 77 |
#inputs, top_p, temperature, top_k, repetition_penalty
|
| 78 |
#with gr.Accordion("Parameters", open=False):
|
|
|
|
| 83 |
#chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 84 |
|
| 85 |
#inputs.submit( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
|
| 86 |
+
inputs.submit(predict, [inputs], [status, chatbot])
|
| 87 |
#b1.click( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
|
| 88 |
+
b1.click(predict, [inputs], [status, chatbot])
|
| 89 |
b1.click(reset_textbox, [], [inputs])
|
| 90 |
inputs.submit(reset_textbox, [], [inputs])
|
| 91 |
|