Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
5be1557
1
Parent(s):
263464a
update sth. at 2024-11-15 18:59:00
Browse files- app.py +16 -26
- requirements.txt +0 -1
app.py
CHANGED
@@ -1,23 +1,19 @@
|
|
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 |
# 文本聊天模型
|
14 |
client_text = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
15 |
|
16 |
-
# 图片生成模型
|
17 |
-
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
except Exception as e:
|
22 |
print(f"Error initializing clients: {e}")
|
23 |
|
@@ -36,25 +32,19 @@ def chat_with_model(messages):
|
|
36 |
# ---------- 图像生成模块 ----------
|
37 |
def image_gen(prompt):
|
38 |
"""
|
39 |
-
|
40 |
"""
|
41 |
try:
|
42 |
-
#
|
43 |
-
|
44 |
-
prompt = translator.translate(prompt, src='zh-cn', dest='en').text
|
45 |
-
|
46 |
-
# 显示服务正在生成图像
|
47 |
-
status_message = "图像生成中,请稍候..."
|
48 |
-
|
49 |
-
image = client_image.text_to_image(prompt)
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
|
54 |
-
return
|
55 |
except Exception as e:
|
56 |
print(f"Image generation failed: {e}")
|
57 |
-
return None,
|
58 |
|
59 |
# ===================== Gradio 界面构建 =====================
|
60 |
|
@@ -81,16 +71,16 @@ def build_interface():
|
|
81 |
# 图像生成模块
|
82 |
with gr.Tab("图像生成"):
|
83 |
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
84 |
-
|
85 |
-
|
86 |
image_button = gr.Button("生成图像")
|
87 |
|
88 |
# 处理图像生成请求
|
89 |
def image_handler(prompt):
|
90 |
-
|
91 |
-
return
|
92 |
|
93 |
-
image_button.click(image_handler, inputs=image_prompt, outputs=[
|
94 |
|
95 |
gr.Markdown("### 使用说明")
|
96 |
gr.Markdown("本助手支持文本聊天和图像生成功能,使用上方选项卡切换不同功能。")
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
# ===================== 核心逻辑模块 =====================
|
6 |
|
|
|
|
|
|
|
7 |
# 初始化模型客户端
|
8 |
try:
|
9 |
# 文本聊天模型
|
10 |
client_text = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
11 |
|
12 |
+
# 图片生成模型 1
|
13 |
+
client_image_1 = InferenceClient()
|
14 |
|
15 |
+
# 图片生成模型 2 (FLUX)
|
16 |
+
client_image_2 = InferenceClient("black-forest-labs/FLUX.1-dev")
|
17 |
except Exception as e:
|
18 |
print(f"Error initializing clients: {e}")
|
19 |
|
|
|
32 |
# ---------- 图像生成模块 ----------
|
33 |
def image_gen(prompt):
|
34 |
"""
|
35 |
+
调用两个图像生成模型,生成两个图像。
|
36 |
"""
|
37 |
try:
|
38 |
+
# 使用服务一 (默认模型)
|
39 |
+
image_1 = client_image_1.text_to_image(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
# 使用服务二 (FLUX 模型)
|
42 |
+
image_2 = client_image_2.text_to_image(prompt)
|
43 |
|
44 |
+
return image_1, image_2 # 返回两个生成的图像
|
45 |
except Exception as e:
|
46 |
print(f"Image generation failed: {e}")
|
47 |
+
return None, None # 如果生成失败,返回两个空值
|
48 |
|
49 |
# ===================== Gradio 界面构建 =====================
|
50 |
|
|
|
71 |
# 图像生成模块
|
72 |
with gr.Tab("图像生成"):
|
73 |
image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
|
74 |
+
image_output_1 = gr.Image(label="服务一生成的图像")
|
75 |
+
image_output_2 = gr.Image(label="服务二生成的图像")
|
76 |
image_button = gr.Button("生成图像")
|
77 |
|
78 |
# 处理图像生成请求
|
79 |
def image_handler(prompt):
|
80 |
+
img_1, img_2 = image_gen(prompt)
|
81 |
+
return img_1, img_2
|
82 |
|
83 |
+
image_button.click(image_handler, inputs=image_prompt, outputs=[image_output_1, image_output_2])
|
84 |
|
85 |
gr.Markdown("### 使用说明")
|
86 |
gr.Markdown("本助手支持文本聊天和图像生成功能,使用上方选项卡切换不同功能。")
|
requirements.txt
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
gradio
|
2 |
huggingface_hub
|
3 |
-
googletrans
|
4 |
Pillow
|
|
|
1 |
gradio
|
2 |
huggingface_hub
|
|
|
3 |
Pillow
|