Danielbrdz commited on
Commit
f40a3fa
verified
1 Parent(s): ab4fe30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -3
app.py CHANGED
@@ -2,7 +2,9 @@ import gradio as gr
2
  import os
3
  import json
4
  import requests
5
- from datetime import datetime
 
 
6
  from supabase import create_client, Client
7
 
8
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
@@ -16,6 +18,38 @@ MAX_TOKENS = 4096
16
  TEMPERATURE = 0.7
17
  TOP_P = 0.95
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def persist_data(session_data, user_identifier=None):
20
  if not SUPABASE_URL or not SUPABASE_KEY:
21
  return
@@ -43,6 +77,13 @@ def persist_data(session_data, user_identifier=None):
43
  pass
44
 
45
  def respond(message, history: list[tuple[str, str]]):
 
 
 
 
 
 
 
46
  messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
47
 
48
  for user_msg, assistant_msg in history:
@@ -72,7 +113,8 @@ def respond(message, history: list[tuple[str, str]]):
72
  GROQ_API_URL,
73
  headers=headers,
74
  json=payload,
75
- stream=True
 
76
  )
77
  response.raise_for_status()
78
 
@@ -104,9 +146,15 @@ def respond(message, history: list[tuple[str, str]]):
104
  current_session = history + [(message, accumulated_response)]
105
  persist_data(current_session)
106
 
 
 
 
107
  except requests.exceptions.RequestException as e:
108
  print(f"Error en la solicitud a la API de Groq: {e}")
109
- yield "Lo siento, ocurri贸 un error de conexi贸n al procesar tu solicitud."
 
 
 
110
 
111
  demo = gr.ChatInterface(
112
  respond,
 
2
  import os
3
  import json
4
  import requests
5
+ import time
6
+ from datetime import datetime, timedelta
7
+ from collections import deque
8
  from supabase import create_client, Client
9
 
10
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
 
18
  TEMPERATURE = 0.7
19
  TOP_P = 0.95
20
 
21
+ MAX_REQUESTS_PER_MINUTE = 30
22
+ REQUEST_WINDOW = 60
23
+
24
+ class RateLimiter:
25
+ def __init__(self, max_requests=MAX_REQUESTS_PER_MINUTE, window=REQUEST_WINDOW):
26
+ self.max_requests = max_requests
27
+ self.window = window
28
+ self.requests = deque()
29
+
30
+ def can_make_request(self):
31
+ now = time.time()
32
+ while self.requests and self.requests[0] <= now - self.window:
33
+ self.requests.popleft()
34
+
35
+ return len(self.requests) < self.max_requests
36
+
37
+ def add_request(self):
38
+ self.requests.append(time.time())
39
+
40
+ def time_until_next_request(self):
41
+ if not self.requests:
42
+ return 0
43
+
44
+ oldest_request = self.requests[0]
45
+ time_passed = time.time() - oldest_request
46
+ if time_passed >= self.window:
47
+ return 0
48
+
49
+ return self.window - time_passed
50
+
51
+ rate_limiter = RateLimiter()
52
+
53
  def persist_data(session_data, user_identifier=None):
54
  if not SUPABASE_URL or not SUPABASE_KEY:
55
  return
 
77
  pass
78
 
79
  def respond(message, history: list[tuple[str, str]]):
80
+ if not rate_limiter.can_make_request():
81
+ wait_time = rate_limiter.time_until_next_request()
82
+ yield f"L铆mite de solicitudes alcanzado. Por favor espera {int(wait_time)} segundos antes de hacer otra consulta."
83
+ return
84
+
85
+ rate_limiter.add_request()
86
+
87
  messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
88
 
89
  for user_msg, assistant_msg in history:
 
113
  GROQ_API_URL,
114
  headers=headers,
115
  json=payload,
116
+ stream=True,
117
+ timeout=30
118
  )
119
  response.raise_for_status()
120
 
 
146
  current_session = history + [(message, accumulated_response)]
147
  persist_data(current_session)
148
 
149
+ except requests.exceptions.Timeout:
150
+ print("Timeout en la solicitud a la API")
151
+ yield "La solicitud tard贸 demasiado tiempo. Por favor int茅ntalo de nuevo."
152
  except requests.exceptions.RequestException as e:
153
  print(f"Error en la solicitud a la API de Groq: {e}")
154
+ if "429" in str(e):
155
+ yield "L铆mite de solicitudes excedido en el servidor. Por favor espera un momento antes de intentar de nuevo."
156
+ else:
157
+ yield "Lo siento, ocurri贸 un error de conexi贸n al procesar tu solicitud."
158
 
159
  demo = gr.ChatInterface(
160
  respond,