-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (54 loc) · 1.69 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from fastapi import FastAPI
from openai import OpenAI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class ImageRequest(BaseModel):
image_url: str
class ImagePromptRequest(BaseModel):
prompt: str
class TranslationRequest(BaseModel):
text: str
client = OpenAI(
api_key="sk-"
)
@app.post("/process_image/")
async def process_image(request: ImageRequest):
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{"type": "image_url", "image_url": {"url": request.image_url}},
],
}
],
max_tokens=300,
)
return response.choices[0].message.content
@app.post("/generate_image/")
async def generate_image(request: ImagePromptRequest):
response = client.images.generate(
model="dall-e-3",
prompt=request.prompt,
size="1024x1024",
quality="standard",
n=1,
)
image_url = response.data[0].url
return {"image_url": image_url}
@app.post("/translate_to_korean/")
async def translate_to_korean(request: TranslationRequest):
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a translation assistant. Translate from English to Korean."},
{"role": "user", "content": request.text}
]
)
translated_text = response.choices[0].message.content
return {"translated_text": translated_text}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)