Upload 7 files
Browse files- 25.jpg +0 -0
- ap.jpg +0 -0
- chunks.json +0 -0
- embeddings.npy +3 -0
- jas1.jpg +0 -0
- main.py +177 -0
- requirements.txt +7 -0
25.jpg
ADDED
![]() |
ap.jpg
ADDED
![]() |
chunks.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
embeddings.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:45d075920bc18cc67d260526c74537cbebff2bf273c4f9ea1a31bd060bbde321
|
3 |
+
size 5075072
|
jas1.jpg
ADDED
![]() |
main.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
import streamlit as st
|
6 |
+
from groq import Groq
|
7 |
+
from sentence_transformers import SentenceTransformer
|
8 |
+
|
9 |
+
from streamlit_extras.stylable_container import stylable_container
|
10 |
+
|
11 |
+
st.set_page_config(page_title='Psicopatologia Geral - Jaspers', layout='wide', page_icon='🧠')
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
with stylable_container(
|
16 |
+
key="banner",
|
17 |
+
css_styles="""
|
18 |
+
img {
|
19 |
+
width: 1700px;
|
20 |
+
height: 280px;
|
21 |
+
overflow: hidden;
|
22 |
+
position: relative;
|
23 |
+
object-fit: cover;
|
24 |
+
border-radius: 20px; /* Adiciona bordas arredondadas */
|
25 |
+
mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));
|
26 |
+
-webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)); /* For Safari */
|
27 |
+
}
|
28 |
+
""",
|
29 |
+
):
|
30 |
+
st.image("25.jpg")
|
31 |
+
|
32 |
+
|
33 |
+
# Inicialização do modelo de embedding
|
34 |
+
@st.cache_resource
|
35 |
+
def load_embedding_model():
|
36 |
+
return SentenceTransformer('all-MiniLM-L6-v2')
|
37 |
+
|
38 |
+
model = load_embedding_model()
|
39 |
+
|
40 |
+
# Inicialização do cliente Groq
|
41 |
+
client = Groq(
|
42 |
+
api_key=st.secrets["GROQ_API_KEY"],
|
43 |
+
)
|
44 |
+
|
45 |
+
# Funções auxiliares
|
46 |
+
def load_index_and_embeddings(index_file: str, embeddings_file: str):
|
47 |
+
index = faiss.read_index(index_file)
|
48 |
+
embeddings = np.load(embeddings_file)
|
49 |
+
return index, embeddings
|
50 |
+
|
51 |
+
def search(query: str, index, embeddings: np.ndarray, chunks: list, k: int = 5):
|
52 |
+
query_vector = model.encode([query])
|
53 |
+
distances, indices = index.search(query_vector.astype('float32'), k)
|
54 |
+
return [chunks[i] for i in indices[0]]
|
55 |
+
|
56 |
+
# Função para chamar a API da Groq
|
57 |
+
def query_groq(prompt, client):
|
58 |
+
chat_completion = client.chat.completions.create(
|
59 |
+
messages=[
|
60 |
+
{
|
61 |
+
"role": "user",
|
62 |
+
"content": prompt,
|
63 |
+
}
|
64 |
+
],
|
65 |
+
model="mixtral-8x7b-32768",
|
66 |
+
temperature=0.7,
|
67 |
+
max_tokens=2500,
|
68 |
+
)
|
69 |
+
return chat_completion.choices[0].message.content
|
70 |
+
|
71 |
+
# Função para converter chunk em texto
|
72 |
+
def chunk_to_text(chunk):
|
73 |
+
if isinstance(chunk, str):
|
74 |
+
return chunk
|
75 |
+
elif isinstance(chunk, list):
|
76 |
+
return " ".join(map(str, chunk))
|
77 |
+
else:
|
78 |
+
return str(chunk)
|
79 |
+
|
80 |
+
|
81 |
+
# Carregar índice, embeddings e chunks
|
82 |
+
@st.cache_resource
|
83 |
+
def load_data():
|
84 |
+
index, embeddings = load_index_and_embeddings("index.faiss", "embeddings.npy")
|
85 |
+
with open("chunks.json", "r") as f:
|
86 |
+
chunks = json.load(f)
|
87 |
+
return index, embeddings, chunks
|
88 |
+
|
89 |
+
index, embeddings, chunks = load_data()
|
90 |
+
|
91 |
+
# Interface Streamlit
|
92 |
+
st.title("Dr. Pers - Psicopatologia Geral - K. Jaspers")
|
93 |
+
|
94 |
+
# Inicializar histórico do chat
|
95 |
+
if "groq_chat_history" not in st.session_state:
|
96 |
+
st.session_state.groq_chat_history = []
|
97 |
+
|
98 |
+
# Exibição do histórico do chat
|
99 |
+
for message in st.session_state.groq_chat_history:
|
100 |
+
with st.chat_message(message["role"]):
|
101 |
+
st.markdown(message["content"])
|
102 |
+
|
103 |
+
# Input do usuário
|
104 |
+
user_message = st.chat_input("Digite sua pergunta sobre Psicopatologia Geral:")
|
105 |
+
|
106 |
+
if user_message:
|
107 |
+
# Adiciona a mensagem do usuário ao histórico
|
108 |
+
st.session_state.groq_chat_history.append({"role": "user", "content": user_message})
|
109 |
+
|
110 |
+
# Exibe a mensagem do usuário
|
111 |
+
with st.chat_message("user"):
|
112 |
+
st.markdown(user_message)
|
113 |
+
|
114 |
+
# Realiza a busca e gera a resposta
|
115 |
+
relevant_chunks = search(user_message, index, embeddings, chunks)
|
116 |
+
context = "\n".join(chunk_to_text(chunk) for chunk in relevant_chunks)
|
117 |
+
|
118 |
+
prompt = f"""Você é um velho psiquiatra, Dr. Pers, que ajudou a transcrever o livro Psicopatologia geral de Karl Jaspers.
|
119 |
+
Sua função é auxiliar jovens psiquiatras e interessados em psicopatologia a entender melhor esta grande obra.
|
120 |
+
Com base no seguinte contexto, que tem esta obra completa, responda à pergunta do usuário. Seja sempre direto
|
121 |
+
e elabore bem as respostas, porém caso o usuário queira falar sobre qualquer outro assunto, responda:
|
122 |
+
"Não tenho conhecimento concreto para tratar de outros temas senão da Psicopatologia geral." Seja sempre cortês,
|
123 |
+
e sempre dê suas respostas em português do Brasil.
|
124 |
+
|
125 |
+
Contexto:
|
126 |
+
{context}
|
127 |
+
|
128 |
+
Pergunta: {user_message}
|
129 |
+
|
130 |
+
Resposta:"""
|
131 |
+
|
132 |
+
# Gera a resposta
|
133 |
+
response = query_groq(prompt, client)
|
134 |
+
|
135 |
+
# Adiciona a resposta ao histórico
|
136 |
+
st.session_state.groq_chat_history.append({"role": "assistant", "content": response})
|
137 |
+
|
138 |
+
# Exibe a resposta
|
139 |
+
with st.chat_message("assistant"):
|
140 |
+
st.markdown(response)
|
141 |
+
|
142 |
+
# Informações adicionais
|
143 |
+
st.sidebar.title("Informações")
|
144 |
+
|
145 |
+
with stylable_container(
|
146 |
+
key="jas",
|
147 |
+
css_styles="""
|
148 |
+
img {
|
149 |
+
position: relative;
|
150 |
+
object-fit: cover;
|
151 |
+
border-radius: 18px;
|
152 |
+
}
|
153 |
+
""",
|
154 |
+
):
|
155 |
+
st.sidebar.image("jas1.jpg")
|
156 |
+
|
157 |
+
def clear_chat_history():
|
158 |
+
"""
|
159 |
+
Clears the chat history and resets the initial analysis in the session state.
|
160 |
+
This function clears the chat history and resets the initial analysis in the session state.
|
161 |
+
"""
|
162 |
+
st.session_state.groq_chat_history = []
|
163 |
+
|
164 |
+
if st.sidebar.button("Reiniciar a conversa", type="primary"):
|
165 |
+
clear_chat_history()
|
166 |
+
|
167 |
+
st.sidebar.image('ap.jpg', width=150)
|
168 |
+
st.sidebar.markdown(
|
169 |
+
"""
|
170 |
+
#### Informações:
|
171 |
+
###### - Psicopatologia Geral, I e II - Jaspers
|
172 |
+
##### - Acesso livre
|
173 |
+
### Links:
|
174 |
+
##### - [Obsidian - Dr Guilherme](http://dr-guilhermeapolinario.com) 🌎
|
175 |
+
##### - [GitHub - Dr Guilherme](http://dr-guilhermeapolinario.com) 🌎
|
176 |
+
"""
|
177 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
faiss-cpu
|
3 |
+
sentence-transformers
|
4 |
+
requests
|
5 |
+
numpy
|
6 |
+
groq
|
7 |
+
streamlit-extras
|