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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -1,48 +1,61 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
  def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
5
- client = InferenceClient(selected_model)
6
-
7
- messages = [{"role": "system", "content": system_message}]
8
- for val in history:
9
- if val[0]:
10
- messages.append({"role": "user", "content": val[0]})
11
- if val[1]:
12
- messages.append({"role": "assistant", "content": val[1]})
13
- messages.append({"role": "user", "content": message})
14
-
15
- response = ""
16
- for message in client.chat_completion(
17
- messages,
18
- max_tokens=max_tokens,
19
- stream=True,
20
- temperature=temperature,
21
- top_p=top_p,
22
- ):
23
- token = message.choices[0].delta.content
24
- response += token
25
- yield response
 
 
 
 
 
26
 
 
27
  models = {
28
  "deepseek-ai/DeepSeek-Coder-V2-Instruct": "DeepSeek-Coder-V2-Instruct",
29
  "CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus",
30
  "meta-llama/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
31
  "bartowski/DeepSeek-V2-Chat-0628-GGUF": "DeepSeek-V2-Chat-0628-GGUF",
32
  "google/gemma-7b": "Gemma-7b",
33
- "openai-community/gpt2": "gpt2"
34
  }
35
 
36
  demo = gr.ChatInterface(
37
  respond,
38
  additional_inputs=[
39
- gr.Textbox(value="You are a friendly Chatbot.", label="시스템 메시지"),
40
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="최대 새 토큰 수"),
41
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="온도"),
42
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (핵 샘플링)"),
43
  gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
44
  ],
45
  )
46
 
47
  if __name__ == "__main__":
48
- demo.launch()
 
 
 
1
  import gradio as gr
2
  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}]
15
+ for val in history:
16
+ if val[0]:
17
+ messages.append({"role": "user", "content": val[0]})
18
+ if val[1]:
19
+ messages.append({"role": "assistant", "content": val[1]})
20
+ messages.append({"role": "user", "content": message})
21
+
22
+ response = ""
23
+ for message in client.chat_completion(
24
+ messages,
25
+ max_tokens=max_tokens,
26
+ stream=True,
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",
41
  "meta-llama/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
42
  "bartowski/DeepSeek-V2-Chat-0628-GGUF": "DeepSeek-V2-Chat-0628-GGUF",
43
  "google/gemma-7b": "Gemma-7b",
44
+ "openai-community/gpt2": "GPT-2"
45
  }
46
 
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 (핵 샘플링)"),
54
  gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
55
  ],
56
  )
57
 
58
  if __name__ == "__main__":
59
+ if not hf_token:
60
+ print("경고: HF_TOKEN 환경 변수가 설정되지 않았습니다. 일부 모델에 접근할 수 없을 수 있습니다.")
61
+ demo.launch(share=True) # public link 생성