This repository was archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (116 loc) · 4.34 KB
/
app.py
File metadata and controls
141 lines (116 loc) · 4.34 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
import base64
import json
import os
import traceback
import openai
from fastapi import FastAPI
from pydantic import BaseModel
from dotenv import load_dotenv
import utils
load_dotenv()
app = FastAPI()
os.environ['NO_PROXY'] = 'localhost,127.0.0.1'
class TextPrompt(BaseModel):
prompt: str
class ImageBase64Prompt(BaseModel):
images: list[str]
class ImageGeneratePrompt(BaseModel):
type: str
prompt: str
client = openai.AsyncClient(
api_key=os.getenv('API_KEY'),
base_url=os.getenv('BASE_URL'),
)
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv('API_KEY')}'
}
@app.post("/api/aigc/generate/draft/text")
async def generate_draft_from_text(item: TextPrompt):
messages = [
{'role': 'system', 'content': '我将给你提供一段简要的提示,请你将这篇博文补充为现代社交网络风格的标题和内容供用户发布微博。'
'标题不超过50字,内容不超过500字。'
'如果用户输入的 prompt 不足以生成微博,则内容部分返回为null。'
'不要加入#和标签,但可以多使用emoji字符。'
'并且根据你生成的内容指定一个配图风格,Must be one of "vivid" or "natural".'
'请你以以下的JSON格式返回: {"title": 标题, "content": 内容, "style": 风格}。'},
{'role': 'user', 'content': item.prompt},
]
try:
response = await client.chat.completions.create(
model='gpt-4-turbo',
messages=messages,
response_format={'type': 'json_object'}
)
result = response.choices[0].message.content
return {
'code': 0,
'message': '',
'data': json.loads(result)
}
except Exception as e:
traceback.print_exception(e)
return {
'code': 1,
'message': '',
}
@app.post("/api/aigc/generate/draft/image")
async def generate_draft_from_image(item: ImageBase64Prompt):
messages = [
{'role': 'system', 'content': '我将给你提供一些博文中的图片,请你将这篇博文写为现代社交网络风格的标题和内容供用户发布微博。'
'标题不超过50字,内容不超过500字。'
'如果用户输入的图片不足以生成微博,则内容部分返回为null。'
'不要加入带有 # 符号的标签,但可以多使用emoji字符。'
'请你以以下的JSON格式返回: {"title": 标题, "content": 内容}。'},
{'role': 'user',
'content': [
{
'type': 'image_url',
'image_url': {
'url': f'data:image/jpeg;base64,{utils.resize_image(image)}'
}
}
for image in item.images]
},
]
completion = await client.chat.completions.create(
model='gpt-4-turbo',
messages=messages,
response_format={'type': 'json_object'},
)
result = json.loads(completion.choices[0].message.content)
if result['content']:
return {
'code': 0,
'message': '',
'data': result,
}
return {
'code': 1,
'message': '',
}
@app.post("/api/aigc/generate/image/text")
async def generate_image_from_text(item: ImageGeneratePrompt):
try:
response = await client.images.generate(
model='dall-e-3',
prompt=f'我需要你生成适合微博配图的图片,{'' if item.type == "natural" else '风格偏向日式动漫风格,'}'
f'需要生成的内容如下: {item.prompt}',
size='1024x1024',
quality='standard',
n=1,
style=item.type if item.type == 'vivid' or item.type == 'natural' else 'vivid',
response_format='url'
)
return {
'code': 0,
'message': '',
'data': response.data[0]
}
except Exception as e:
traceback.print_exception(e)
return {
'code': 1,
'message': ''
}