Spaces:
Sleeping
Sleeping
Commit
·
ba01e1c
1
Parent(s):
beb47a1
Update main.py
Browse files
main.py
CHANGED
@@ -1,10 +1,23 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from fastapi.responses import FileResponse
|
|
|
4 |
import openai
|
5 |
openai.api_key = "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"
|
6 |
|
7 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
10 |
|
@@ -12,12 +25,3 @@ app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
12 |
def index() -> FileResponse:
|
13 |
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
14 |
|
15 |
-
@app.get("/infer_chatgpt")
|
16 |
-
def chatgpt(input):
|
17 |
-
try:
|
18 |
-
prompt = {"role": "user", "content": input}
|
19 |
-
result = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[prompt])
|
20 |
-
output = result.get('choices')[0].get('message').get('content')
|
21 |
-
except Exception as e:
|
22 |
-
output = str(e)
|
23 |
-
return {"output": output}
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from fastapi.responses import FileResponse
|
4 |
+
from pydantic import BaseModel
|
5 |
import openai
|
6 |
openai.api_key = "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"
|
7 |
|
8 |
app = FastAPI()
|
9 |
+
class ChatInput(BaseModel):
|
10 |
+
input: str
|
11 |
+
|
12 |
+
@app.post("/infer_chatgpt")
|
13 |
+
def chatgpt(input: ChatInput):
|
14 |
+
try:
|
15 |
+
prompt = {"role": "user", "content": input.input}
|
16 |
+
result = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[prompt])
|
17 |
+
output = result.get('choices')[0].get('message').get('content')
|
18 |
+
except Exception as e:
|
19 |
+
output = {"output": str(e)}
|
20 |
+
return {"output": output}
|
21 |
|
22 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
23 |
|
|
|
25 |
def index() -> FileResponse:
|
26 |
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|