Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import requests
|
| 4 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
|
| 6 |
+
import time
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def check_openai_speed(api_key, prompt):
|
| 11 |
+
headers = {
|
| 12 |
+
'Content-Type': 'application/json',
|
| 13 |
+
'Authorization': f'Bearer {openai.api_key}',
|
| 14 |
+
}
|
| 15 |
+
data = {
|
| 16 |
+
'prompt': "q",
|
| 17 |
+
'temperature': 0.5,
|
| 18 |
+
'max_tokens': 1,
|
| 19 |
+
'top_p': 1,
|
| 20 |
+
'frequency_penalty': 0,
|
| 21 |
+
'presence_penalty': 0
|
| 22 |
+
}
|
| 23 |
+
start_time = time.time()
|
| 24 |
+
response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions', headers=headers, json=data)
|
| 25 |
+
end_time = time.time()
|
| 26 |
+
response_time = end_time - start_time
|
| 27 |
+
print(f'Response time: {response_time:.2f} seconds')
|
| 28 |
+
print(f'Response status code: {response.status_code}')
|
| 29 |
+
print(f'Response text: {response.text}')
|
| 30 |
+
response_time = "Time to answer: " + f'{end_time - start_time:.2f}' + " seconds"
|
| 31 |
+
status_code = "Response code: " + str(response.status_code)
|
| 32 |
+
return status_code, response_time
|
| 33 |
+
|
| 34 |
+
title = "Status API ChatGPT"
|
| 35 |
+
css="""
|
| 36 |
+
footer {visibility: hidden}
|
| 37 |
+
.gradio-container {padding-top: 100px}
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with gr.Blocks(css=css, title=title) as demo:
|
| 42 |
+
gr.HTML("<h1>Status API ChatGPT</h1>")
|
| 43 |
+
dt_1 = gr.outputs.HTML()
|
| 44 |
+
dt_2 = gr.outputs.HTML()
|
| 45 |
+
dt =[dt_1, dt_2]
|
| 46 |
+
demo.load(check_openai_speed, inputs=None, outputs=dt)
|
| 47 |
+
|
| 48 |
+
demo.launch(share=False, debug=True )
|