reichaves commited on
Commit
a587a55
·
unverified ·
1 Parent(s): 33ca018

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -23,7 +23,6 @@ from bs4 import BeautifulSoup
23
  from langchain_core.documents import Document
24
  import time
25
  from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type
26
- from groq.error import RateLimitError
27
 
28
  # Configurar o tema para dark
29
  st.set_page_config(page_title="RAG Q&A Conversacional", layout="wide", initial_sidebar_state="expanded", page_icon="🤖", menu_items=None)
@@ -167,20 +166,30 @@ st.write("Insira uma URL e converse com o conteúdo dela - aqui é usado o model
167
  groq_api_key = st.text_input("Insira sua chave de API Groq:", type="password")
168
  huggingface_api_token = st.text_input("Insira seu token de API Hugging Face:", type="password")
169
 
170
- # Retry decorator for handling rate limit errors
171
  @retry(
172
- retry=retry_if_exception_type(RateLimitError),
173
  wait=wait_exponential(multiplier=1, min=4, max=60),
174
  stop=stop_after_attempt(5)
175
  )
176
  def rate_limited_llm_call(llm, **kwargs):
177
- return llm(**kwargs)
 
 
 
 
 
 
 
 
 
 
178
 
179
  if groq_api_key and huggingface_api_token:
180
  # Configurar o token da API do Hugging Face
181
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = huggingface_api_token
182
 
183
  # Inicializar o modelo de linguagem e embeddings
 
184
  llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.2-90b-text-preview", temperature=0)
185
  rate_limited_llm = lambda **kwargs: rate_limited_llm_call(llm, **kwargs)
186
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
@@ -205,7 +214,7 @@ if groq_api_key and huggingface_api_token:
205
  max_chars = 50000
206
  if len(text) > max_chars:
207
  text = text[:max_chars]
208
- st.warning(f"The webpage content was truncated to {max_chars} characters due to length.")
209
 
210
  # Create a Document object
211
  document = Document(page_content=text, metadata={"source": url})
@@ -216,7 +225,7 @@ if groq_api_key and huggingface_api_token:
216
  # Create FAISS vector store
217
  vectorstore = FAISS.from_documents(splits, embeddings)
218
 
219
- st.success(f"Processed {len(splits)} document chunks from the URL.")
220
 
221
  retriever = vectorstore.as_retriever()
222
 
 
23
  from langchain_core.documents import Document
24
  import time
25
  from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type
 
26
 
27
  # Configurar o tema para dark
28
  st.set_page_config(page_title="RAG Q&A Conversacional", layout="wide", initial_sidebar_state="expanded", page_icon="🤖", menu_items=None)
 
166
  groq_api_key = st.text_input("Insira sua chave de API Groq:", type="password")
167
  huggingface_api_token = st.text_input("Insira seu token de API Hugging Face:", type="password")
168
 
 
169
  @retry(
170
+ retry=retry_if_exception_type(Exception), # We'll catch all exceptions for now
171
  wait=wait_exponential(multiplier=1, min=4, max=60),
172
  stop=stop_after_attempt(5)
173
  )
174
  def rate_limited_llm_call(llm, **kwargs):
175
+ try:
176
+ return llm(**kwargs)
177
+ except Exception as e:
178
+ if "rate limit" in str(e).lower():
179
+ # This is likely a rate limit error
180
+ st.error(f"Rate limit reached. Please try again in a few moments. Error: {str(e)}")
181
+ raise e
182
+ else:
183
+ # Some other error occurred
184
+ st.error(f"An error occurred while processing your request: {str(e)}")
185
+ raise e
186
 
187
  if groq_api_key and huggingface_api_token:
188
  # Configurar o token da API do Hugging Face
189
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = huggingface_api_token
190
 
191
  # Inicializar o modelo de linguagem e embeddings
192
+ # Initialize the LLM with rate limiting
193
  llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.2-90b-text-preview", temperature=0)
194
  rate_limited_llm = lambda **kwargs: rate_limited_llm_call(llm, **kwargs)
195
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
 
214
  max_chars = 50000
215
  if len(text) > max_chars:
216
  text = text[:max_chars]
217
+ st.warning(f"O conteúdo da página da web foi truncado para {max_chars} caracteres devido ao comprimento.")
218
 
219
  # Create a Document object
220
  document = Document(page_content=text, metadata={"source": url})
 
225
  # Create FAISS vector store
226
  vectorstore = FAISS.from_documents(splits, embeddings)
227
 
228
+ st.success(f"Processado {len(splits)} pedaços de documentos (chunks) da URL.")
229
 
230
  retriever = vectorstore.as_retriever()
231