Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import time | |
| """ | |
| `huggingface_hub` の推論 API サポートについての詳細は、ドキュメントを確認してください: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference | |
| """ | |
| client = InferenceClient("Qwen/Qwen2.5-3b-Instruct") | |
| def respond( | |
| message, | |
| history: list[tuple[str, str]], | |
| system_message, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| ): | |
| messages = [{"role": "system", "content": system_message}] | |
| for val in history: | |
| if val[0]: | |
| messages.append({"role": "user", "content": val[0]}) | |
| if val[1]: | |
| messages.append({"role": "assistant", "content": val[1]}) | |
| messages.append({"role": "user", "content": message}) | |
| # AI応答時間計測開始 | |
| start_time = time.time() | |
| # 応答生成 | |
| response = client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ) | |
| # 応答にかかった時間を計測 | |
| elapsed_time = time.time() - start_time | |
| # 応答内容と経過時間を返す | |
| return response.choices[0].message.content, f"応答にかかった時間: {elapsed_time:.2f}秒" | |
| """ | |
| ChatInterfaceのカスタマイズ方法については、gradioのドキュメントを確認してください: https://www.gradio.app/docs/chatinterface | |
| """ | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="ユーザーの質問や依頼にのみ答えてください。ポジティブに答えてください", label="システムメッセージ"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="最大新規トークン"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="温度"), | |
| gr.Slider( | |
| minimum=0.1, | |
| maximum=1.0, | |
| value=0.95, | |
| step=0.05, | |
| label="Top-p (核サンプリング)", | |
| ), | |
| ], | |
| # 背景色をCSSで設定 | |
| css=""" | |
| .gradio-container { | |
| background-color: #212121; | |
| } | |
| """ | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |