giseldo commited on
Commit
a5e365d
·
verified ·
1 Parent(s): 01ec90c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -17
app.py CHANGED
@@ -5,6 +5,58 @@ import markdown
5
  import tempfile
6
  import os
7
  import io
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  groqllm = LLM(model="groq/llama-3.3-70b-versatile")
10
 
@@ -67,20 +119,118 @@ def executar_equipe_interface(disciplina, assunto, topicos_str):
67
 
68
  saida_final = f"{saida1}\n\n{saida2}\n\n{saida3}"
69
 
70
- return saida_final
71
-
72
- iface = gr.Interface(
73
- fn=executar_equipe_interface,
74
- inputs=[
75
- gr.Textbox(label="Disciplina", value="Matemática"),
76
- gr.Textbox(label="Assunto", value="Funções"),
77
- gr.Textbox(label="Tópicos (separados por vírgula)", value="Função quadrática, Função exponencial, Função logarítmica")
78
- ],
79
- outputs=[
80
- gr.Markdown(label="Material Completo (Markdown)")
81
- ],
82
- title="Interface - Material de Estudos",
83
- description="Interface para gerar material de estudo."
84
- )
85
-
86
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import tempfile
6
  import os
7
  import io
8
+ import hashlib
9
+
10
+ # Simple user database - In a production system, use a secure database
11
+ USER_DATABASE = {
12
+ "admin": "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918", # admin
13
+ "user": "04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb", # user123
14
+ "test": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # test
15
+ }
16
+
17
+ # Password hashing function
18
+ def hash_password(password):
19
+ return hashlib.sha256(password.encode()).hexdigest()
20
+
21
+ # Login function
22
+ def login(username, password):
23
+ if username in USER_DATABASE and USER_DATABASE[username] == hash_password(password):
24
+ return True, f"Bem-vindo, {username}!"
25
+ else:
26
+ return False, "Credenciais inválidas. Tente novamente."
27
+
28
+ # Function to create PDF from markdown
29
+ def markdown_to_pdf(markdown_text):
30
+ """
31
+ Convert markdown text to PDF content
32
+ """
33
+ # Convert markdown to HTML
34
+ html_text = markdown.markdown(markdown_text)
35
+
36
+ # Add some basic styling
37
+ styled_html = f"""
38
+ <html>
39
+ <head>
40
+ <style>
41
+ body {{ font-family: Arial, sans-serif; margin: 20px; }}
42
+ h1 {{ color: #2c3e50; }}
43
+ h2 {{ color: #3498db; }}
44
+ h3 {{ color: #2980b9; }}
45
+ a {{ color: #3498db; }}
46
+ </style>
47
+ </head>
48
+ <body>
49
+ {html_text}
50
+ </body>
51
+ </html>
52
+ """
53
+
54
+ # Create PDF from HTML content
55
+ pdf_output = io.BytesIO()
56
+ pisa.CreatePDF(styled_html, dest=pdf_output)
57
+ pdf_output.seek(0)
58
+
59
+ return pdf_output
60
 
61
  groqllm = LLM(model="groq/llama-3.3-70b-versatile")
62
 
 
119
 
120
  saida_final = f"{saida1}\n\n{saida2}\n\n{saida3}"
121
 
122
+ # Generate PDF from the output
123
+ pdf_output = markdown_to_pdf(saida_final)
124
+
125
+ # Save the PDF to a temporary file
126
+ temp_dir = tempfile.gettempdir()
127
+ pdf_filename = f"material_estudo_{hash_password(saida_final)[:8]}.pdf"
128
+ pdf_path = os.path.join(temp_dir, pdf_filename)
129
+
130
+ with open(pdf_path, 'wb') as f:
131
+ f.write(pdf_output.getbuffer())
132
+
133
+ return saida_final, pdf_path
134
+
135
+ # Main application function with both login and main interfaces
136
+ def main_app():
137
+ with gr.Blocks(theme="soft") as app:
138
+ # Authentication state
139
+ is_logged_in = gr.State(False)
140
+ current_user = gr.State("")
141
+
142
+ # Login Interface
143
+ with gr.Group(visible=True) as login_block:
144
+ gr.Markdown("# Sistema de Material de Estudo")
145
+ gr.Markdown("Por favor, faça login para continuar")
146
+ username_input = gr.Textbox(label="Usuário")
147
+ password_input = gr.Textbox(label="Senha", type="password")
148
+ login_button = gr.Button("Login")
149
+ login_message = gr.Textbox(label="Mensagem", interactive=False)
150
+
151
+ # Login credentials info (for demo purposes)
152
+ gr.Markdown("""
153
+ ### Credenciais para teste:
154
+ - Usuário: admin, Senha: admin
155
+ - Usuário: user, Senha: user123
156
+ - Usuário: test, Senha: test
157
+ """)
158
+
159
+ # Main Application Interface
160
+ with gr.Group(visible=False) as main_block:
161
+ gr.Markdown("# Gerador de Material de Estudos")
162
+ user_info = gr.Markdown("Bem-vindo!")
163
+ logout_button = gr.Button("Logout")
164
+
165
+ with gr.Row():
166
+ disciplina_input = gr.Textbox(label="Disciplina", value="Matemática")
167
+ assunto_input = gr.Textbox(label="Assunto", value="Funções")
168
+ topicos_input = gr.Textbox(label="Tópicos (separados por vírgula)",
169
+ value="Função quadrática, Função exponencial, Função logarítmica")
170
+
171
+ generate_button = gr.Button("Gerar Material de Estudo")
172
+
173
+ with gr.Row():
174
+ markdown_output = gr.Markdown(label="Material Completo")
175
+ pdf_output = gr.File(label="Download PDF")
176
+
177
+ # Login function
178
+ def login_fn(username, password):
179
+ success, message = login(username, password)
180
+ if success:
181
+ return {
182
+ login_block: gr.update(visible=False),
183
+ main_block: gr.update(visible=True),
184
+ login_message: message,
185
+ user_info: f"### Usuário: {username}",
186
+ is_logged_in: True,
187
+ current_user: username
188
+ }
189
+ else:
190
+ return {
191
+ login_message: message,
192
+ is_logged_in: False
193
+ }
194
+
195
+ # Logout function
196
+ def logout_fn():
197
+ return {
198
+ login_block: gr.update(visible=True),
199
+ main_block: gr.update(visible=False),
200
+ username_input: "",
201
+ password_input: "",
202
+ login_message: "",
203
+ is_logged_in: False,
204
+ current_user: ""
205
+ }
206
+
207
+ # Generate material function
208
+ def generate_material(disciplina, assunto, topicos):
209
+ markdown, pdf = executar_equipe_interface(disciplina, assunto, topicos)
210
+ return markdown, pdf
211
+
212
+ # Connect buttons to functions
213
+ login_button.click(
214
+ login_fn,
215
+ inputs=[username_input, password_input],
216
+ outputs=[login_block, main_block, login_message, user_info, is_logged_in, current_user]
217
+ )
218
+
219
+ logout_button.click(
220
+ logout_fn,
221
+ outputs=[login_block, main_block, username_input, password_input,
222
+ login_message, is_logged_in, current_user]
223
+ )
224
+
225
+ generate_button.click(
226
+ generate_material,
227
+ inputs=[disciplina_input, assunto_input, topicos_input],
228
+ outputs=[markdown_output, pdf_output]
229
+ )
230
+
231
+ return app
232
+
233
+ # Launch the application
234
+ if __name__ == "__main__":
235
+ app = main_app()
236
+ app.launch()