Spaces:
Runtime error
Runtime error
app.py
Browse filesCommunism
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio.inputs import Dropdown
|
3 |
+
import requests
|
4 |
+
|
5 |
+
"""
|
6 |
+
Use Gradio library to create a form with a textbox to store the input text.
|
7 |
+
And API_KEY textbox to store the key.
|
8 |
+
Use api.openai.com to get the response.
|
9 |
+
"""
|
10 |
+
|
11 |
+
|
12 |
+
def generate_text(input_text, api_key, temperature, max_tokens, frequency_penalty, presence_penalty, engine_id):
|
13 |
+
url = "https://api.openai.com/v1/engines/{engine_id}/completions".format(engine_id=engine_id)
|
14 |
+
data = {
|
15 |
+
"prompt": input_text,
|
16 |
+
"max_tokens": max_tokens,
|
17 |
+
"temperature": temperature,
|
18 |
+
"frequency_penalty": frequency_penalty,
|
19 |
+
"presence_penalty": presence_penalty
|
20 |
+
}
|
21 |
+
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
|
22 |
+
response = requests.post(url=url, json=data, headers=headers)
|
23 |
+
if response.status_code == 200:
|
24 |
+
return response.json()["choices"][0]["text"]
|
25 |
+
else:
|
26 |
+
return "Error: " + response.text
|
27 |
+
|
28 |
+
|
29 |
+
if __name__ == "__main__":
|
30 |
+
gr.Interface(
|
31 |
+
generate_text, # function to be called
|
32 |
+
[gr.inputs.Textbox(lines=1), gr.inputs.Textbox(lines=1), gr.inputs.Slider(minimum=0, maximum=1, step=0.01),
|
33 |
+
gr.inputs.Slider(minimum=32, maximum=500, step=1), gr.inputs.Slider(minimum=0, maximum=1, step=0.01), gr.inputs.Slider(minimum=0, maximum=1, step=0.01), gr.inputs.Dropdown(["davinci-codex", "cushman-codex"])], # input types
|
34 |
+
gr.outputs.Textbox() # output type
|