AIRider commited on
Commit
bf4739d
·
verified ·
1 Parent(s): 5bdf9aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -3,12 +3,10 @@ from huggingface_hub import InferenceClient
3
  import traceback
4
  import os
5
 
6
- # Hugging Face API 토큰을 환경 변수에서 가져옵니다.
7
  hf_token = os.getenv("HF_TOKEN")
8
 
9
  def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
10
  try:
11
- # API 토큰을 사용하여 InferenceClient를 초기화합니다.
12
  client = InferenceClient(model=selected_model, token=hf_token)
13
 
14
  messages = [{"role": "system", "content": system_message}]
@@ -27,14 +25,21 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, se
27
  temperature=temperature,
28
  top_p=top_p,
29
  ):
30
- token = message.choices[0].delta.content
31
- response += token
32
- yield response
 
 
 
 
 
 
 
 
33
  except Exception as e:
34
  error_msg = f"오류 발생: {str(e)}\n\n상세 오류:\n{traceback.format_exc()}"
35
  yield error_msg
36
 
37
- # 모델 목록 (무료 및 유료 모델 포함)
38
  models = {
39
  "deepseek-ai/DeepSeek-Coder-V2-Instruct": "DeepSeek-Coder-V2-Instruct",
40
  "CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus",
@@ -47,7 +52,9 @@ models = {
47
  demo = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
50
- gr.Textbox(value="당신은 친절한 챗봇입니다.", label="시스템 메시지"),
 
 
51
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="최대 새 토큰 수"),
52
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="온도"),
53
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (핵 샘플링)"),
@@ -58,4 +65,4 @@ demo = gr.ChatInterface(
58
  if __name__ == "__main__":
59
  if not hf_token:
60
  print("경고: HF_TOKEN 환경 변수가 설정되지 않았습니다. 일부 모델에 접근할 수 없을 수 있습니다.")
61
- demo.launch(share=True) # public link 생성
 
3
  import traceback
4
  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)
11
 
12
  messages = [{"role": "system", "content": system_message}]
 
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",
 
52
  demo = gr.ChatInterface(
53
  respond,
54
  additional_inputs=[
55
+ gr.Textbox(value="너는 나의 최고의 비서이다.
56
+ 내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
57
+ 반드시 한글로 답변할것.", label="시스템 메시지"),
58
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="최대 새 토큰 수"),
59
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="온도"),
60
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (핵 샘플링)"),
 
65
  if __name__ == "__main__":
66
  if not hf_token:
67
  print("경고: HF_TOKEN 환경 변수가 설정되지 않았습니다. 일부 모델에 접근할 수 없을 수 있습니다.")
68
+ demo.launch(share=True)