adowu commited on
Commit
c2eebb8
·
verified ·
1 Parent(s): eba525c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -7
main.py CHANGED
@@ -34,7 +34,7 @@ REQUEST_TIMEOUT = int(os.getenv("REQUEST_TIMEOUT", "120"))
34
  MAX_RETRIES = int(os.getenv("MAX_RETRIES", "3"))
35
  RETRY_BASE_DELAY = float(os.getenv("RETRY_BASE_DELAY", "1.5"))
36
 
37
- MAX_INPUT_TOKENS = int(os.getenv("MAX_INPUT_TOKENS", "80000")) # limit w tokenach
38
 
39
  # przybliżone przeliczenie: 1 token ~ 4 znaki
40
  AVG_CHARS_PER_TOKEN = 4
@@ -52,7 +52,7 @@ async def get_client() -> Client:
52
  global _client
53
  if _client is None:
54
  log.info("Connecting to %s", HF_SPACE_URL)
55
- _client = await asyncio.to_thread(Client, HF_SPACE_URL)
56
  log.info("Connected.")
57
  return _client
58
 
@@ -140,12 +140,9 @@ def _condense_messages(messages: list[Message], max_tokens: int) -> str:
140
  tcount = _token_count(text)
141
 
142
  if tokens_so_far + tcount > max_tokens:
143
- # nadmiar: spróbuj skrócić starsze wiadomości
144
  remaining_tokens = max_tokens - tokens_so_far
145
  if remaining_tokens <= 0:
146
- # nie mieszczą się już starsze, pomijamy starsze części
147
  continue
148
- # skracamy proporcjonalnie
149
  approx_chars = remaining_tokens * AVG_CHARS_PER_TOKEN
150
  text = text[-approx_chars:] # zachowujemy ostatnią część
151
  tcount = _token_count(text)
@@ -224,9 +221,20 @@ async def _call_with_retries(prompt: str, req: ChatCompletionRequest) -> str:
224
 
225
  async def _call_falcon_once(prompt: str, req: ChatCompletionRequest) -> str:
226
  client = await get_client()
227
- settings = {"model": req.model, "temperature": req.temperature, "max_new_tokens": req.max_tokens, "top_p": req.top_p}
 
 
 
 
 
228
 
229
- await asyncio.to_thread(client.predict, api_name="/new_chat")
 
 
 
 
 
 
230
 
231
  result = await asyncio.to_thread(
232
  client.predict,
 
34
  MAX_RETRIES = int(os.getenv("MAX_RETRIES", "3"))
35
  RETRY_BASE_DELAY = float(os.getenv("RETRY_BASE_DELAY", "1.5"))
36
 
37
+ MAX_INPUT_TOKENS = 32000 # ustawione na stałe zgodnie z prośbą
38
 
39
  # przybliżone przeliczenie: 1 token ~ 4 znaki
40
  AVG_CHARS_PER_TOKEN = 4
 
52
  global _client
53
  if _client is None:
54
  log.info("Connecting to %s", HF_SPACE_URL)
55
+ _client = await asyncio.to_thread(Client, HF_SPACE_URL, show_error=True)
56
  log.info("Connected.")
57
  return _client
58
 
 
140
  tcount = _token_count(text)
141
 
142
  if tokens_so_far + tcount > max_tokens:
 
143
  remaining_tokens = max_tokens - tokens_so_far
144
  if remaining_tokens <= 0:
 
145
  continue
 
146
  approx_chars = remaining_tokens * AVG_CHARS_PER_TOKEN
147
  text = text[-approx_chars:] # zachowujemy ostatnią część
148
  tcount = _token_count(text)
 
221
 
222
  async def _call_falcon_once(prompt: str, req: ChatCompletionRequest) -> str:
223
  client = await get_client()
224
+ settings = {
225
+ "model": req.model,
226
+ "temperature": req.temperature,
227
+ "max_new_tokens": req.max_tokens,
228
+ "top_p": req.top_p,
229
+ }
230
 
231
+ # inicjalizacja nowego chatu z promptem
232
+ await asyncio.to_thread(
233
+ client.predict,
234
+ input_value=prompt,
235
+ settings_form_value=settings,
236
+ api_name="/new_chat",
237
+ )
238
 
239
  result = await asyncio.to_thread(
240
  client.predict,