gpt4 book ai didi

python - Googletrans API AttributeError

转载 作者:行者123 更新时间:2023-12-04 13:30:53 28 4
gpt4 key购买 nike

即使在 googletrans stopped working with error 'NoneType' object has no attribute 'group' 上更改 gtoken 后,我仍然收到“AttributeError: 'NoneType' object has no attribute 'group'”错误但我得到了 __init__() got an unexpected keyword argument 'client'错误代替
这是我的 main.py

from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import uvicorn
from googletrans import Translator
#init
app = FastAPI(debug=True)

templates = Jinja2Templates(directory="template")


#route
@app.get('/')
def home(request: Request):
text = request.get('text')
lang = request.get('lang')
#print('text:',text,'lang:',lang)

#connect the translator
translator=Translator()

#detect langguage
dt = translator.detect(text)
dt2 =dt.lang

#translate the text
translated = translator.translate(text, lang)
tr =translated.text

return templates.TemplateResponse({"request": request},"translates.html",{'translated':tr,'u_lang':dt2,'t_lang':lang})

#def translator(request):


if __name__=="__main__":
uvicorn.run(app,host="127.0.0.1",port=8000)
在这里我的 translate.html 在网站内执行翻译
<form action="" method="get">
<br>

<div class="form-input">
<center><label for="TextareaInput">Enter Text </label></center>
<center><textarea class="form-control" value="text" id="TextareaInput" rows="3"></textarea></center>
</div>
<div class="ui divider"></div>
<div class="form-selection">
<center><label for="languages">Choose Langguage:</label></center>
<center><select name="trans" id="languages">
<option value="en">English</option>
<option value="ms">Malay</option>
<option value="zh-cn">Mandarin</option>
<option value="ko">Korean</option>
<option value="ja">Japanese</option>
<option value="vi">Vietnamese</option>
<option value="th">Thailand</option>
</select></center>
</div>
<div class="ui divider"></div>
<div>
<center> <button class="ui button">Translate</button></center>
</div>
<div class="ui divider"></div>
<div class="form-output">
<div class="container">
<br><br>
<h1>Text succes translated {{u_lang}} to {{t_lang}}</h1>
<center>
<h1>{{translated}}</h1>
</center>
</div>
</div>
</form>
我已经撞墙了,因为这个错误不断出现

最佳答案

我遇到了同样的问题,直到我发现了模块 google_trans_new .你应该试试看:

pip install google_trans_new
对于翻译部分:
from google_trans_new import google_translator  

translator = google_translator()
translate_text = translator.translate('首先感谢我的父母他们对我的关爱',lang_tgt='en')
返回:
'First of all thank my parents for their love'
检测:
from google_trans_new import google_translator  

detector = google_translator()
detect_result = detector.detect('首先感谢我的父母他们对我的关爱')

这使
['zh-CN', 'chinese (simplified)']

关于python - Googletrans API AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65095668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com