EstelleSkwarto commited on
Commit
7315e4e
1 Parent(s): dec799a

completed api functions

Browse files
api.py CHANGED
@@ -1,99 +1,169 @@
 
1
  import uvicorn
2
  from fastapi import FastAPI, Form, Request
3
  from fastapi.staticfiles import StaticFiles
4
  from fastapi.templating import Jinja2Templates
5
- import re
6
-
7
  from src.inference import inferenceAPI
8
  from src.inference_t5 import inferenceAPI_t5
9
 
10
 
11
- # ------ INFERENCE MODEL ------------------------------------------------------
12
- # appel de la fonction inference, adaptee pour une entree txt
13
  def summarize(text: str):
14
- if choisir_modele.var == "lstm":
15
- return " ".join(inferenceAPI(text))
16
- elif choisir_modele.var == "fineTunedT5":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  text = inferenceAPI_t5(text)
18
  return re.sub("<extra_id_0> ", "", text)
 
 
19
 
20
 
21
- # ----------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
22
 
23
 
24
- def choisir_modele(choixModele):
25
- print("ON A RECUP LE CHOIX MODELE")
26
- if choixModele == "lstm":
27
- choisir_modele.var = "lstm"
28
- elif choixModele == "fineTunedT5":
29
- choisir_modele.var = "fineTunedT5"
 
 
30
 
31
 
32
  # -------- API ---------------------------------------------------------------
33
  app = FastAPI()
34
 
35
- # static files pour envoi du css au navigateur
36
  templates = Jinja2Templates(directory="templates")
37
  app.mount("/templates", StaticFiles(directory="templates"), name="templates")
38
 
39
 
40
  @app.get("/")
41
  async def index(request: Request):
 
 
42
  return templates.TemplateResponse(
43
- "index.html.jinja", {"request": request, "current_route": "/"}
 
 
 
 
 
 
44
  )
45
 
46
 
47
  @app.get("/model")
48
  async def get_model(request: Request):
 
 
49
  return templates.TemplateResponse(
50
- "index.html.jinja", {"request": request, "current_route": "/model"}
 
 
 
 
 
 
51
  )
52
 
53
 
54
  @app.get("/predict")
55
  async def get_prediction(request: Request):
 
 
56
  return templates.TemplateResponse(
57
  "index.html.jinja", {"request": request, "current_route": "/predict"}
58
  )
59
 
60
 
61
  @app.post("/model")
62
- async def choix_model(request: Request, choixModel: str = Form(None)):
63
- print(choixModel)
64
- if not choixModel:
65
- erreur_modele = "Merci de saisir un modèle."
 
 
 
 
 
66
  return templates.TemplateResponse(
67
- "index.html.jinja", {"request": request, "text": erreur_modele}
 
 
 
 
 
 
68
  )
69
  else:
70
- choisir_modele(choixModel)
71
- print("C'est bon on utilise le modèle demandé")
72
  return templates.TemplateResponse(
73
- "index.html.jinja", {"request": request}
 
 
 
 
 
74
  )
75
 
76
 
77
- # retourner le texte, les predictions et message d'erreur si formulaire envoye
78
- # vide
79
  @app.post("/predict")
80
  async def prediction(request: Request, text: str = Form(None)):
 
 
 
81
  if not text:
82
- error = "Merci de saisir votre texte."
83
  return templates.TemplateResponse(
84
- "index.html.jinja", {"request": request, "text": error}
 
 
 
 
 
 
85
  )
86
  else:
87
  summary = summarize(text)
88
  return templates.TemplateResponse(
89
  "index.html.jinja",
90
- {"request": request, "text": text, "summary": summary},
 
 
 
 
 
 
91
  )
92
 
93
 
94
  # ------------------------------------------------------------------------------------
95
 
96
 
97
- # lancer le serveur et le recharger a chaque modification sauvegardee
98
  if __name__ == "__main__":
99
  uvicorn.run("api:app", port=8000, reload=True)
 
1
+ import re
2
  import uvicorn
3
  from fastapi import FastAPI, Form, Request
4
  from fastapi.staticfiles import StaticFiles
5
  from fastapi.templating import Jinja2Templates
 
 
6
  from src.inference import inferenceAPI
7
  from src.inference_t5 import inferenceAPI_t5
8
 
9
 
 
 
10
  def summarize(text: str):
11
+ """
12
+ Returns the summary of an input text.
13
+
14
+ Parameter
15
+ ---------
16
+ text : str
17
+ A text to summarize.
18
+
19
+ Returns
20
+ -------
21
+ :str
22
+ The summary of the input text.
23
+ """
24
+ if global_choose_model.var == "lstm":
25
+ text = " ".join(inferenceAPI(text))
26
+ return re.sub("^1|1$|<start>|<end>", "", text)
27
+ elif global_choose_model.var == "fineTunedT5":
28
  text = inferenceAPI_t5(text)
29
  return re.sub("<extra_id_0> ", "", text)
30
+ elif global_choose_model.var == "":
31
+ return "You have not chosen a model."
32
 
33
 
34
+ def global_choose_model(model_choice):
35
+ """This function allows to connect the choice of the
36
+ model and the summary function by defining global variables.
37
+ The aime is to access a variable outside of a function."""
38
+ if model_choice == "lstm":
39
+ global_choose_model.var = "lstm"
40
+ elif model_choice == "fineTunedT5":
41
+ global_choose_model.var = "fineTunedT5"
42
+ elif model_choice == " --- ":
43
+ global_choose_model.var = ""
44
 
45
 
46
+ # definition of the main elements used in the script
47
+ model_list = [
48
+ {"model": " --- ", "name": " --- "},
49
+ {"model": "lstm", "name": "LSTM"},
50
+ {"model": "fineTunedT5", "name": "Fine-tuned T5"},
51
+ ]
52
+ selected_model = " --- "
53
+ model_choice = ""
54
 
55
 
56
  # -------- API ---------------------------------------------------------------
57
  app = FastAPI()
58
 
59
+ # static files to send the css
60
  templates = Jinja2Templates(directory="templates")
61
  app.mount("/templates", StaticFiles(directory="templates"), name="templates")
62
 
63
 
64
  @app.get("/")
65
  async def index(request: Request):
66
+ """This function is used to create an endpoint for the
67
+ index page of the app."""
68
  return templates.TemplateResponse(
69
+ "index.html.jinja",
70
+ {
71
+ "request": request,
72
+ "current_route": "/",
73
+ "model_list": model_list,
74
+ "selected_model": selected_model,
75
+ },
76
  )
77
 
78
 
79
  @app.get("/model")
80
  async def get_model(request: Request):
81
+ """This function is used to create an endpoint for
82
+ the model page of the app."""
83
  return templates.TemplateResponse(
84
+ "index.html.jinja",
85
+ {
86
+ "request": request,
87
+ "current_route": "/model",
88
+ "model_list": model_list,
89
+ "selected_model": selected_model,
90
+ },
91
  )
92
 
93
 
94
  @app.get("/predict")
95
  async def get_prediction(request: Request):
96
+ """This function is used to create an endpoint for
97
+ the predict page of the app."""
98
  return templates.TemplateResponse(
99
  "index.html.jinja", {"request": request, "current_route": "/predict"}
100
  )
101
 
102
 
103
  @app.post("/model")
104
+ async def choose_model(request: Request, model_choice: str = Form(None)):
105
+ """This functions allows to retrieve the model chosen by the user. Then, it
106
+ can end to an error message if it not defined or it is sent to the
107
+ global_choose_model function which connects the user choice to the
108
+ use of a model."""
109
+ selected_model = model_choice
110
+ # print(selected_model)
111
+ if not model_choice:
112
+ model_error = "Please select a model."
113
  return templates.TemplateResponse(
114
+ "index.html.jinja",
115
+ {
116
+ "request": request,
117
+ "text": model_error,
118
+ "model_list": model_list,
119
+ "selected_model": selected_model,
120
+ },
121
  )
122
  else:
123
+ global_choose_model(model_choice)
 
124
  return templates.TemplateResponse(
125
+ "index.html.jinja",
126
+ {
127
+ "request": request,
128
+ "model_list": model_list,
129
+ "selected_model": selected_model,
130
+ },
131
  )
132
 
133
 
 
 
134
  @app.post("/predict")
135
  async def prediction(request: Request, text: str = Form(None)):
136
+ """This function allows to retrieve the input text of the user.
137
+ Then, it can end to an error message or it can be sent to
138
+ the summarize function."""
139
  if not text:
140
+ text_error = "Please enter your text."
141
  return templates.TemplateResponse(
142
+ "index.html.jinja",
143
+ {
144
+ "request": request,
145
+ "text": text_error,
146
+ "model_list": model_list,
147
+ "selected_model": selected_model,
148
+ },
149
  )
150
  else:
151
  summary = summarize(text)
152
  return templates.TemplateResponse(
153
  "index.html.jinja",
154
+ {
155
+ "request": request,
156
+ "text": text,
157
+ "summary": summary,
158
+ "model_list": model_list,
159
+ "selected_model": selected_model,
160
+ },
161
  )
162
 
163
 
164
  # ------------------------------------------------------------------------------------
165
 
166
 
167
+ # launch the server and reload it each time a change is saved
168
  if __name__ == "__main__":
169
  uvicorn.run("api:app", port=8000, reload=True)
templates/index.html.jinja CHANGED
@@ -1,5 +1,5 @@
1
  <!DOCTYPE html>
2
- <html lang="fr">
3
  <head>
4
  <title>Text summarization API</title>
5
  <meta charset="utf-8" />
@@ -8,28 +8,11 @@
8
  <script>
9
  function customReset()
10
  {
11
- document.getElementById("my_form").value = "";
12
  document.getElementById("text").value = "";
13
  document.getElementById("summary").value = "";
14
  }
15
  </script>
16
- <script>
17
- function submitBothForms()
18
- {
19
- document.getElementById("my_form").submit();
20
- document.getElementById("choixModel").submit();
21
- }
22
- </script>
23
- <script>
24
- function getValue() {
25
- var e = document.getElementById("choixModel");
26
- var value = e.value;
27
- var text = e.options[e.selectedIndex].text;
28
- return text}
29
- </script>
30
- <script type="text/javascript">
31
- document.getElementById('choixModel').value = "<?php echo $_GET['choixModel'];?>";
32
- </script>
33
  </head>
34
  <body>
35
  <div id="header">
@@ -44,22 +27,29 @@
44
  <hr/>
45
  </nav>
46
 
47
- <div class="choixModel">
48
- <form id="choixModel" method="post" action="/model">
49
  <label for="selectModel">Choose a model :</label>
50
- <select name="choixModel" class="selectModel" id="choixModel">
51
- <option value="lstm">LSTM</option>
52
- <option value="fineTunedT5" selected>Fine-tuned T5</option>
 
 
 
 
 
 
 
53
  </select>
54
  </form>
55
- <button form ="choixModel" class='search_bn' type="submit" class="btn btn-primary btn-block btn-large" rows="1" cols="50">Select model</button>
56
  </div>
57
 
58
  <div>
59
  <table>
60
  <tr>
61
  <td>
62
- <form id = "my_form" action="/predict" method="post" class="formulaire">
63
  <textarea id="text" name="text" placeholder="Enter your text here!" rows="15" cols="75">{{text}}</textarea>
64
  <input type="hidden" name="textarea_value" value="{{ text }}">
65
  </form>
@@ -71,12 +61,12 @@
71
  </table>
72
  </div>
73
  <div class="buttons">
 
74
  {% if current_route == "/" %}
75
- <button>Merci de sélectionner un modèle</button>
76
  {% else %}
77
- <!-- <button id="submit" type="submit" onclick=submitBothForms()>SUBMIT</button> -->
78
- <button form ="my_form" class='search_bn' type="submit" class="btn btn-primary btn-block btn-large" rows="1" cols="50">Go !</button>
79
- <button form ="my_form" type="button" value="Reset" onclick="customReset();">Reset</button>
80
  {% endif %}
81
  </div>
82
 
@@ -85,7 +75,7 @@
85
  <li>&copy; Untitled. All rights reserved.</li>
86
  </ul>
87
  <ul>
88
- <li>Projet mené dans le cadre des cours du master 2 TAL (Traitement Automatique des Langues)</li>
89
  <li>Lingyun GAO -- Estelle SALMON -- Eve SAUVAGE</li>
90
  </ul>
91
  </div>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
  <title>Text summarization API</title>
5
  <meta charset="utf-8" />
 
8
  <script>
9
  function customReset()
10
  {
11
+ document.getElementById("text_form").value = "";
12
  document.getElementById("text").value = "";
13
  document.getElementById("summary").value = "";
14
  }
15
  </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  </head>
17
  <body>
18
  <div id="header">
 
27
  <hr/>
28
  </nav>
29
 
30
+ <div class="model_choice">
31
+ <form id="model_choice" method="post" action="/model">
32
  <label for="selectModel">Choose a model :</label>
33
+ <select name="model_choice" class="selectModel" id="model_choice">
34
+ <!--A for jinja loop to retrieve option buttons from the api
35
+ and to keep them selected when a choice is made. -->
36
+ {% for x in model_list%}
37
+ {%if selected_model == x.model%}
38
+ <option value="{{x.model}}" selected>{{x.name}}</option>
39
+ {%else%}
40
+ <option value="{{x.model}}">{{x.name}}</option>
41
+ {%endif%}
42
+ {%endfor%}
43
  </select>
44
  </form>
45
+ <button form ="model_choice" class='search_bn' type="submit" class="btn btn-primary btn-block btn-large" rows="1" cols="50">Select model</button>
46
  </div>
47
 
48
  <div>
49
  <table>
50
  <tr>
51
  <td>
52
+ <form id = "text_form" action="/predict" method="post" class="formulaire">
53
  <textarea id="text" name="text" placeholder="Enter your text here!" rows="15" cols="75">{{text}}</textarea>
54
  <input type="hidden" name="textarea_value" value="{{ text }}">
55
  </form>
 
61
  </table>
62
  </div>
63
  <div class="buttons">
64
+ <!--A if loop to disable Go and Reset button for the index page.-->
65
  {% if current_route == "/" %}
66
+ <button>Please select a model</button>
67
  {% else %}
68
+ <button form ="text_form" class='search_bn' type="submit" class="btn btn-primary btn-block btn-large" rows="1" cols="50">Go !</button>
69
+ <button form ="text_form" type="button" value="Reset" onclick="customReset();">Reset</button>
 
70
  {% endif %}
71
  </div>
72
 
 
75
  <li>&copy; Untitled. All rights reserved.</li>
76
  </ul>
77
  <ul>
78
+ <li>University project as part of the NLP (Natural Language Processing) Master's program</li>
79
  <li>Lingyun GAO -- Estelle SALMON -- Eve SAUVAGE</li>
80
  </ul>
81
  </div>
templates/site_style/css/main.css CHANGED
@@ -471,7 +471,4 @@ textarea {
471
  margin-top: 20px;
472
  }
473
 
474
- html body div form select[name="choixModel"] {
475
- width: 150px;
476
- }
477
  }
 
471
  margin-top: 20px;
472
  }
473
 
 
 
 
474
  }