AIRider commited on
Commit
871126f
·
verified ·
1 Parent(s): 9a9e197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -16,32 +16,32 @@ models = {
16
  def get_client(model):
17
  return InferenceClient(model=model, token=hf_token)
18
 
19
- def respond(message, chat_history, system_message, max_tokens, temperature, top_p, selected_model):
20
  stop_event.clear()
21
- client = get_client(selected_model)
22
 
23
- messages = [{"role": "system", "content": system_message}]
24
- messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in chat_history for i, m in enumerate(h) if m])
25
  messages.append({"role": "user", "content": message})
26
 
27
- response = ""
28
  try:
29
- for chunk in client.chat_completion(
30
- messages,
31
- max_tokens=max_tokens,
 
32
  temperature=temperature,
33
  top_p=top_p,
34
  stream=True
35
  ):
36
  if stop_event.is_set():
37
  break
38
- if hasattr(chunk.choices[0], 'delta'):
39
- token = chunk.choices[0].delta.content
40
- else:
41
- token = chunk.choices[0].text
42
- if token:
43
- response += token
44
  yield response
 
45
  except Exception as e:
46
  yield f"오류 발생: {str(e)}"
47
 
 
16
  def get_client(model):
17
  return InferenceClient(model=model, token=hf_token)
18
 
19
+ def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
20
  stop_event.clear()
21
+ client = InferenceClient(model=selected_model, token=hf_token)
22
 
23
+ messages = [{"role": "system", "content": system_message + "\n사용자의 입력에만 직접적으로 답변하세요. 추가 질문을 생성하거나 사용자의 입력을 확장하지 마세요."}]
24
+ messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h) if m])
25
  messages.append({"role": "user", "content": message})
26
 
 
27
  try:
28
+ response = ""
29
+ for chunk in client.text_generation(
30
+ prompt="\n".join([f"{m['role']}: {m['content']}" for m in messages]),
31
+ max_new_tokens=max_tokens,
32
  temperature=temperature,
33
  top_p=top_p,
34
  stream=True
35
  ):
36
  if stop_event.is_set():
37
  break
38
+ if chunk:
39
+ response += chunk
40
+ # 응답의 시작 부분에서 사용자 입력의 반복을 제거
41
+ if response.startswith(message):
42
+ response = response[len(message):].lstrip()
 
43
  yield response
44
+
45
  except Exception as e:
46
  yield f"오류 발생: {str(e)}"
47