Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,14 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
"""
|
| 5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -14,7 +20,14 @@ def respond(
|
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
top_p,
|
|
|
|
|
|
|
| 17 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
for val in history:
|
|
@@ -27,15 +40,15 @@ def respond(
|
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
token =
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
|
@@ -56,9 +69,19 @@ demo = gr.ChatInterface(
|
|
| 56 |
step=0.05,
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 4 |
"""
|
| 5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
"""
|
|
|
|
| 7 |
|
| 8 |
+
# 定义多个模型及其对应的InferenceClient
|
| 9 |
+
MODEL_CLIENTS = {
|
| 10 |
+
"Zephyr 7B Beta": InferenceClient("HuggingFaceH4/zephyr-7b-beta"),
|
| 11 |
+
"Mistral 7B Instruct v0.2": InferenceClient("mistralai/Mistral-7B-Instruct-v0.2"),
|
| 12 |
+
# 可以添加更多模型,例如 Llama-2-7b-chat, 或更小的模型进行对比
|
| 13 |
+
# "Llama 2 7B Chat": InferenceClient("meta-llama/Llama-2-7b-chat-hf"), # 注意:Llama 2可能需要访问权限
|
| 14 |
+
}
|
| 15 |
|
| 16 |
def respond(
|
| 17 |
message,
|
|
|
|
| 20 |
max_tokens,
|
| 21 |
temperature,
|
| 22 |
top_p,
|
| 23 |
+
# 新增参数,用于选择模型
|
| 24 |
+
selected_model_name,
|
| 25 |
):
|
| 26 |
+
# 根据选择的模型名称获取对应的client
|
| 27 |
+
client = MODEL_CLIENTS.get(selected_model_name)
|
| 28 |
+
if not client:
|
| 29 |
+
return "Error: Selected model client not found."
|
| 30 |
+
|
| 31 |
messages = [{"role": "system", "content": system_message}]
|
| 32 |
|
| 33 |
for val in history:
|
|
|
|
| 40 |
|
| 41 |
response = ""
|
| 42 |
|
| 43 |
+
# 使用选定的client进行推理
|
| 44 |
+
for message_chunk in client.chat_completion( # 修改变量名以避免冲突
|
| 45 |
messages,
|
| 46 |
max_tokens=max_tokens,
|
| 47 |
stream=True,
|
| 48 |
temperature=temperature,
|
| 49 |
top_p=top_p,
|
| 50 |
):
|
| 51 |
+
token = message_chunk.choices[0].delta.content # 同样修改变量名
|
|
|
|
| 52 |
response += token
|
| 53 |
yield response
|
| 54 |
|
|
|
|
| 69 |
step=0.05,
|
| 70 |
label="Top-p (nucleus sampling)",
|
| 71 |
),
|
| 72 |
+
# 新增一个Dropdown用于选择模型
|
| 73 |
+
gr.Dropdown(
|
| 74 |
+
list(MODEL_CLIENTS.keys()), # 选项为MODEL_CLIENTS的键(模型名称)
|
| 75 |
+
value=list(MODEL_CLIENTS.keys())[0], # 默认选中第一个模型
|
| 76 |
+
label="Select Model",
|
| 77 |
+
interactive=True, # 允许用户更改
|
| 78 |
+
),
|
| 79 |
],
|
| 80 |
+
# 如果要展示多个模型的输出,可能需要更复杂的界面,例如并排显示
|
| 81 |
+
# 或者让用户选择一个模型,然后显示其输出。上面的方式是让用户选择一个模型进行对话。
|
| 82 |
+
# 如果要同时显示多个模型的输出,ChatInterface可能不够直接,可能需要自定义一个Gradio界面。
|
| 83 |
)
|
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|