Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
for val in history:
|
|
@@ -35,13 +29,16 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
respond,
|
| 47 |
additional_inputs=[
|
|
@@ -53,11 +50,10 @@ demo = gr.ChatInterface(
|
|
| 53 |
maximum=1.0,
|
| 54 |
value=0.95,
|
| 55 |
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)"
|
| 57 |
),
|
| 58 |
-
]
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import evaluate
|
| 4 |
|
| 5 |
+
# 创建困惑度计算工具
|
| 6 |
+
perplexity = evaluate.load("perplexity", module_type="metric")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 创建推理客户端
|
| 9 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
|
| 11 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
|
| 14 |
for val in history:
|
|
|
|
| 29 |
top_p=top_p,
|
| 30 |
):
|
| 31 |
token = message.choices[0].delta.content
|
|
|
|
| 32 |
response += token
|
| 33 |
yield response
|
| 34 |
|
| 35 |
+
# 计算困惑度
|
| 36 |
+
perplexity_results = perplexity.compute(model_id='gpt2', add_start_token=False, predictions=[response])
|
| 37 |
+
perplexity_value = perplexity_results['perplexity']
|
| 38 |
+
|
| 39 |
+
# 将困惑度结果添加到聊天回复中
|
| 40 |
+
yield f"Perplexity of the response: {perplexity_value}"
|
| 41 |
+
|
| 42 |
demo = gr.ChatInterface(
|
| 43 |
respond,
|
| 44 |
additional_inputs=[
|
|
|
|
| 50 |
maximum=1.0,
|
| 51 |
value=0.95,
|
| 52 |
step=0.05,
|
| 53 |
+
label="Top-p (nucleus sampling)"
|
| 54 |
),
|
| 55 |
+
]
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|