-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquart8888.py
41 lines (34 loc) · 1.18 KB
/
quart8888.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
from quart import Quart, request, jsonify
import httpx
import asyncio
import hypercorn.asyncio
import hypercorn.config
app = Quart(__name__)
async def fetch_translation(client, url, payload):
response = await client.post(url, json=payload)
response.raise_for_status()
return response.json()
@app.post("/translate")
async def translate_text():
data = await request.get_json()
if not data or not data.get('text') or not data.get('source_lang'):
return jsonify({'error': 'Bad Request'}), 400
api_url = "http://127.0.0.1:5000/translate"
api_key = ""
payload = {
"q": data['text'],
"source": "auto",
"target": "zh",
"format": "text",
"api_key": api_key,
}
async with httpx.AsyncClient() as client:
try:
response = await fetch_translation(client, api_url, payload)
return jsonify({"data": response["translatedText"],"code":200})
except httpx.RequestError as e:
return jsonify({'error': 'Internal Server Error'}), 500
if __name__ == "__main__":
config = hypercorn.config.Config()
config.bind = ["0.0.0.0:8888"]
asyncio.run(hypercorn.asyncio.serve(app, config))