File size: 10,959 Bytes
dc224f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import streamlit as st
import os
from pathlib import Path
from typing import List, Dict
import tempfile
from pinecone import Pinecone
from datetime import datetime

def save_uploaded_file(file) -> str:
    """Salva um arquivo temporariamente e retorna o caminho"""
    with tempfile.NamedTemporaryFile(delete=False, suffix=Path(file.name).suffix) as tmp:
        tmp.write(file.getvalue())
        return tmp.name

def format_citation(citation: dict, index: int) -> str:
    """Formata uma citação com estilo de referência acadêmica"""
    try:
        references = citation.get("references", [])
        position = citation.get("position", 0)
        citation_text = []
        
        for ref in references:
            if not ref:
                continue
                
            file_data = ref.get("file", {}) or {}
            metadata = file_data.get("metadata", {}) or {}
            
            # Usar nome original do arquivo se disponível
            file_name = (metadata.get("original_name") 
                        or file_data.get("name", "Documento"))
            pages = ref.get("pages", [])
            
            # Criar link de referência
            citation_text.append(f"[{index}] {file_name}")
            if pages:
                citation_text.append(f"Page{'s' if len(pages) > 1 else ''} {', '.join(map(str, pages))}")
        
        return " | ".join(citation_text)
    except Exception as e:
        st.error(f"Erro ao formatar citação: {str(e)}")
        return f"[{index}] Referência"

def show_references(citations: List[dict]):
    """Mostra a seção de referências no estilo acadêmico com trechos relevantes"""
    if not citations:
        return
    
    st.markdown("---")
    st.markdown("### References:")
    
    for i, citation in enumerate(citations, 1):
        try:
            references = citation.get("references", [])
            position = citation.get("position", 0)
            
            for ref in references:
                if not ref:
                    continue
                    
                file_data = ref.get("file", {}) or {}
                metadata = file_data.get("metadata", {}) or {}
                
                # Usar nome original do arquivo
                file_name = (metadata.get("original_name") 
                            or file_data.get("name", "Documento"))
                pages = ref.get("pages", [])
                
                with st.container():
                    # Cabeçalho da referência
                    col1, col2 = st.columns([1, 4])
                    with col1:
                        st.markdown(f"**[{i}]**")
                    with col2:
                        st.markdown(f"**{file_name}**")
                        if pages:
                            st.caption(f"Page {', '.join(map(str, pages))}")
                    
                    # Trecho relevante
                    if position is not None and citation.get("text"):
                        with st.expander("Ver trecho citado"):
                            st.caption("Trecho relevante:")
                            st.markdown("> " + citation["text"].strip())
                    
                    st.divider()
        except Exception as e:
            st.error(f"Erro ao mostrar referência {i}: {str(e)}")

def format_snippet(snippet: dict) -> str:
    """Formata um snippet de contexto"""
    try:
        content = snippet.get("content", "").strip()
        score = snippet.get("score", 0)
        reference = snippet.get("reference", {}) or {}
        file_data = reference.get("file", {}) or {}
        metadata = file_data.get("metadata", {}) or {}
        
        # Pegar nome original do arquivo se disponível
        file_name = (metadata.get("original_name") 
                    or file_data.get("name", "Documento"))
        
        # Pegar páginas
        pages = reference.get("pages", [])
        page_info = f"Page {', '.join(map(str, pages))}" if pages else ""
        
        # Formatar texto
        return f"""

        📄 **Fonte:** {file_name} {page_info}

        📊 **Relevância:** {score:.2%}

        

        > {content}

        """
    except Exception as e:
        return f"❌ Erro ao formatar snippet: {str(e)}"

def show_context(snippets: List[dict]):
    """Mostra snippets de contexto relevantes"""
    if not snippets:
        return
        
    with st.expander("🔍 **Contexto Relevante**"):
        try:
            st.info("ℹ️ Trechos relevantes encontrados nos documentos:")
            
            for snippet in snippets:
                if snippet:  # Verificar se o snippet é válido
                    st.markdown(format_snippet(snippet))
                    st.divider()
        except Exception as e:
            st.error(f"❌ Erro ao mostrar contexto: {str(e)}")

def main():
    st.title("💬 Chat")
    
    if 'assistant' not in st.session_state:
        st.error("❌ Nenhum assistente configurado!")
        return
    
    # Área de documentos
    with st.sidebar:
        st.subheader("📁 Documentos")
        
        # Mostrar contagem de documentos
        try:
            files = st.session_state['assistant'].get_files()
            total_files = len(files)
            
            # Mostrar status dos documentos
            if total_files > 0:
                st.info(f"📊 {total_files} documento{'s' if total_files > 1 else ''} carregado{'s' if total_files > 1 else ''}")
                
                # Mostrar detalhes em um expander
                with st.expander("Ver detalhes dos documentos"):
                    for file in files:
                        with st.container():
                            st.text(f"📄 {file['name'] if 'name' in file else 'Documento'}")
                            if 'size' in file:
                                st.caption(f"📦 {file['size']/1024:.1f} KB")
                            if 'created_on' in file:
                                st.caption(f"📅 {file['created_on']}")
                            st.divider()
            else:
                st.warning("⚠️ Nenhum documento carregado")
            
        except Exception as e:
            st.error(f"❌ Erro ao carregar documentos: {str(e)}")
            st.warning("⚠️ Nenhum documento carregado")
        
        st.info("ℹ️ Carregue documentos para permitir que o assistente os utilize nas respostas.")
        
        # Upload de arquivos
        uploaded_files = st.file_uploader(
            "Carregar documentos",
            accept_multiple_files=True,
            type=['pdf', 'txt', 'doc', 'docx']
        )
        
        if uploaded_files:
            if st.button("📤 Enviar Documentos"):
                with st.spinner("Carregando documentos..."):
                    try:
                        for uploaded_file in uploaded_files:
                            with st.status(f"Processando {uploaded_file.name}..."):
                                temp_path = save_uploaded_file(uploaded_file)
                                try:
                                    response = st.session_state['assistant'].upload_file(temp_path)
                                    st.success("✅ Arquivo processado!")
                                finally:
                                    try:
                                        os.unlink(temp_path)
                                    except:
                                        pass
                                        
                        st.success("✅ Todos os documentos foram carregados!")
                        st.rerun()
                        
                    except Exception as e:
                        st.error(f"❌ Erro: {str(e)}")

    # Interface do chat
    if "messages" not in st.session_state:
        st.session_state.messages = []

    # Mostrar mensagens anteriores
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])
            
            # Mostrar citações se existirem
            if message.get("citations"):
                with st.expander("📚 **Fontes Consultadas**"):
                    st.info("ℹ️ O assistente baseou sua resposta nos seguintes documentos:")
                    for i, citation in enumerate(message["citations"], 1):
                        st.markdown(format_citation(citation, i))

    # Input do usuário
    if prompt := st.chat_input("Digite sua mensagem..."):
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)
            
        with st.chat_message("assistant"):
            try:
                response = st.session_state['assistant'].fazer_pergunta(prompt)
                
                # Mostrar contexto relevante primeiro
                if response.get("snippets"):
                    show_context(response["snippets"])
                
                # Mostrar resposta com citações inline
                content = response["content"]
                citations = response.get("citations", [])
                
                # Processar citações e extrair trechos
                processed_citations = []
                for citation in citations:
                    # Extrair o trecho do texto original
                    position = citation.get("position", 0)
                    text_before = content[max(0, position-100):position]
                    text_after = content[position:position+100]
                    citation["text"] = f"...{text_before}**{text_after}**..."
                    processed_citations.append(citation)
                
                # Adicionar números de referência ao conteúdo
                for i, citation in enumerate(processed_citations, 1):
                    position = citation.get("position", 0)
                    content = content[:position] + f" [{i}]" + content[position:]
                
                # Mostrar conteúdo
                st.markdown(content)
                
                # Mostrar seção de referências
                if processed_citations:
                    show_references(processed_citations)
                
                # Salvar mensagem
                st.session_state.messages.append({
                    "role": "assistant",
                    "content": content,
                    "citations": processed_citations
                })
                
            except Exception as e:
                st.error(f"❌ Erro: {str(e)}")

if __name__ == "__main__":
    main()