42 lines
960 B
Python
42 lines
960 B
Python
from flask import Flask, render_template, request, jsonify
|
|
import dashscope
|
|
from dashscope import Generation
|
|
|
|
app = Flask(__name__)
|
|
|
|
# ==========================
|
|
# 配置通义千问 API Key
|
|
# ==========================
|
|
dashscope.api_key = "sk-27ec095dc16a49ef84536e0a6e2452bf"
|
|
|
|
|
|
def ai_rewrite(text):
|
|
prompt = f"请帮我润色以下文本,使其更加通顺自然:{text}"
|
|
|
|
response = Generation.call(
|
|
model="qwen-turbo",
|
|
prompt=prompt
|
|
)
|
|
|
|
if response.status_code == 200:
|
|
return response.output.text
|
|
else:
|
|
return "AI 接口调用失败,请稍后重试"
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
|
|
@app.route("/rewrite", methods=["POST"])
|
|
def rewrite():
|
|
data = request.json
|
|
text = data.get("text", "")
|
|
result = ai_rewrite(text)
|
|
return jsonify({"result": result})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|