Kims12 commited on
Commit
23d8e09
·
verified ·
1 Parent(s): 99e5053

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -28
app.py CHANGED
@@ -4,13 +4,11 @@ import os
4
 
5
  MODELS = {
6
  "Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
7
- "DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
8
  "Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
9
  "Meta-Llama 3.1 70B-Instruct": "meta-llama/Meta-Llama-3.1-70B-Instruct",
10
  "Microsoft": "microsoft/Phi-3-mini-4k-instruct",
11
  "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
12
  "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
13
- "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
14
  "Aya-23-35B": "CohereForAI/aya-23-35B"
15
  }
16
 
@@ -18,7 +16,7 @@ def get_client(model_name):
18
  model_id = MODELS[model_name]
19
  hf_token = os.getenv("HF_TOKEN")
20
  if not hf_token:
21
- raise ValueError("HF_TOKEN environment variable is required")
22
  return InferenceClient(model_id, token=hf_token)
23
 
24
  def respond(
@@ -73,28 +71,102 @@ def respond(
73
  chat_history.append((message, partial_message))
74
  yield chat_history
75
  except Exception as e:
76
- error_message = f"An error occurred: {str(e)}"
77
  chat_history.append((message, error_message))
78
  yield chat_history
79
 
80
  def clear_conversation():
81
  return []
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  with gr.Blocks() as demo:
84
  gr.Markdown("# Prompting AI Chatbot")
85
  gr.Markdown("언어모델별 프롬프트 테스트 챗봇입니다.")
86
-
87
- with gr.Row():
88
- with gr.Column(scale=1):
89
- model_name = gr.Radio(
90
- choices=list(MODELS.keys()),
91
- label="Language Model",
92
- value="Zephyr 7B Beta"
93
- )
94
- max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens")
95
- temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
96
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
97
- system_message = gr.Textbox(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  value="""반드시 한글로 답변할 것.
99
  너는 최고의 비서이다.
100
  내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
@@ -102,17 +174,33 @@ with gr.Blocks() as demo:
102
  label="System Message",
103
  lines=3
104
  )
105
-
106
- with gr.Column(scale=2):
107
- chatbot = gr.Chatbot()
108
- msg = gr.Textbox(label="메세지를 입력하세요")
109
- with gr.Row():
110
- submit_button = gr.Button("전송")
111
- clear_button = gr.Button("대화 내역 지우기")
112
-
113
- msg.submit(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
114
- submit_button.click(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
115
- clear_button.click(clear_conversation, outputs=chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  if __name__ == "__main__":
118
- demo.launch()
 
4
 
5
  MODELS = {
6
  "Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
 
7
  "Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
8
  "Meta-Llama 3.1 70B-Instruct": "meta-llama/Meta-Llama-3.1-70B-Instruct",
9
  "Microsoft": "microsoft/Phi-3-mini-4k-instruct",
10
  "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
11
  "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
 
12
  "Aya-23-35B": "CohereForAI/aya-23-35B"
13
  }
14
 
 
16
  model_id = MODELS[model_name]
17
  hf_token = os.getenv("HF_TOKEN")
18
  if not hf_token:
19
+ raise ValueError("HF_TOKEN 환경 변수가 필요합니다.")
20
  return InferenceClient(model_id, token=hf_token)
21
 
22
  def respond(
 
71
  chat_history.append((message, partial_message))
72
  yield chat_history
73
  except Exception as e:
74
+ error_message = f"오류가 발생했습니다: {str(e)}"
75
  chat_history.append((message, error_message))
76
  yield chat_history
77
 
78
  def clear_conversation():
79
  return []
80
 
81
+ # Cohere Command R+ 전용 응답 함수
82
+ from openai import OpenAI
83
+
84
+ ACCESS_TOKEN = os.getenv("HF_TOKEN")
85
+
86
+ cohere_client = OpenAI(
87
+ base_url="https://api-inference.huggingface.co/v1/",
88
+ api_key=ACCESS_TOKEN,
89
+ )
90
+
91
+ def cohere_respond(
92
+ message,
93
+ chat_history,
94
+ system_message,
95
+ max_tokens,
96
+ temperature,
97
+ top_p,
98
+ ):
99
+ messages = [{"role": "system", "content": system_message}]
100
+
101
+ for human, assistant in chat_history:
102
+ if human:
103
+ messages.append({"role": "user", "content": human})
104
+ if assistant:
105
+ messages.append({"role": "assistant", "content": assistant})
106
+
107
+ messages.append({"role": "user", "content": message})
108
+
109
+ response = ""
110
+
111
+ try:
112
+ for msg in cohere_client.chat.completions.create(
113
+ model="CohereForAI/c4ai-command-r-plus-08-2024",
114
+ max_tokens=max_tokens,
115
+ stream=True,
116
+ temperature=temperature,
117
+ top_p=top_p,
118
+ messages=messages,
119
+ ):
120
+ token = msg.choices[0].delta.content
121
+ response += token
122
+ if len(chat_history) > 0 and chat_history[-1][0] == message:
123
+ chat_history[-1] = (message, response)
124
+ else:
125
+ chat_history.append((message, response))
126
+ yield chat_history
127
+ except Exception as e:
128
+ error_message = f"오류가 발생했습니다: {str(e)}"
129
+ chat_history.append((message, error_message))
130
+ yield chat_history
131
+
132
  with gr.Blocks() as demo:
133
  gr.Markdown("# Prompting AI Chatbot")
134
  gr.Markdown("언어모델별 프롬프트 테스트 챗봇입니다.")
135
+
136
+ with gr.Tab("일반 모델"):
137
+ with gr.Row():
138
+ with gr.Column(scale=1):
139
+ model_name = gr.Radio(
140
+ choices=list(MODELS.keys()),
141
+ label="Language Model",
142
+ value="Zephyr 7B Beta"
143
+ )
144
+ max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens")
145
+ temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
146
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
147
+ system_message = gr.Textbox(
148
+ value="""반드시 한글로 답변할 것.
149
+ 너는 최고의 비서이다.
150
+ 내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
151
+ """,
152
+ label="System Message",
153
+ lines=3
154
+ )
155
+
156
+ with gr.Column(scale=2):
157
+ chatbot = gr.Chatbot()
158
+ msg = gr.Textbox(label="메세지를 입력하세요")
159
+ with gr.Row():
160
+ submit_button = gr.Button("전송")
161
+ clear_button = gr.Button("대화 내역 지우기")
162
+
163
+ msg.submit(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
164
+ submit_button.click(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
165
+ clear_button.click(clear_conversation, outputs=chatbot, queue=False)
166
+
167
+ with gr.Tab("Cohere Command R+"):
168
+ with gr.Row():
169
+ cohere_system_message = gr.Textbox(
170
  value="""반드시 한글로 답변할 것.
171
  너는 최고의 비서이다.
172
  내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
 
174
  label="System Message",
175
  lines=3
176
  )
177
+ cohere_max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
178
+ cohere_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
179
+ cohere_top_p = gr.Slider(
180
+ minimum=0.1,
181
+ maximum=1.0,
182
+ value=0.95,
183
+ step=0.05,
184
+ label="Top-P",
185
+ )
186
+
187
+ cohere_chatbot = gr.Chatbot(height=600)
188
+ cohere_msg = gr.Textbox(label="메세지를 입력하세요")
189
+ with gr.Row():
190
+ cohere_submit_button = gr.Button("전송")
191
+ cohere_clear_button = gr.Button("대화 내역 지우기")
192
+
193
+ cohere_msg.submit(
194
+ cohere_respond,
195
+ [cohere_msg, cohere_chatbot, cohere_system_message, cohere_max_tokens, cohere_temperature, cohere_top_p],
196
+ cohere_chatbot
197
+ )
198
+ cohere_submit_button.click(
199
+ cohere_respond,
200
+ [cohere_msg, cohere_chatbot, cohere_system_message, cohere_max_tokens, cohere_temperature, cohere_top_p],
201
+ cohere_chatbot
202
+ )
203
+ cohere_clear_button.click(clear_conversation, outputs=cohere_chatbot, queue=False)
204
 
205
  if __name__ == "__main__":
206
+ demo.launch()