Thibault Goehringer commited on
Commit
0f33e7d
·
1 Parent(s): f8b29b1

Add fastapi endpoint

Browse files
Files changed (1) hide show
  1. main.py +15 -13
main.py CHANGED
@@ -1,8 +1,10 @@
1
  import os
2
  import openai
 
3
 
4
- openai.api_key = os.environ.get("openai_api_key")
5
 
 
 
6
 
7
  _tweet = "I'm a pretty average middle aged guy, got a solid job and had a good family but things got messy and took " \
8
  "a turn for the worst. Everyone says you need to hit rock bottom to go back up so here I am!"
@@ -12,15 +14,15 @@ def prompt(tweet, roast=True):
12
  roast_or_toast = "snarky joke" if roast else "encouragement"
13
  return f"A user tweeted this tweet:\n\n{tweet}\\n\nThe following {roast_or_toast} was given as an answer:"
14
 
15
-
16
- response = openai.Completion.create(
17
- engine="text-davinci-002",
18
- prompt=prompt(_tweet, roast=True),
19
- temperature=0.7,
20
- max_tokens=60,
21
- top_p=1,
22
- frequency_penalty=0,
23
- presence_penalty=0
24
- )
25
-
26
- print(response.choices[0].text.strip())
 
1
  import os
2
  import openai
3
+ from fastapi import FastAPI
4
 
 
5
 
6
+ openai.api_key = os.environ.get("openai_api_key")
7
+ app = FastAPI()
8
 
9
  _tweet = "I'm a pretty average middle aged guy, got a solid job and had a good family but things got messy and took " \
10
  "a turn for the worst. Everyone says you need to hit rock bottom to go back up so here I am!"
 
14
  roast_or_toast = "snarky joke" if roast else "encouragement"
15
  return f"A user tweeted this tweet:\n\n{tweet}\\n\nThe following {roast_or_toast} was given as an answer:"
16
 
17
+ @app.get("/")
18
+ def read_root():
19
+ response = openai.Completion.create(
20
+ engine="text-davinci-002",
21
+ prompt=prompt(_tweet, roast=True),
22
+ temperature=0.7,
23
+ max_tokens=60,
24
+ top_p=1,
25
+ frequency_penalty=0,
26
+ presence_penalty=0
27
+ )
28
+ return response.choices[0].text.strip()