Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
941cba6
1
Parent(s):
7abf81a
update sth. at 2024-11-15 18:31:06
Browse files
app.py
CHANGED
@@ -2,165 +2,107 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
-
|
6 |
-
import json
|
7 |
-
import uuid
|
8 |
|
9 |
# ===================== 核心逻辑模块 =====================
|
10 |
|
11 |
# 初始化所需的模型客户端
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
def check_service_status():
|
21 |
-
"""
|
22 |
-
检查各个服务的可用状态,返回服务状态字典。
|
23 |
"""
|
24 |
-
|
25 |
-
"Gemma": check_inference_client(client_gemma),
|
26 |
-
"Mixtral": check_inference_client(client_mixtral),
|
27 |
-
"Llama": check_inference_client(client_llama),
|
28 |
-
"Yi": check_inference_client(client_yi),
|
29 |
-
}
|
30 |
-
return services
|
31 |
-
|
32 |
-
def check_inference_client(client):
|
33 |
-
"""
|
34 |
-
尝试发送简单请求以检查服务的可用性。
|
35 |
"""
|
36 |
try:
|
37 |
-
|
38 |
-
response
|
39 |
-
|
40 |
-
|
41 |
-
return
|
42 |
-
|
43 |
-
def get_service_status_markdown():
|
44 |
-
"""
|
45 |
-
格式化服务状态为 Markdown 文本,用于界面展示。
|
46 |
-
"""
|
47 |
-
statuses = check_service_status()
|
48 |
-
status_text = "\n".join([f"{service}: {'🟢 可用' if available else '🔴 不可用'}" for service, available in statuses.items()])
|
49 |
-
return status_text # 返回字符串而不是 gr.Markdown 对象
|
50 |
-
|
51 |
|
52 |
# ---------- 图像生成模块 ----------
|
53 |
|
54 |
def image_gen(prompt):
|
55 |
"""
|
56 |
-
|
57 |
-
"""
|
58 |
-
client = InferenceClient("KingNish/Image-Gen-Pro")
|
59 |
-
response = client.predict("Image Generation", None, prompt, api_name="/image_gen_pro")
|
60 |
-
image = response.get("image") # 假设返回的结果包含图像
|
61 |
-
return image
|
62 |
-
|
63 |
-
|
64 |
-
# ---------- 文本和图像问答模块 ----------
|
65 |
-
|
66 |
-
def process_llava_input(message, history, processor):
|
67 |
-
"""
|
68 |
-
处理 LLaVA 图像问答输入,提取文本与图像输入,生成模型输入。
|
69 |
-
"""
|
70 |
-
image = None
|
71 |
-
if message["files"]:
|
72 |
-
image = message["files"][0] # 如果有上传的图像文件
|
73 |
-
else:
|
74 |
-
for hist in history:
|
75 |
-
if isinstance(hist[0], tuple):
|
76 |
-
image = hist[0][0] # 从历史记录中提取最后一个图像
|
77 |
-
|
78 |
-
txt = message["text"]
|
79 |
-
image = Image.open(image).convert("RGB")
|
80 |
-
prompt = f"<|im_start|>user <image>\n{txt}<|im_end|><|im_start|>assistant"
|
81 |
-
inputs = processor(prompt, image, return_tensors="pt")
|
82 |
-
return inputs
|
83 |
-
|
84 |
-
|
85 |
-
def llava_answer(inputs, model):
|
86 |
"""
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
|
95 |
-
# ----------
|
96 |
|
97 |
-
def
|
98 |
-
"""
|
99 |
-
执行网络搜索,返回搜索结果标题和链接。
|
100 |
"""
|
101 |
-
|
102 |
-
with requests.Session() as session:
|
103 |
-
resp = session.get("https://www.google.com/search", params={"q": query, "num": 3})
|
104 |
-
soup = BeautifulSoup(resp.text, "html.parser")
|
105 |
-
|
106 |
-
# 提取搜索结果的标题和链接
|
107 |
-
for item in soup.select('div.g'):
|
108 |
-
title_element = item.select_one("h3")
|
109 |
-
link_element = item.select_one("a")
|
110 |
-
if title_element and link_element:
|
111 |
-
title = title_element.get_text()
|
112 |
-
link = link_element["href"]
|
113 |
-
search_results.append((title, link))
|
114 |
-
return search_results
|
115 |
-
|
116 |
-
|
117 |
-
# ---------- 回答生成模块 ----------
|
118 |
-
|
119 |
-
def respond(message, history, client):
|
120 |
"""
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
# ===================== Gradio 界面构建 =====================
|
130 |
|
131 |
def build_interface():
|
132 |
"""
|
133 |
-
构建 Gradio
|
134 |
"""
|
135 |
with gr.Blocks() as demo:
|
136 |
-
#
|
137 |
-
gr.Markdown("# 服务状态")
|
138 |
-
gr.Markdown(get_service_status_markdown()) # 直接传入字符串
|
139 |
-
|
140 |
-
# 多模态交互主界面
|
141 |
with gr.Tab("文本聊天"):
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
|
|
146 |
with gr.Tab("图像生成"):
|
147 |
-
image_prompt = gr.Textbox(label="图像提示词", placeholder="
|
148 |
-
image_output = gr.Image()
|
149 |
image_button = gr.Button("生成图像")
|
150 |
|
|
|
|
|
|
|
151 |
with gr.Tab("图像问答"):
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
gr.Markdown("### 说明")
|
163 |
-
gr.Markdown("该助手支持文本聊天、图像生成和图像问答等功能。根据不同需求选择对应的选项卡使用。")
|
164 |
|
165 |
return demo
|
166 |
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
+
import os
|
|
|
|
|
6 |
|
7 |
# ===================== 核心逻辑模块 =====================
|
8 |
|
9 |
# 初始化所需的模型客户端
|
10 |
+
try:
|
11 |
+
# 文本聊天模型
|
12 |
+
client_text = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
13 |
+
|
14 |
+
# 图片生成模型
|
15 |
+
client_image = InferenceClient("black-forest-labs/FLUX.1-dev")
|
16 |
+
|
17 |
+
# 图像问答模型
|
18 |
+
client_vqa = InferenceClient()
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error initializing clients: {e}")
|
21 |
|
22 |
+
# ---------- 文本聊天模块 ----------
|
23 |
|
24 |
+
def chat_with_model(messages):
|
|
|
|
|
|
|
|
|
25 |
"""
|
26 |
+
调用文本聊天模型生成对话内容。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
"""
|
28 |
try:
|
29 |
+
response = client_text.chat_completion(messages, max_tokens=100)
|
30 |
+
return response["choices"][0]["message"]["content"]
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Chat generation failed: {e}")
|
33 |
+
return "聊天生成失败,请稍后再试。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# ---------- 图像生成模块 ----------
|
36 |
|
37 |
def image_gen(prompt):
|
38 |
"""
|
39 |
+
调用图像生成模型生成图像。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
"""
|
41 |
+
try:
|
42 |
+
image = client_image.text_to_image(prompt)
|
43 |
+
return image
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Image generation failed: {e}")
|
46 |
+
return None
|
|
|
47 |
|
48 |
+
# ---------- 图像问答模块 ----------
|
49 |
|
50 |
+
def visual_qa(image, question):
|
|
|
|
|
51 |
"""
|
52 |
+
调用视觉文档问答模型回答图像问题。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
"""
|
54 |
+
try:
|
55 |
+
# 如果输入是路径或URL,则直接加载
|
56 |
+
if isinstance(image, str):
|
57 |
+
response = client_vqa.visual_question_answering(image=image, question=question)
|
58 |
+
else:
|
59 |
+
# 将本地图像保存为临时文件以供模型使用
|
60 |
+
temp_image_path = f"/tmp/{os.path.basename(image.filename)}"
|
61 |
+
image.save(temp_image_path)
|
62 |
+
response = client_vqa.visual_question_answering(image=temp_image_path, question=question)
|
63 |
+
return response["answer"]
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Visual QA failed: {e}")
|
66 |
+
return "图像问答失败,请稍后再试。"
|
67 |
|
68 |
# ===================== Gradio 界面构建 =====================
|
69 |
|
70 |
def build_interface():
|
71 |
"""
|
72 |
+
构建 Gradio 界面布局,包括文本聊天、图像生成和图像问答选项卡。
|
73 |
"""
|
74 |
with gr.Blocks() as demo:
|
75 |
+
# 文本聊天
|
|
|
|
|
|
|
|
|
76 |
with gr.Tab("文本聊天"):
|
77 |
+
chatbox_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
|
78 |
+
chatbox_output = gr.Textbox(label="回答")
|
79 |
+
chatbox_button = gr.Button("发送")
|
80 |
+
|
81 |
+
def chat_handler(user_input):
|
82 |
+
messages = [{"role": "user", "content": user_input}]
|
83 |
+
return chat_with_model(messages)
|
84 |
+
|
85 |
+
chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
|
86 |
|
87 |
+
# 图像生成
|
88 |
with gr.Tab("图像生成"):
|
89 |
+
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
90 |
+
image_output = gr.Image(label="生成的图像")
|
91 |
image_button = gr.Button("生成图像")
|
92 |
|
93 |
+
image_button.click(image_gen, inputs=image_prompt, outputs=image_output)
|
94 |
+
|
95 |
+
# 图像问答
|
96 |
with gr.Tab("图像问答"):
|
97 |
+
vqa_image = gr.Image(label="上传图像")
|
98 |
+
vqa_question = gr.Textbox(label="问题", placeholder="输入关于图像的问题")
|
99 |
+
vqa_output = gr.Textbox(label="回答")
|
100 |
+
vqa_button = gr.Button("回答")
|
101 |
+
|
102 |
+
vqa_button.click(visual_qa, inputs=[vqa_image, vqa_question], outputs=vqa_output)
|
103 |
+
|
104 |
+
gr.Markdown("### 使用说明")
|
105 |
+
gr.Markdown("本助手支持文本聊天、图像生成和图像问答功能,使用上方选项卡切换不同功能。")
|
|
|
|
|
|
|
106 |
|
107 |
return demo
|
108 |
|
app.py_v1
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import json
|
7 |
+
import uuid
|
8 |
+
|
9 |
+
# ===================== 核心逻辑模块 =====================
|
10 |
+
|
11 |
+
# 初始化所需的模型客户端
|
12 |
+
client_gemma = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
13 |
+
client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO")
|
14 |
+
client_llama = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
15 |
+
client_yi = InferenceClient("01-ai/Yi-1.5-34B-Chat")
|
16 |
+
|
17 |
+
|
18 |
+
# ---------- 服务状态检查模块 ----------
|
19 |
+
|
20 |
+
def check_service_status():
|
21 |
+
"""
|
22 |
+
检查各个服务的可用状态,返回服务状态字典。
|
23 |
+
"""
|
24 |
+
services = {
|
25 |
+
"Gemma": check_inference_client(client_gemma),
|
26 |
+
"Mixtral": check_inference_client(client_mixtral),
|
27 |
+
"Llama": check_inference_client(client_llama),
|
28 |
+
"Yi": check_inference_client(client_yi),
|
29 |
+
}
|
30 |
+
return services
|
31 |
+
|
32 |
+
def check_inference_client(client):
|
33 |
+
"""
|
34 |
+
尝试发送简单请求以检查服务的可用性。
|
35 |
+
"""
|
36 |
+
try:
|
37 |
+
# 发送一个简单的预测请求以验证可用性,使用空文本预测请求
|
38 |
+
response = client.predict({"inputs": ""})
|
39 |
+
return True if response else False
|
40 |
+
except Exception:
|
41 |
+
return False
|
42 |
+
|
43 |
+
def get_service_status_markdown():
|
44 |
+
"""
|
45 |
+
格式化服务状态为 Markdown 文本,用于界面展示。
|
46 |
+
"""
|
47 |
+
statuses = check_service_status()
|
48 |
+
status_text = "\n".join([f"{service}: {'🟢 可用' if available else '🔴 不可用'}" for service, available in statuses.items()])
|
49 |
+
return status_text # 返回字符串而不是 gr.Markdown 对象
|
50 |
+
|
51 |
+
|
52 |
+
# ---------- 图像生成模块 ----------
|
53 |
+
|
54 |
+
def image_gen(prompt):
|
55 |
+
"""
|
56 |
+
调用图像生成模型生成图像,返回生成的图像路径。
|
57 |
+
"""
|
58 |
+
client = InferenceClient("KingNish/Image-Gen-Pro")
|
59 |
+
response = client.predict("Image Generation", None, prompt, api_name="/image_gen_pro")
|
60 |
+
image = response.get("image") # 假设返回的结果包含图像
|
61 |
+
return image
|
62 |
+
|
63 |
+
|
64 |
+
# ---------- 文本和图像问答模块 ----------
|
65 |
+
|
66 |
+
def process_llava_input(message, history, processor):
|
67 |
+
"""
|
68 |
+
处理 LLaVA 图像问答输入,提取文本与图像输入,生成模型输入。
|
69 |
+
"""
|
70 |
+
image = None
|
71 |
+
if message["files"]:
|
72 |
+
image = message["files"][0] # 如果有上传的图像文件
|
73 |
+
else:
|
74 |
+
for hist in history:
|
75 |
+
if isinstance(hist[0], tuple):
|
76 |
+
image = hist[0][0] # 从历史记录中提取最后一个图像
|
77 |
+
|
78 |
+
txt = message["text"]
|
79 |
+
image = Image.open(image).convert("RGB")
|
80 |
+
prompt = f"<|im_start|>user <image>\n{txt}<|im_end|><|im_start|>assistant"
|
81 |
+
inputs = processor(prompt, image, return_tensors="pt")
|
82 |
+
return inputs
|
83 |
+
|
84 |
+
|
85 |
+
def llava_answer(inputs, model):
|
86 |
+
"""
|
87 |
+
调用 LLaVA 模型回答图像问答请求,返回回答结果。
|
88 |
+
"""
|
89 |
+
# 使用模型生成回答的逻辑
|
90 |
+
output = model.generate(**inputs)
|
91 |
+
answer = output[0]["generated_text"] # 假设模型返回文本在 `generated_text` 字段
|
92 |
+
return answer
|
93 |
+
|
94 |
+
|
95 |
+
# ---------- 网络搜索模块 ----------
|
96 |
+
|
97 |
+
def search(query):
|
98 |
+
"""
|
99 |
+
执行网络搜索,返回搜索结果标题和链接。
|
100 |
+
"""
|
101 |
+
search_results = []
|
102 |
+
with requests.Session() as session:
|
103 |
+
resp = session.get("https://www.google.com/search", params={"q": query, "num": 3})
|
104 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
105 |
+
|
106 |
+
# 提取搜索结果的标题和链接
|
107 |
+
for item in soup.select('div.g'):
|
108 |
+
title_element = item.select_one("h3")
|
109 |
+
link_element = item.select_one("a")
|
110 |
+
if title_element and link_element:
|
111 |
+
title = title_element.get_text()
|
112 |
+
link = link_element["href"]
|
113 |
+
search_results.append((title, link))
|
114 |
+
return search_results
|
115 |
+
|
116 |
+
|
117 |
+
# ---------- 回答生成模块 ----------
|
118 |
+
|
119 |
+
def respond(message, history, client):
|
120 |
+
"""
|
121 |
+
根据输入的消息和历史记录,选择合适的模型生成回答。
|
122 |
+
"""
|
123 |
+
# 使用指定的模型 client 来生成回答
|
124 |
+
response = client.predict({"inputs": message})
|
125 |
+
answer = response.get("generated_text") # 假设返回结果包含生成的文本
|
126 |
+
return answer
|
127 |
+
|
128 |
+
|
129 |
+
# ===================== Gradio 界面构建 =====================
|
130 |
+
|
131 |
+
def build_interface():
|
132 |
+
"""
|
133 |
+
构建 Gradio 界面布局,包括服务状态栏、文本聊天、图像生成和图像问答选项卡。
|
134 |
+
"""
|
135 |
+
with gr.Blocks() as demo:
|
136 |
+
# 服务状态栏
|
137 |
+
gr.Markdown("# 服务状态")
|
138 |
+
gr.Markdown(get_service_status_markdown()) # 直接传入字符串
|
139 |
+
|
140 |
+
# 多模态交互主界面
|
141 |
+
with gr.Tab("文本聊天"):
|
142 |
+
chat_textbox = gr.Textbox(label="输入你的问题", placeholder="输入文本...")
|
143 |
+
chat_output = gr.Chatbot()
|
144 |
+
chat_button = gr.Button("发送")
|
145 |
+
|
146 |
+
with gr.Tab("图像生成"):
|
147 |
+
image_prompt = gr.Textbox(label="图像提示词", placeholder="输入描述来生成图像")
|
148 |
+
image_output = gr.Image()
|
149 |
+
image_button = gr.Button("生成图像")
|
150 |
+
|
151 |
+
with gr.Tab("图像问答"):
|
152 |
+
image_upload = gr.Image(label="上传图像")
|
153 |
+
image_question = gr.Textbox(label="提问", placeholder="输入关于图像的问题")
|
154 |
+
answer_output = gr.Textbox(label="回答")
|
155 |
+
answer_button = gr.Button("回答")
|
156 |
+
|
157 |
+
# 各个按钮的点击事件
|
158 |
+
chat_button.click(lambda msg, hist: respond(msg, hist, client_gemma), inputs=[chat_textbox, chat_output], outputs=chat_output)
|
159 |
+
image_button.click(image_gen, inputs=image_prompt, outputs=image_output)
|
160 |
+
answer_button.click(lambda msg, hist: llava_answer(process_llava_input(msg, hist, processor)), inputs=[image_upload, image_question], outputs=answer_output)
|
161 |
+
|
162 |
+
gr.Markdown("### 说明")
|
163 |
+
gr.Markdown("该助手支持文本聊天、图像生成和图像问答等功能。根据不同需求选择对应的选项卡使用。")
|
164 |
+
|
165 |
+
return demo
|
166 |
+
|
167 |
+
# 启动 Gradio 界面
|
168 |
+
if __name__ == "__main__":
|
169 |
+
demo = build_interface()
|
170 |
+
demo.launch()
|
支持类型.md
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# hf的功能
|
2 |
+
## 文字
|
3 |
+
https://huggingface.co/docs/huggingface_hub/main/en/guides/inference#run-inference-on-servers
|
4 |
+
|
5 |
+
from huggingface_hub import InferenceClient
|
6 |
+
messages = [{"role": "user", "content": "中国的首都是哪里?"}]
|
7 |
+
client = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
8 |
+
client.chat_completion(messages, max_tokens=100)
|
9 |
+
|
10 |
+
## 图片
|
11 |
+
> 中文识别不好
|
12 |
+
|
13 |
+
from huggingface_hub import InferenceClient
|
14 |
+
client = InferenceClient()
|
15 |
+
image = client.text_to_image("An astronaut riding a horse on the moon.")
|
16 |
+
image.save("/Users/sanbo/Desktop/astronaut.png") # 'image' is a PIL.Image object
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
from huggingface_hub import InferenceClient
|
21 |
+
client = InferenceClient("black-forest-labs/FLUX.1-dev")
|
22 |
+
image = client.text_to_image("一个天上飞的乌龟.")
|
23 |
+
image.save("/Users/sanbo/Desktop/astronaut.png") # 'image' is a PIL.Image object
|
24 |
+
|
25 |
+
## 视觉问答visual_question_answering
|
26 |
+
|
27 |
+
from huggingface_hub import InferenceClient
|
28 |
+
client = InferenceClient()
|
29 |
+
client.visual_question_answering(
|
30 |
+
image="https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg",
|
31 |
+
question="What is the animal doing?"
|
32 |
+
)
|
33 |
+
|
34 |
+
## 翻译
|
35 |
+
from huggingface_hub import InferenceClient
|
36 |
+
client = InferenceClient()
|
37 |
+
client.translation("My name is Wolfgang and I live in Berlin")
|
38 |
+
'Mein Name ist Wolfgang und ich lebe in Berlin.'
|
39 |
+
client.translation("My name is Wolfgang and I live in Berlin", model="Helsinki-NLP/opus-mt-en-zh")
|
40 |
+
|
41 |
+
### 使用特定模型
|
42 |
+
client = InferenceClient(model="prompthero/openjourney-v4")
|
43 |
+
client.text_to_image("xxx")
|
44 |
+
|
45 |
+
方式二
|
46 |
+
client = InferenceClient()
|
47 |
+
client.text_to_image(..., model="prompthero/openjourney-v4")
|
48 |
+
|
49 |
+
|
50 |
+
## 客户端请求
|
51 |
+
|
52 |
+
from huggingface_hub import InferenceClient
|
53 |
+
client = InferenceClient()
|
54 |
+
response = client.post(json={"inputs": "An astronaut riding a horse on the moon."}, model="stabilityai/stable-diffusion-2-1")
|
55 |
+
response.content
|
56 |
+
|
57 |
+
## 支持模型
|
58 |
+
https://huggingface.co/models?other=conversational&sort=likes
|
59 |
+
|
60 |
+
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
61 |
+
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
62 |
+
|
63 |
+
|
64 |
+
## 支持任务
|
65 |
+
|
66 |
+
|
67 |
+
| 域 | 任务 | 支持 | 文件 |
|
68 |
+
| :--------: | :---------------------------------------------------------------------------: | :---: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
69 |
+
| 音频 | [音频分类](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) |
|
70 |
+
| 音频 | [音频到音频](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) |
|
71 |
+
| | [自动语音识别](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) |
|
72 |
+
| | [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) |
|
73 |
+
| 计算机视觉 | [图像分类](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) |
|
74 |
+
| | [图像分割](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) |
|
75 |
+
| | [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) |
|
76 |
+
| | [图像到文本](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) |
|
77 |
+
| | [对象检测](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) |
|
78 |
+
| | [文字转影像](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) |
|
79 |
+
| | [零拍摄图像分类](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) |
|
80 |
+
| 多式 | [文档问题存档](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) |
|
81 |
+
| | [视觉问题回答](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) |
|
82 |
+
| NLP | 会话 | | 已弃用,请使用聊天完成 |
|
83 |
+
| | [聊天完成](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) |
|
84 |
+
| | [特征提取](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) |
|
85 |
+
| | [填充掩膜](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) |
|
86 |
+
| | [问答](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) |
|
87 |
+
| | [句子相似度](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) |
|
88 |
+
| | [总结](https://huggingface.co/tasks/summarization) | ✅ | [summarizing()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.summarization) |
|
89 |
+
| | [表格问题分类](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) |
|
90 |
+
| | [文本分类](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) |
|
91 |
+
| | [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) |
|
92 |
+
| | [记号分类](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) |
|
93 |
+
| | [翻译](https://huggingface.co/tasks/translation) | ✅ | [translation()](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.translation) |
|
94 |
+
| | [零炮分类](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) |
|
95 |
+
| 表格 | [表格分类](https://huggingface.co/tasks/tabular-classification) | ✅ | [表分类](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/inference_client#huggingface_hub.InferenceClient.tabular_classification) |
|
96 |
+
| | [表格回归](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) |
|
97 |
+
|
98 |
+
任务页面: https://huggingface.co/tasks
|