-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
36 lines (34 loc) · 1.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import openai
import json
from pathlib import Path
from flask import request, make_response
def gener8(request):
openai.api_key = os.environ.get("OPENAI_API_KEY", "Specified env var not set")
gener8_model = os.environ.get("FINE_TUNED_MODEL", "Specified env var not set")
req_prompt = request.data.decode("utf-8")
if len(req_prompt) == 0:
return build_response("Bad request!", 400)
#print(req_prompt)
# https://beta.openai.com/docs/api-reference/completions/create
response = openai.Completion.create(
model=gener8_model,
prompt=req_prompt,
temperature=0.5,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["---", "```"]
)
#print("response", response)
jsonToPython = json.loads(str(response))
#print("jsonToPython", jsonToPython)
result = "# Auto-generated by Gener8 - https://github.com/PrasadG193/Gener8\n" + jsonToPython['choices'][0]['text']
#print("result", result)
return build_response(result, 200)
def build_response(result, status_code):
response = make_response(result, status_code)
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type")
return response