vioott commited on
Commit
809791b
·
1 Parent(s): 17f9a3a

feat(app): adds start route

Browse files
Files changed (3) hide show
  1. app.py +50 -22
  2. templates/start.html +21 -0
  3. user_data.py +8 -6
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import google.generativeai as genai
3
- from flask import Flask, render_template, request
4
  from user_data import get_user_history
5
  from logs import save_log
6
  from book_recommendations import (
@@ -75,18 +75,43 @@ def ia_decision(user_id, history):
75
  return response.text
76
 
77
 
78
- @app.route('/recommend/<int:user_id>')
79
- def recommend(user_id):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  history = get_user_history(user_id)
81
  if not history:
82
  return "Usuário não encontrado", 404
83
- ia_response = ia_decision(user_id, history)
84
- save_log(user_id, history, ia_response)
85
- return render_template(
86
- 'recommendation.html',
87
- user_id=user_id,
88
- message=ia_response
89
- )
 
 
90
 
91
 
92
  @app.route('/chat/<int:user_id>', methods=['GET', 'POST'])
@@ -136,23 +161,26 @@ def chat(user_id):
136
 
137
  save_log(user_id, history, response)
138
 
139
- return render_template('chat.html', user_id=user_id, response=response)
 
 
 
 
 
140
 
141
 
142
- @app.route('/perfil/<int:user_id>', methods=['GET', 'POST'])
143
- def perfil(user_id):
144
  history = get_user_history(user_id)
145
  if not history:
146
  return "Usuário não encontrado", 404
147
-
148
- if request.method == 'POST':
149
- name = request.form.get('name')
150
- preferences = request.form.getlist('preferences')
151
- # aqui vamos salvar nome e preferências
152
- history['name'] = name
153
- history['preferences'] = preferences
154
-
155
- return render_template('perfil.html', user_id=user_id, history=history)
156
 
157
 
158
  if __name__ == '__main__':
 
1
  import os
2
  import google.generativeai as genai
3
+ from flask import Flask, redirect, render_template, request
4
  from user_data import get_user_history
5
  from logs import save_log
6
  from book_recommendations import (
 
75
  return response.text
76
 
77
 
78
+ @app.route('/', methods=['GET', 'POST'])
79
+ def inicio():
80
+ if request.method == 'POST':
81
+ name = request.form.get('name')
82
+ preferences = request.form.getlist('preferences')
83
+
84
+ # Simula criação de novo ID
85
+ from user_data import user_histories
86
+ new_id = max(user_histories.keys()) + 1
87
+ user_histories[new_id] = {
88
+ 'fiction': 0,
89
+ 'non_fiction': 0,
90
+ 'science': 0,
91
+ 'fantasy': 0,
92
+ 'name': name,
93
+ 'preferences': preferences
94
+ }
95
+
96
+ return redirect(f'/chat/{new_id}')
97
+
98
+ return render_template('start.html')
99
+
100
+
101
+ @app.route('/perfil/<int:user_id>', methods=['GET', 'POST'])
102
+ def perfil(user_id):
103
  history = get_user_history(user_id)
104
  if not history:
105
  return "Usuário não encontrado", 404
106
+
107
+ if request.method == 'POST':
108
+ name = request.form.get('name')
109
+ preferences = request.form.getlist('preferences')
110
+ # aqui vamos salvar nome e preferências
111
+ history['name'] = name
112
+ history['preferences'] = preferences
113
+
114
+ return render_template('profile.html', user_id=user_id, history=history)
115
 
116
 
117
  @app.route('/chat/<int:user_id>', methods=['GET', 'POST'])
 
161
 
162
  save_log(user_id, history, response)
163
 
164
+ return render_template(
165
+ 'chat.html',
166
+ user_id=user_id,
167
+ response=response,
168
+ history=history
169
+ )
170
 
171
 
172
+ @app.route('/recomendacoes/<int:user_id>')
173
+ def recommend(user_id):
174
  history = get_user_history(user_id)
175
  if not history:
176
  return "Usuário não encontrado", 404
177
+ ia_response = ia_decision(user_id, history)
178
+ save_log(user_id, history, ia_response)
179
+ return render_template(
180
+ 'recommendation.html',
181
+ user_id=user_id,
182
+ message=ia_response
183
+ )
 
 
184
 
185
 
186
  if __name__ == '__main__':
templates/start.html ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Bem-vindo!</title>
5
+ </head>
6
+ <body>
7
+ <h1>Crie seu perfil</h1>
8
+ <form method="POST">
9
+ <label for="name">Seu nome:</label><br>
10
+ <input type="text" id="name" name="name" required><br><br>
11
+
12
+ <p>Quais gêneros de livros você gosta?</p>
13
+ {% for genre in ['ficção', 'não ficção', 'ciência', 'fantasia'] %}
14
+ <input type="checkbox" id="{{ genre }}" name="preferences" value="{{ genre }}">
15
+ <label for="{{ genre }}">{{ genre.title() }}</label><br>
16
+ {% endfor %}
17
+
18
+ <br><button type="submit">Começar</button>
19
+ </form>
20
+ </body>
21
+ </html>
user_data.py CHANGED
@@ -1,11 +1,13 @@
 
 
 
 
 
 
 
 
1
  def get_user_history(user_id: int) -> dict:
2
  """
3
  Retorna o histórico de compras do usuário.
4
  """
5
- user_histories = {
6
- 1: {'fiction': 5, 'non_fiction': 2, 'science': 0, 'fantasy': 0},
7
- 2: {'fiction': 1, 'non_fiction': 4, 'science': 1, 'fantasy': 2},
8
- 3: {'fiction': 0, 'non_fiction': 1, 'science': 5, 'fantasy': 0},
9
- 4: {'fiction': 1, 'non_fiction': 0, 'science': 0, 'fantasy': 6},
10
- }
11
  return user_histories.get(user_id)
 
1
+ user_histories = {
2
+ 1: {'fiction': 5, 'non_fiction': 2, 'science': 0, 'fantasy': 0},
3
+ 2: {'fiction': 1, 'non_fiction': 4, 'science': 1, 'fantasy': 2},
4
+ 3: {'fiction': 0, 'non_fiction': 1, 'science': 5, 'fantasy': 0},
5
+ 4: {'fiction': 1, 'non_fiction': 0, 'science': 0, 'fantasy': 6},
6
+ }
7
+
8
+
9
  def get_user_history(user_id: int) -> dict:
10
  """
11
  Retorna o histórico de compras do usuário.
12
  """
 
 
 
 
 
 
13
  return user_histories.get(user_id)