Slide/backend/app.py
2024-12-09 22:26:29 +08:00

37 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
from text_analyzer import analyze_text
from ppt_generator import generate_ppt_image
import os
import logging
app = Flask(__name__)
CORS(app)
@app.route('/generate', methods=['POST'])
def generate():
try:
data = request.json
text = data.get('text')
if not text:
return jsonify({"error": "No text provided"}), 400
analyzed_text = analyze_text(text)
logging.info(f"Text analysis result: {analyzed_text}")
image_path = generate_ppt_image(analyzed_text)
logging.info(f"Generated image path: {image_path}")
# 修改这里返回图片的URL而不是路径
image_url = f'/get_image/{os.path.basename(image_path)}'
return jsonify({"image_url": image_url})
except Exception as e:
logging.error(f"Error in generate endpoint: {str(e)}", exc_info=True)
return jsonify({"error": str(e)}), 500
@app.route('/get_image/<filename>', methods=['GET'])
def get_image(filename):
return send_file(f'generated/{filename}', mimetype='image/png')
if __name__ == '__main__':
app.run(debug=True)