# hf的功能 ## 文字 https://huggingface.co/docs/huggingface_hub/main/en/guides/inference#run-inference-on-servers from huggingface_hub import InferenceClient messages = [{"role": "user", "content": "中国的首都是哪里?"}] client = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct") client.chat_completion(messages, max_tokens=100) from huggingface_hub import InferenceClient messages = [{"role": "user", "content": "中国的首都是哪里?"}] client = InferenceClient("google/gemma-2-2b-it") client.chat_completion(messages, max_tokens=100) ## 图片 > 中文识别不好 from huggingface_hub import InferenceClient client = InferenceClient() image = client.text_to_image("An astronaut riding a horse on the moon.") image.save("/Users/sanbo/Desktop/astronaut.png") # 'image' is a PIL.Image object from huggingface_hub import InferenceClient client = InferenceClient("black-forest-labs/FLUX.1-dev") image = client.text_to_image("一个天上飞的乌龟.") image.save("/Users/sanbo/Desktop/astronaut.png") # 'image' is a PIL.Image object ## 视觉问答visual_question_answering from huggingface_hub import InferenceClient client = InferenceClient() client.visual_question_answering( image="https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg", question="What is the animal doing?" ) ## 翻译 from huggingface_hub import InferenceClient client = InferenceClient() client.translation("My name is Wolfgang and I live in Berlin") 'Mein Name ist Wolfgang und ich lebe in Berlin.' client.translation("My name is Wolfgang and I live in Berlin", model="Helsinki-NLP/opus-mt-en-zh") Helsinki-NLP/opus-mt-zh-en ### 使用特定模型 client = InferenceClient(model="prompthero/openjourney-v4") client.text_to_image("xxx") 方式二 client = InferenceClient() client.text_to_image(..., model="prompthero/openjourney-v4") ## 客户端请求 from huggingface_hub import InferenceClient client = InferenceClient() response = client.post(json={"inputs": "An astronaut riding a horse on the moon."}, model="stabilityai/stable-diffusion-2-1") response.content ## 支持模型 https://huggingface.co/models?other=conversational&sort=likes client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct") client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct") ## 支持任务 | 域 | 任务 | 支持 | 文件 | | :--------: | :---------------------------------------------------------------------------: | :---: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 音频 | [音频分类](https://huggingface.co/tasks/audio-classification) | ✅ | [audio_classification()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.audio_classification) | | 音频 | [音频到音频](https://huggingface.co/tasks/audio-to-audio) | ✅ | [audio_to_audio()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.audio_to_audio) | | | [自动语音识别](https://huggingface.co/tasks/automatic-speech-recognition) | ✅ | [自动语音识别](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.automatic_speech_recognition) | | | [Text-to-Speech](https://huggingface.co/tasks/text-to-speech) | ✅ | [text_to_speech()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.text_to_speech) | | 计算机视觉 | [图像分类](https://huggingface.co/tasks/image-classification) | ✅ | [image_classification()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.image_classification) | | | [图像分割](https://huggingface.co/tasks/image-segmentation) | ✅ | [image_segmentation()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.image_segmentation) | | | [Image-to-Image](https://huggingface.co/tasks/image-to-image) | ✅ | [image_to_image()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.image_to_image) | | | [图像到文本](https://huggingface.co/tasks/image-to-text) | ✅ | [image_to_text()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.image_to_text) | | | [对象检测](https://huggingface.co/tasks/object-detection) | ✅ | [object_detection()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.object_detection) | | | [文字转影像](https://huggingface.co/tasks/text-to-image) | ✅ | [text_to_image()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.text_to_image) | | | [零拍摄图像分类](https://huggingface.co/tasks/zero-shot-image-classification) | ✅ | [zero_shot_image_classification()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.zero_shot_image_classification) | | 多式 | [文档问题存档](https://huggingface.co/tasks/document-question-answering) | ✅ | [document_question_answering()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.document_question_answering) | | | [视觉问题回答](https://huggingface.co/tasks/visual-question-answering) | ✅ | [visual_question_answering()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.visual_question_answering) | | NLP | 会话 | | 已弃用,请使用聊天完成 | | | [聊天完成](https://huggingface.co/tasks/text-generation) | ✅ | [chat_completion()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.chat_completion) | | | [特征提取](https://huggingface.co/tasks/feature-extraction) | ✅ | [feature_extraction()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.feature_extraction) | | | [填充掩膜](https://huggingface.co/tasks/fill-mask) | ✅ | [fill_mask()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.fill_mask) | | | [问答](https://huggingface.co/tasks/question-answering) | ✅ | [question_answering()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.question_answering) | | | [句子相似度](https://huggingface.co/tasks/sentence-similarity) | ✅ | [sentence_similarity()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.sentence_similarity) | | | [总结](https://huggingface.co/tasks/summarization) | ✅ | [summarizing()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.summarization) | | | [表格问题分类](https://huggingface.co/tasks/table-question-answering) | ✅ | [table_question_answering()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.table_question_answering) | | | [文本分类](https://huggingface.co/tasks/text-classification) | ✅ | [text_classification()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.text_classification) | | | [Generation Text一代](https://huggingface.co/tasks/text-generation) | ✅ | [text_generation()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.text_generation) | | | [记号分类](https://huggingface.co/tasks/token-classification) | ✅ | [token_classification()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.token_classification) | | | [翻译](https://huggingface.co/tasks/translation) | ✅ | [translation()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.translation) | | | [零炮分类](https://huggingface.co/tasks/zero-shot-classification) | ✅ | [零炮分类](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.zero_shot_classification) | | 表格 | [表格分类](https://huggingface.co/tasks/tabular-classification) | ✅ | [表分类](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.tabular_classification) | | | [表格回归](https://huggingface.co/tasks/tabular-regression) | ✅ | [table_regression()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.tabular_regression) | 任务页面: https://huggingface.co/tasks