Spaces:
Sleeping
Sleeping
Commit
·
7ba18ce
1
Parent(s):
7acc0b8
sync github
Browse files
app.py
CHANGED
@@ -16,9 +16,28 @@ from llama_index.vector_stores.chroma import ChromaVectorStore
|
|
16 |
import chromadb
|
17 |
import nest_asyncio
|
18 |
|
19 |
-
|
20 |
-
|
21 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Lista de pastas que precisam ser criadas
|
24 |
pastas = ["bm25_retriever", "chat_store", "chroma_db", "documentos"]
|
@@ -35,7 +54,24 @@ for pasta in pastas:
|
|
35 |
|
36 |
# Configuração do Streamlit
|
37 |
st.sidebar.title("Configuração de LLM")
|
38 |
-
sidebar_option = st.sidebar.radio("Selecione o LLM", ["Ollama", "OpenAI"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
if sidebar_option == "Ollama":
|
41 |
Settings.llm = Ollama(model="llama3.2:latest", request_timeout=500.0, num_gpu=1)
|
@@ -43,9 +79,50 @@ if sidebar_option == "Ollama":
|
|
43 |
elif sidebar_option == "OpenAI":
|
44 |
from llama_index.llms.openai import OpenAI
|
45 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
46 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
47 |
Settings.llm = OpenAI(model="gpt-3.5-turbo")
|
48 |
Settings.embed_model = OpenAIEmbedding(model_name="text-embedding-ada-002")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
else:
|
50 |
raise Exception("Opção de LLM inválida!")
|
51 |
|
@@ -139,8 +216,8 @@ else:
|
|
139 |
chat_store.persist(persist_path=chat_store_path)
|
140 |
|
141 |
# Interface do Chatbot
|
142 |
-
st.title("Chatbot
|
143 |
-
st.write("Este chatbot
|
144 |
if "chat_history" not in st.session_state:
|
145 |
st.session_state.chat_history = []
|
146 |
|
|
|
16 |
import chromadb
|
17 |
import nest_asyncio
|
18 |
|
|
|
|
|
19 |
import os
|
20 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
21 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
22 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
23 |
+
from llama_index.core import Settings
|
24 |
+
from typing import List, Optional
|
25 |
+
from llama_index.core import PromptTemplate
|
26 |
+
import torch
|
27 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
28 |
+
import huggingface_hub
|
29 |
+
|
30 |
+
import logging
|
31 |
+
import sys
|
32 |
+
from PIL import Image
|
33 |
+
|
34 |
+
#Token do huggingface
|
35 |
+
HF_TOKEN: Optional[str] = os.getenv("HF_TOKEN")
|
36 |
+
huggingface_hub.login(HF_TOKEN)
|
37 |
+
#Configuração da imagem da aba
|
38 |
+
|
39 |
+
im = Image.open("./pngegg.png")
|
40 |
+
st.set_page_config(page_title = "Chatbot Carômetro", page_icon=im, layout = "wide")
|
41 |
|
42 |
# Lista de pastas que precisam ser criadas
|
43 |
pastas = ["bm25_retriever", "chat_store", "chroma_db", "documentos"]
|
|
|
54 |
|
55 |
# Configuração do Streamlit
|
56 |
st.sidebar.title("Configuração de LLM")
|
57 |
+
sidebar_option = st.sidebar.radio("Selecione o LLM", ["Ollama", "OpenAI", "HF Local"])
|
58 |
+
# logo_url = 'app\logos\logo-sicoob.jpg'
|
59 |
+
# st.sidebar.image(logo_url)
|
60 |
+
import base64
|
61 |
+
|
62 |
+
#Configuração da imagem da sidebar
|
63 |
+
with open("sicoob-logo.png", "rb") as f:
|
64 |
+
data = base64.b64encode(f.read()).decode("utf-8")
|
65 |
+
|
66 |
+
st.sidebar.markdown(
|
67 |
+
f"""
|
68 |
+
<div style="display:table;margin-top:-80%;margin-left:0%;">
|
69 |
+
<img src="data:image/png;base64,{data}" width="250" height="70">
|
70 |
+
</div>
|
71 |
+
""",
|
72 |
+
unsafe_allow_html=True,
|
73 |
+
)
|
74 |
+
|
75 |
|
76 |
if sidebar_option == "Ollama":
|
77 |
Settings.llm = Ollama(model="llama3.2:latest", request_timeout=500.0, num_gpu=1)
|
|
|
79 |
elif sidebar_option == "OpenAI":
|
80 |
from llama_index.llms.openai import OpenAI
|
81 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
|
|
82 |
Settings.llm = OpenAI(model="gpt-3.5-turbo")
|
83 |
Settings.embed_model = OpenAIEmbedding(model_name="text-embedding-ada-002")
|
84 |
+
elif sidebar_option == 'HF Local':
|
85 |
+
|
86 |
+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
87 |
+
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
88 |
+
|
89 |
+
query_wrapper_prompt = PromptTemplate(
|
90 |
+
"Você é um assistente virtual capaz de interagir normalmente, além de"
|
91 |
+
" fornecer informações sobre organogramas e listar funcionários."
|
92 |
+
" Aqui estão os documentos relevantes para o contexto:\n"
|
93 |
+
"{context_str}"
|
94 |
+
"\nInstrução: Use o histórico da conversa anterior, ou o contexto acima, para responder."
|
95 |
+
)
|
96 |
+
#Embedding do huggingface
|
97 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
98 |
+
model_name="BAAI/bge-small-en-v1.5"
|
99 |
+
)
|
100 |
+
|
101 |
+
#Carregamento do modelo local, descomentar o modelo desejado
|
102 |
+
llm = HuggingFaceLLM(
|
103 |
+
# context_window=2048,
|
104 |
+
# max_new_tokens=256,
|
105 |
+
# generate_kwargs={"do_sample": False},
|
106 |
+
# query_wrapper_prompt=query_wrapper_prompt,
|
107 |
+
#model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
108 |
+
#model_name="Qwen/Qwen2.5-14B-Instruct",
|
109 |
+
# model_name="meta-llama/Llama-3.2-3B",
|
110 |
+
#model_name="HuggingFaceH4/zephyr-7b-beta",
|
111 |
+
# model_name="meta-llama/Meta-Llama-3-8B",
|
112 |
+
#model_name="numind/NuExtract-1.5",
|
113 |
+
model_name="meta-llama/Llama-3.1-8B",
|
114 |
+
|
115 |
+
tokenizer_name="meta-llama/Llama-3.1-8B",
|
116 |
+
device_map="auto",
|
117 |
+
# tokenizer_kwargs={"max_length": 2048},
|
118 |
+
# uncomment this if using CUDA to reduce memory usage
|
119 |
+
model_kwargs={"torch_dtype": torch.float16},
|
120 |
+
)
|
121 |
+
|
122 |
+
|
123 |
+
# Settings.chunk_size = 512
|
124 |
+
Settings.llm = llm
|
125 |
+
|
126 |
else:
|
127 |
raise Exception("Opção de LLM inválida!")
|
128 |
|
|
|
216 |
chat_store.persist(persist_path=chat_store_path)
|
217 |
|
218 |
# Interface do Chatbot
|
219 |
+
st.title("Chatbot Carômetro")
|
220 |
+
st.write("Este chatbot pode te ajudar a conseguir informações relevantes sobre os carômetros da Sicoob.")
|
221 |
if "chat_history" not in st.session_state:
|
222 |
st.session_state.chat_history = []
|
223 |
|