AIRider commited on
Commit
a5db718
·
verified ·
1 Parent(s): c2e4a2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -17
app.py CHANGED
@@ -5,6 +5,43 @@ import os
5
 
6
  hf_token = os.getenv("HF_TOKEN")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
9
  try:
10
  client = InferenceClient(model=selected_model, token=hf_token)
@@ -18,28 +55,17 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, se
18
  messages.append({"role": "user", "content": message})
19
 
20
  response = ""
21
- for message in client.chat_completion(
22
- messages,
23
- max_tokens=max_tokens,
24
- stream=True,
25
- temperature=temperature,
26
- top_p=top_p,
27
- ):
28
- if hasattr(message.choices[0], 'delta'):
29
- token = message.choices[0].delta.content
30
- else:
31
- token = message.choices[0].text # 일부 모델은 'text' 속성을 사용할 수 있습니다.
32
-
33
- if token is not None:
34
- response += token
35
- yield response
36
-
37
- if not response: # 응답이 비어있는 경우
38
  yield "모델이 응답을 생성하지 못했습니다. 다른 입력이나 모델을 시도해보세요."
39
  except Exception as e:
40
  error_msg = f"오류 발생: {str(e)}\n\n상세 오류:\n{traceback.format_exc()}"
41
  yield error_msg
42
 
 
43
  models = {
44
  "deepseek-ai/DeepSeek-Coder-V2-Instruct": "DeepSeek-Coder-V2-Instruct",
45
  "CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus",
 
5
 
6
  hf_token = os.getenv("HF_TOKEN")
7
 
8
+ def get_model_response(client, messages, max_tokens, temperature, top_p, model_name):
9
+ prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages])
10
+
11
+ try:
12
+ # 먼저 chat_completion 시도
13
+ response = client.chat_completion(
14
+ messages,
15
+ max_tokens=max_tokens,
16
+ temperature=temperature,
17
+ top_p=top_p,
18
+ stream=True
19
+ )
20
+ for message in response:
21
+ if hasattr(message.choices[0], 'delta'):
22
+ token = message.choices[0].delta.content
23
+ else:
24
+ token = message.choices[0].text
25
+ if token:
26
+ yield token
27
+ except Exception as chat_error:
28
+ try:
29
+ # chat_completion 실패 시 text_generation 시도
30
+ response = client.text_generation(
31
+ prompt,
32
+ max_new_tokens=max_tokens,
33
+ temperature=temperature,
34
+ top_p=top_p,
35
+ stream=True
36
+ )
37
+ for token in response:
38
+ yield token
39
+ except Exception as text_error:
40
+ # 두 방법 모두 실패 시 오류 메시지 반환
41
+ yield f"모델 {model_name}에 대한 추론 실패:\n"
42
+ yield f"Chat 오류: {str(chat_error)}\n"
43
+ yield f"Text 오류: {str(text_error)}"
44
+
45
  def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
46
  try:
47
  client = InferenceClient(model=selected_model, token=hf_token)
 
55
  messages.append({"role": "user", "content": message})
56
 
57
  response = ""
58
+ for token in get_model_response(client, messages, max_tokens, temperature, top_p, selected_model):
59
+ response += token
60
+ yield response
61
+
62
+ if not response:
 
 
 
 
 
 
 
 
 
 
 
 
63
  yield "모델이 응답을 생성하지 못했습니다. 다른 입력이나 모델을 시도해보세요."
64
  except Exception as e:
65
  error_msg = f"오류 발생: {str(e)}\n\n상세 오류:\n{traceback.format_exc()}"
66
  yield error_msg
67
 
68
+ # 원래의 모델 목록 복원
69
  models = {
70
  "deepseek-ai/DeepSeek-Coder-V2-Instruct": "DeepSeek-Coder-V2-Instruct",
71
  "CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus",