Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -8,6 +8,8 @@ from utils import get_text, translate_text, chat_text
|
|
8 |
from json_flatten import flatten
|
9 |
from pydantic import BaseModel
|
10 |
from typing import Optional
|
|
|
|
|
11 |
|
12 |
app = FastAPI(
|
13 |
title="DOCUMANTICAI API",
|
@@ -111,7 +113,32 @@ async def list_models():
|
|
111 |
return JSONResponse(content={"models": models})
|
112 |
|
113 |
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
async def translate_text_endpoint(text: str, target_language: str):
|
116 |
"""
|
117 |
### Endpoint Description:
|
@@ -139,12 +166,12 @@ class ChatRequest(BaseModel):
|
|
139 |
text: str
|
140 |
history: Optional[str] = None
|
141 |
|
142 |
-
|
143 |
def chat_endpoint(request: ChatRequest):
|
144 |
response_text = chat_text(request.text, request.query, request.history)
|
145 |
return {"reply": response_text}
|
146 |
|
147 |
-
@app.get("/chat")
|
148 |
def chat_endpoint(query: str, text: str, history: Optional[str] = None):
|
149 |
response_text = chat_text(text, query, history)
|
150 |
return {"reply": response_text}
|
|
|
8 |
from json_flatten import flatten
|
9 |
from pydantic import BaseModel
|
10 |
from typing import Optional
|
11 |
+
from fastapi import FastAPI, HTTPException
|
12 |
+
import json
|
13 |
|
14 |
app = FastAPI(
|
15 |
title="DOCUMANTICAI API",
|
|
|
113 |
return JSONResponse(content={"models": models})
|
114 |
|
115 |
|
116 |
+
class TranslateRequest(BaseModel):
|
117 |
+
text: str
|
118 |
+
target_language: str
|
119 |
+
|
120 |
+
@app.post("/translate")
|
121 |
+
async def translate_text_endpoint(request: TranslateRequest):
|
122 |
+
"""
|
123 |
+
### Endpoint Description:
|
124 |
+
Translate text to a specified language.
|
125 |
+
|
126 |
+
#### Request Body:
|
127 |
+
- `text`: The text to translate. (Required)
|
128 |
+
- `target_language`: The language to translate the text to. (Required)
|
129 |
+
|
130 |
+
#### Response:
|
131 |
+
- The translated text in the specified language.
|
132 |
+
"""
|
133 |
+
try:
|
134 |
+
response = translate_text(request.text, request.target_language)
|
135 |
+
response = json.loads(response)
|
136 |
+
return JSONResponse(content=response)
|
137 |
+
except Exception as e:
|
138 |
+
return JSONResponse(content={"error": str(e)}, status_code=400)
|
139 |
+
|
140 |
+
|
141 |
+
# @app.get("/translate")
|
142 |
async def translate_text_endpoint(text: str, target_language: str):
|
143 |
"""
|
144 |
### Endpoint Description:
|
|
|
166 |
text: str
|
167 |
history: Optional[str] = None
|
168 |
|
169 |
+
@app.post("/chat")
|
170 |
def chat_endpoint(request: ChatRequest):
|
171 |
response_text = chat_text(request.text, request.query, request.history)
|
172 |
return {"reply": response_text}
|
173 |
|
174 |
+
# @app.get("/chat")
|
175 |
def chat_endpoint(query: str, text: str, history: Optional[str] = None):
|
176 |
response_text = chat_text(text, query, history)
|
177 |
return {"reply": response_text}
|