Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
3391dd2
1
Parent(s):
7160aa7
update sth. at 2024-11-15 18:51:34
Browse files- app.py +31 -67
- requirements.txt +4 -5
- 支持类型.md +3 -0
app.py
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
# ===================== 核心逻辑模块 =====================
|
6 |
|
|
|
|
|
|
|
7 |
# 初始化模型客户端
|
8 |
try:
|
9 |
# 文本聊天模型
|
@@ -12,100 +16,60 @@ try:
|
|
12 |
# 图片生成模型
|
13 |
client_image = InferenceClient("black-forest-labs/FLUX.1-dev")
|
14 |
|
15 |
-
#
|
16 |
-
client_vqa =
|
17 |
except Exception as e:
|
18 |
print(f"Error initializing clients: {e}")
|
19 |
|
20 |
-
# ---------- 文本聊天模块 ----------
|
21 |
-
|
22 |
-
def chat_with_model(messages):
|
23 |
-
"""
|
24 |
-
调用文本聊天模型生成对话内容。
|
25 |
-
"""
|
26 |
-
try:
|
27 |
-
response = client_text.chat_completion(messages, max_tokens=100)
|
28 |
-
return response["choices"][0]["message"]["content"]
|
29 |
-
except Exception as e:
|
30 |
-
print(f"Chat generation failed: {e}")
|
31 |
-
return "聊天生成失败,请稍后再试。"
|
32 |
-
|
33 |
# ---------- 图像生成模块 ----------
|
34 |
-
|
35 |
def image_gen(prompt):
|
36 |
"""
|
37 |
-
|
38 |
"""
|
39 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
image = client_image.text_to_image(prompt)
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
print(f"Image generation failed: {e}")
|
44 |
-
return None
|
45 |
-
|
46 |
-
# ---------- 图像问答模块 ----------
|
47 |
-
|
48 |
-
def visual_qa(image, question):
|
49 |
-
"""
|
50 |
-
调用视觉文档问答模型回答图像问题。
|
51 |
-
"""
|
52 |
-
try:
|
53 |
-
# 检查输入是否为URL路径或本地路径
|
54 |
-
if isinstance(image, str) and image.startswith("http"):
|
55 |
-
# 如果是网络路径,直接使用
|
56 |
-
image_path = image
|
57 |
-
else:
|
58 |
-
# 如果是本地图像,获取Gradio的文件路径
|
59 |
-
image_path = image.name # Gradio会传递上传文件的路径
|
60 |
-
|
61 |
-
# 调用视觉问答API
|
62 |
-
response = client_vqa.visual_question_answering(
|
63 |
-
image=image_path,
|
64 |
-
question=question
|
65 |
-
)
|
66 |
-
return response["answer"]
|
67 |
-
except Exception as e:
|
68 |
-
print(f"Visual QA failed: {e}")
|
69 |
-
return "图像问答失败,请稍后再试。"
|
70 |
|
71 |
# ===================== Gradio 界面构建 =====================
|
72 |
|
73 |
def build_interface():
|
74 |
"""
|
75 |
-
构建 Gradio
|
76 |
"""
|
77 |
with gr.Blocks() as demo:
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
chatbox_output = gr.Textbox(label="回答")
|
82 |
-
chatbox_button = gr.Button("发送")
|
83 |
-
|
84 |
-
def chat_handler(user_input):
|
85 |
-
messages = [{"role": "user", "content": user_input}]
|
86 |
-
return chat_with_model(messages)
|
87 |
-
|
88 |
-
chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
|
89 |
-
|
90 |
# 图像生成
|
91 |
with gr.Tab("图像生成"):
|
92 |
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
93 |
image_output = gr.Image(label="生成的图像")
|
|
|
94 |
image_button = gr.Button("生成图像")
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
vqa_image = gr.Image(label="上传图像")
|
101 |
-
vqa_question = gr.Textbox(label="问题", placeholder="输入关于图像的问题")
|
102 |
-
vqa_output = gr.Textbox(label="回答")
|
103 |
-
vqa_button = gr.Button("回答")
|
104 |
|
105 |
-
|
106 |
|
107 |
gr.Markdown("### 使用说明")
|
108 |
-
gr.Markdown("
|
109 |
|
110 |
return demo
|
111 |
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from PIL import Image
|
4 |
+
import googletrans # 用于中文到英文的翻译
|
5 |
|
6 |
# ===================== 核心逻辑模块 =====================
|
7 |
|
8 |
+
# 初始化翻译客户端
|
9 |
+
translator = googletrans.Translator()
|
10 |
+
|
11 |
# 初始化模型客户端
|
12 |
try:
|
13 |
# 文本聊天模型
|
|
|
16 |
# 图片生成模型
|
17 |
client_image = InferenceClient("black-forest-labs/FLUX.1-dev")
|
18 |
|
19 |
+
# 图像问答模型(暂时隐藏)
|
20 |
+
client_vqa = None
|
21 |
except Exception as e:
|
22 |
print(f"Error initializing clients: {e}")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# ---------- 图像生成模块 ----------
|
|
|
25 |
def image_gen(prompt):
|
26 |
"""
|
27 |
+
调用图像生成模型生成图像,并翻译中文提示词为英文。
|
28 |
"""
|
29 |
try:
|
30 |
+
# 如果是中文,先翻译成英文
|
31 |
+
if any(ord(c) > 127 for c in prompt): # 判断是否是中文字符
|
32 |
+
prompt = translator.translate(prompt, src='zh-cn', dest='en').text
|
33 |
+
|
34 |
+
# 显示服务正在生成图像
|
35 |
+
status_message = "图像生成中,请稍候..."
|
36 |
+
|
37 |
image = client_image.text_to_image(prompt)
|
38 |
+
|
39 |
+
# 图像生成完成,更新状态
|
40 |
+
status_message = "图像生成完成!"
|
41 |
+
|
42 |
+
return image, prompt, status_message # 返回图像和原始提示词及状态
|
43 |
except Exception as e:
|
44 |
print(f"Image generation failed: {e}")
|
45 |
+
return None, "图像生成失败,请稍后再试。", "图像生成失败,请稍后再试。" # 返回错误消息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# ===================== Gradio 界面构建 =====================
|
48 |
|
49 |
def build_interface():
|
50 |
"""
|
51 |
+
构建 Gradio 界面布局,包括图像生成模块。
|
52 |
"""
|
53 |
with gr.Blocks() as demo:
|
54 |
+
# 状态显示区域
|
55 |
+
status_output = gr.Textbox(label="服务状态", value="服务初始化中...", interactive=False)
|
56 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# 图像生成
|
58 |
with gr.Tab("图像生成"):
|
59 |
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
60 |
image_output = gr.Image(label="生成的图像")
|
61 |
+
image_description = gr.Textbox(label="提示词", placeholder="输入中文或英文提示词", interactive=False)
|
62 |
image_button = gr.Button("生成图像")
|
63 |
|
64 |
+
# 处理图像生成请求
|
65 |
+
def image_handler(prompt):
|
66 |
+
img, translated_prompt, status = image_gen(prompt)
|
67 |
+
return img, translated_prompt, status
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
image_button.click(image_handler, inputs=image_prompt, outputs=[image_output, image_description, status_output])
|
70 |
|
71 |
gr.Markdown("### 使用说明")
|
72 |
+
gr.Markdown("本助手支持图像生成功能,支持中文输入,生成图像并显示提示词。")
|
73 |
|
74 |
return demo
|
75 |
|
requirements.txt
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
beautifulsoup4
|
|
|
1 |
+
gradio
|
2 |
+
huggingface_hub
|
3 |
+
googletrans
|
4 |
+
Pillow
|
|
支持类型.md
CHANGED
@@ -38,6 +38,9 @@ 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")
|
|
|
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 |
+
Helsinki-NLP/opus-mt-zh-en
|
42 |
+
|
43 |
+
|
44 |
### 使用特定模型
|
45 |
client = InferenceClient(model="prompthero/openjourney-v4")
|
46 |
client.text_to_image("xxx")
|