Update main.py
Browse files
main.py
CHANGED
@@ -1,29 +1,46 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from fastapi.responses import StreamingResponse, JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
from typing import List, Optional
|
5 |
import time
|
6 |
import json
|
|
|
7 |
import httpx
|
|
|
|
|
|
|
|
|
8 |
from models import AVAILABLE_MODELS, MODEL_ALIASES
|
9 |
|
|
|
|
|
|
|
10 |
app = FastAPI()
|
11 |
|
|
|
12 |
def unix_id():
|
13 |
return str(int(time.time() * 1000))
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
class Message(BaseModel):
|
16 |
role: str
|
17 |
content: str
|
18 |
|
|
|
19 |
class ChatRequest(BaseModel):
|
20 |
messages: List[Message]
|
21 |
model: str
|
22 |
stream: Optional[bool] = False
|
23 |
|
24 |
-
@app.get("/v1/models")
|
25 |
-
async def list_models():
|
26 |
-
return {"object": "list", "data": AVAILABLE_MODELS}
|
27 |
|
28 |
@app.post("/v1/chat/completions")
|
29 |
async def chat_completion(request: ChatRequest):
|
@@ -34,7 +51,7 @@ async def chat_completion(request: ChatRequest):
|
|
34 |
'content-type': 'application/json',
|
35 |
'origin': 'https://www.chatwithmono.xyz',
|
36 |
'referer': 'https://www.chatwithmono.xyz/',
|
37 |
-
'user-agent': 'Mozilla/5.0
|
38 |
}
|
39 |
|
40 |
payload = {
|
@@ -123,3 +140,45 @@ async def chat_completion(request: ChatRequest):
|
|
123 |
"total_tokens": usage_info.get("promptTokens", 0) + usage_info.get("completionTokens", 0),
|
124 |
}
|
125 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
from fastapi.responses import StreamingResponse, JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
from typing import List, Optional
|
5 |
import time
|
6 |
import json
|
7 |
+
import os
|
8 |
import httpx
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
from models import AVAILABLE_MODELS, MODEL_ALIASES
|
14 |
|
15 |
+
# Require IMAGE_API_URL strictly from environment
|
16 |
+
IMAGE_API_URL = os.environ["IMAGE_API_URL"]
|
17 |
+
|
18 |
app = FastAPI()
|
19 |
|
20 |
+
|
21 |
def unix_id():
|
22 |
return str(int(time.time() * 1000))
|
23 |
|
24 |
+
|
25 |
+
# ==== Models ====
|
26 |
+
|
27 |
+
@app.get("/v1/models")
|
28 |
+
async def list_models():
|
29 |
+
return {"object": "list", "data": AVAILABLE_MODELS}
|
30 |
+
|
31 |
+
|
32 |
+
# ==== Chat Completion ====
|
33 |
+
|
34 |
class Message(BaseModel):
|
35 |
role: str
|
36 |
content: str
|
37 |
|
38 |
+
|
39 |
class ChatRequest(BaseModel):
|
40 |
messages: List[Message]
|
41 |
model: str
|
42 |
stream: Optional[bool] = False
|
43 |
|
|
|
|
|
|
|
44 |
|
45 |
@app.post("/v1/chat/completions")
|
46 |
async def chat_completion(request: ChatRequest):
|
|
|
51 |
'content-type': 'application/json',
|
52 |
'origin': 'https://www.chatwithmono.xyz',
|
53 |
'referer': 'https://www.chatwithmono.xyz/',
|
54 |
+
'user-agent': 'Mozilla/5.0',
|
55 |
}
|
56 |
|
57 |
payload = {
|
|
|
140 |
"total_tokens": usage_info.get("promptTokens", 0) + usage_info.get("completionTokens", 0),
|
141 |
}
|
142 |
})
|
143 |
+
|
144 |
+
|
145 |
+
# ==== Image Generation ====
|
146 |
+
|
147 |
+
class ImageGenerationRequest(BaseModel):
|
148 |
+
prompt: str
|
149 |
+
aspect_ratio: Optional[str] = "1:1"
|
150 |
+
n: Optional[int] = 1
|
151 |
+
user: Optional[str] = None
|
152 |
+
|
153 |
+
|
154 |
+
@app.post("/v1/images/generations")
|
155 |
+
async def generate_images(request: ImageGenerationRequest):
|
156 |
+
results = []
|
157 |
+
|
158 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
159 |
+
for _ in range(request.n):
|
160 |
+
params = {
|
161 |
+
"prompt": request.prompt,
|
162 |
+
"aspect_ratio": request.aspect_ratio,
|
163 |
+
"link": "typegpt.net"
|
164 |
+
}
|
165 |
+
|
166 |
+
resp = await client.get(IMAGE_API_URL, params=params)
|
167 |
+
|
168 |
+
if resp.status_code != 200:
|
169 |
+
return JSONResponse(
|
170 |
+
status_code=502,
|
171 |
+
content={"error": "Image generation failed", "details": resp.text}
|
172 |
+
)
|
173 |
+
|
174 |
+
data = resp.json()
|
175 |
+
results.append({
|
176 |
+
"url": data.get("image_link"),
|
177 |
+
"b64_json": data.get("base64_output"),
|
178 |
+
"retries": data.get("attempt")
|
179 |
+
})
|
180 |
+
|
181 |
+
return {
|
182 |
+
"created": int(time.time()),
|
183 |
+
"data": results
|
184 |
+
}
|