Spaces:
Running
Running
Create api_words.py
Browse files- api_words.py +66 -0
api_words.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from vocab import get_words_from_source
|
| 3 |
+
from sentences import get_sentence, save_sentence
|
| 4 |
+
from ai_sentence import generate_sentence, MODEL_LIST
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
@app.route('/api/words', methods=['GET'])
|
| 10 |
+
def handle_words():
|
| 11 |
+
action = request.args.get('action')
|
| 12 |
+
source = request.args.get('source', 'common3000')
|
| 13 |
+
num = int(request.args.get('num', 5))
|
| 14 |
+
word = request.args.get('word')
|
| 15 |
+
model = request.args.get('model', 'gpt2')
|
| 16 |
+
|
| 17 |
+
if action == 'daily':
|
| 18 |
+
words_data = get_words_from_source(source)
|
| 19 |
+
selected_words = random.sample(words_data, 30)
|
| 20 |
+
result = []
|
| 21 |
+
for word_data in selected_words:
|
| 22 |
+
w = word_data['word']
|
| 23 |
+
phonetic = word_data['phonetic']
|
| 24 |
+
sentence_data = get_sentence(w)
|
| 25 |
+
sentence = sentence_data[2] if sentence_data else "例句暫無"
|
| 26 |
+
result.append({"word": w, "phonetic": phonetic, "sentence": sentence})
|
| 27 |
+
return jsonify({"words": result})
|
| 28 |
+
|
| 29 |
+
elif action == 'random':
|
| 30 |
+
words_data = get_words_from_source(source)
|
| 31 |
+
selected_words = random.sample(words_data, num)
|
| 32 |
+
result = []
|
| 33 |
+
for word_data in selected_words:
|
| 34 |
+
w = word_data['word']
|
| 35 |
+
phonetic = word_data['phonetic']
|
| 36 |
+
sentence_data = get_sentence(w)
|
| 37 |
+
sentence = sentence_data[2] if sentence_data else "例句暫無"
|
| 38 |
+
result.append({"word": w, "phonetic": phonetic, "sentence": sentence})
|
| 39 |
+
return jsonify({"words": result})
|
| 40 |
+
|
| 41 |
+
elif action == 'query':
|
| 42 |
+
if not word:
|
| 43 |
+
return jsonify({"error": "缺少 word 參數"}), 400
|
| 44 |
+
sentence_data = get_sentence(word)
|
| 45 |
+
if sentence_data:
|
| 46 |
+
return jsonify({"word": word, "phonetic": sentence_data[1], "sentence": sentence_data[2]})
|
| 47 |
+
else:
|
| 48 |
+
return jsonify({"word": word, "phonetic": "未知", "sentence": "例句暫無"})
|
| 49 |
+
|
| 50 |
+
elif action == 'ai_generate':
|
| 51 |
+
if not word:
|
| 52 |
+
return jsonify({"error": "缺少 word 參數"}), 400
|
| 53 |
+
if model not in MODEL_LIST:
|
| 54 |
+
return jsonify({"error": "無效的模型"}), 400
|
| 55 |
+
try:
|
| 56 |
+
sentence = generate_sentence(word, model)
|
| 57 |
+
save_sentence(word, "未知", sentence, source="ai", model=model)
|
| 58 |
+
return jsonify({"word": word, "phonetic": "未知", "sentence": sentence})
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return jsonify({"error": f"AI 生成失敗: {str(e)}"}), 500
|
| 61 |
+
|
| 62 |
+
else:
|
| 63 |
+
return jsonify({"error": "無效的 action"}), 400
|
| 64 |
+
|
| 65 |
+
if __name__ == '__main__':
|
| 66 |
+
app.run(host='0.0.0.0', port=7860)
|