Spaces:
Runtime error
Runtime error
Update model.py
Browse files
model.py
CHANGED
@@ -1,37 +1,52 @@
|
|
1 |
-
import
|
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 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
|
3 |
+
poem = """Escriba lo que se le pide:
|
4 |
+
---
|
5 |
+
{input}
|
6 |
+
---
|
7 |
+
Este es el resultado: """
|
8 |
+
|
9 |
+
def set_openai_key(key):
|
10 |
+
"""Sets OpenAI key."""
|
11 |
+
openai.api_key = key
|
12 |
+
|
13 |
+
class GeneralModel:
|
14 |
+
def __init__(self):
|
15 |
+
print("Model Intilization--->")
|
16 |
+
# set_openai_key(API_KEY)
|
17 |
+
|
18 |
+
def query(self, prompt, myKwargs={}):
|
19 |
+
"""
|
20 |
+
wrapper for the API to save the prompt and the result
|
21 |
+
"""
|
22 |
+
|
23 |
+
# arguments to send the API
|
24 |
+
kwargs = {
|
25 |
+
"engine": "text-davinci-002",
|
26 |
+
"temperature": 0.85,
|
27 |
+
"max_tokens": 2400,
|
28 |
+
"best_of": 1,
|
29 |
+
"top_p": 1,
|
30 |
+
"frequency_penalty": 0.5,
|
31 |
+
"presence_penalty": 0.5,
|
32 |
+
"stop": ["###"],
|
33 |
+
}
|
34 |
+
|
35 |
+
|
36 |
+
for kwarg in myKwargs:
|
37 |
+
kwargs[kwarg] = myKwargs[kwarg]
|
38 |
+
|
39 |
+
|
40 |
+
r = openai.Completion.create(prompt=prompt, **kwargs)["choices"][0][
|
41 |
+
"text"
|
42 |
+
].strip()
|
43 |
+
return r
|
44 |
+
|
45 |
+
def model_prediction(self, input, api_key):
|
46 |
+
"""
|
47 |
+
wrapper for the API to save the prompt and the result
|
48 |
+
"""
|
49 |
+
# Setting the OpenAI API key got from the OpenAI dashboard
|
50 |
+
set_openai_key(api_key)
|
51 |
+
output = self.query(poem.format(input = input))
|
52 |
+
return output
|