-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
87 lines (59 loc) · 3.65 KB
/
server.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from flask import Flask, jsonify, request
import generator
app = Flask(__name__)
@app.route('/generate_podcast/mock', methods=['POST'])
def generate_podcast_mock():
# mock response
podcast_text = [
"Bonjour et bienvenue à notre podcast !",
"Aujourd'hui, nous parlons d'aller au supermarché.",
"Le supermarché est un endroit pratique pour faire les courses.",
"On peut acheter beaucoup de choses : des fruits, des légumes, du pain, et même des produits de nettoyage.",
"Quand on arrive au supermarché, on prend un caddie ou un panier.",
"Ensuite, on commence par choisir ce dont on a besoin.",
"Par exemple, dans le rayon des fruits, il y a des pommes, des bananes, et des oranges.",
"Dans le rayon des produits frais, on trouve du lait, du yaourt, et du fromage.",
"Il est important de regarder les prix et les promotions.",
"Après avoir tout choisi, on va à la caisse pour payer.",
"On peut payer en espèces, par carte bancaire, ou même avec une application mobile.",
"C'est pratique et rapide !",
"Et voilà, c'est terminé pour aujourd'hui.",
"Merci de nous avoir écoutés, et à bientôt pour un nouvel épisode !"
]
return jsonify({"success": True, "podcast": podcast_text})
@app.route('/generate_podcast/2', methods=['POST'])
def generate_podcast_2():
language = str(request.json['language']) # for example, fr, en, ...
language_level = str(request.json['language_level']) # for example, A1, A2, ...
topic = str(request.json['topic']) # for example: "Going to the supermarket"
# optional parameters
history = list(request.json['history']) if 'history' in request.json else []
new_words = list(request.json['new_words']) if 'new_words' in request.json else []
length = int(request.json['length']) if 'length' in request.json else None
print(f"Generating podcast in {language} on topic '{topic}' at level {language_level} with history: {history} and new words: {new_words}")
podcast_text = generator.generate(language, language_level, topic, history, new_words, length)
print(f"Generated podcast: {podcast_text}")
return jsonify({"success": True, "podcast": podcast_text})
@app.route('/define', methods=['POST'])
def define():
word = str(request.json['word'])
chunk = str(request.json['chunk'])
print(f"Defining word '{word}' in context '{chunk}'")
text = generator.define(word, chunk)
print(f"Definition: {text}")
return jsonify({"success": True, "definition": text})
@app.route('/qanda', methods=['POST'])
def q_and_a():
language = str(request.json['language']) # for example, fr, en, ...
language_level = str(request.json['language_level']) # for example, A1, A2, ...
topic = str(request.json['topic']) # for example: "Going to the supermarket"
messages = list(request.json['messages']) if 'messages' in request.json else [] # {"role": "user/assistant", "content": "..."}
# optional parameters
history = list(request.json['history']) if 'history' in request.json else []
new_words = list(request.json['new_words']) if 'new_words' in request.json else []
print(f"Generating chat response in {language} on topic '{topic}' at level {language_level} with history: {history} and new words: {new_words}. Previous messages: {messages}")
next, role = generator.q_and_a(language, language_level, topic, messages, history, new_words)
print(f"Generated response by {role}: {next}")
return jsonify({"success": True, "next_message": {"role": role, "content": next}})
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 5001, debug = True)