sanbo commited on
Commit
f4bda8e
·
1 Parent(s): 9bf81ad

update sth. at 2024-11-18 23:42:58

Browse files
Files changed (2) hide show
  1. app.py +22 -29
  2. app.py_v2 +144 -0
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import gradio as gr
2
- import subprocess
3
  from huggingface_hub import InferenceClient
4
- from PIL import Image
5
  import requests
6
- import json
7
  # ===================== 核心逻辑模块 =====================
8
 
9
  # 初始化模型客户端
@@ -36,14 +34,13 @@ def chat_with_model(messages):
36
  return "聊天生成失败,请稍后再试。"
37
 
38
  # ---------- chatgpt-4o-mini 模块 ----------
39
-
40
- def chatgpt_4o_mini(Query):
41
  url = 'https://sanbo1200-duck2api.hf.space/completions'
42
  headers = {'Content-Type': 'application/json'}
43
  data = {
44
  "model": "gpt-4o-mini",
45
  "messages": [
46
- {"role": "system", "content": "你是一个辅助机器人"},
47
  {"role": "user", "content": Query}
48
  ],
49
  "stream": False
@@ -52,13 +49,12 @@ def chatgpt_4o_mini(Query):
52
  # 发起 HTTP 请求
53
  response = requests.post(url, json=data, headers=headers, stream=True)
54
  response.encoding = 'utf-8'
55
- if response.status_code!= 200:
56
  return "请求失败"
57
  else:
58
  json_data = response.json()
59
  return json_data['choices'][0]['message']['content']
60
 
61
-
62
  # ---------- 图像生成模块 ----------
63
  def image_gen(prompt):
64
  """
@@ -83,20 +79,19 @@ def image_gen(prompt):
83
  return None, None # 如果生成失败,返回两个空值
84
 
85
  # ===================== Gradio 界面构建 =====================
86
-
87
  def build_interface():
88
  """
89
  构建 Gradio 界面布局,包括文本聊天、chatgpt-4o-mini 和图像生成模块。
90
  """
91
- with gr.Blocks() as demo:
92
- # 服务状态显示区域
93
- status_output = gr.Textbox(label="服务状态", value=service_status, interactive=False)
94
-
95
  # 文本聊天模块
96
  with gr.Tab("Llama3.2-11B"):
97
- chatbox_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
98
- chatbox_output = gr.Textbox(label="回答")
99
- chatbox_button = gr.Button("发送")
100
 
101
  def chat_handler(user_input):
102
  messages = [{"role": "user", "content": user_input}]
@@ -106,35 +101,33 @@ def build_interface():
106
 
107
  # chatgpt-4o-mini 模块
108
  with gr.Tab("gpt4o"):
109
- chatgpt_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
110
- chatgpt_output = gr.Textbox(label="回答")
111
- chatgpt_button = gr.Button("发送")
 
112
 
113
- def chatgpt_handler(user_input):
114
- return chatgpt_4o_mini(user_input)
115
 
116
- chatgpt_button.click(chatgpt_handler, inputs=chatgpt_input, outputs=chatgpt_output)
117
 
118
  # 图像生成模块
119
  with gr.Tab("图像生成"):
120
- image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
121
-
122
- # 创建 Row 布局,左右分布图像
123
  with gr.Row():
124
  image_output_1 = gr.Image(label="服务一生成的图像", elem_id="image_1", interactive=True)
125
  image_output_2 = gr.Image(label="服务二生成的图像", elem_id="image_2", interactive=True)
126
 
127
- image_button = gr.Button("生成图像")
128
 
129
- # 处理图像生成请求
130
  def image_handler(prompt):
131
  img_1, img_2 = image_gen(prompt)
132
  return img_1, img_2
133
 
134
  image_button.click(image_handler, inputs=image_prompt, outputs=[image_output_1, image_output_2])
135
 
136
- gr.Markdown("### 使用说明")
137
- gr.Markdown("本助手支持文本聊天、chatgpt-4o-mini 和图像生成功能,使用上方选项卡切换不同功能。")
138
 
139
  return demo
140
 
 
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
 
3
  import requests
4
+
5
  # ===================== 核心逻辑模块 =====================
6
 
7
  # 初始化模型客户端
 
34
  return "聊天生成失败,请稍后再试。"
35
 
36
  # ---------- chatgpt-4o-mini 模块 ----------
37
+ def chatgpt_4o_mini(system_prompt, Query):
 
38
  url = 'https://sanbo1200-duck2api.hf.space/completions'
39
  headers = {'Content-Type': 'application/json'}
40
  data = {
41
  "model": "gpt-4o-mini",
42
  "messages": [
43
+ {"role": "system", "content": system_prompt}, # 使用用户自定义的系统提示词
44
  {"role": "user", "content": Query}
45
  ],
46
  "stream": False
 
49
  # 发起 HTTP 请求
50
  response = requests.post(url, json=data, headers=headers, stream=True)
51
  response.encoding = 'utf-8'
52
+ if response.status_code != 200:
53
  return "请求失败"
54
  else:
55
  json_data = response.json()
56
  return json_data['choices'][0]['message']['content']
57
 
 
58
  # ---------- 图像生成模块 ----------
59
  def image_gen(prompt):
60
  """
 
79
  return None, None # 如果生成失败,返回两个空值
80
 
81
  # ===================== Gradio 界面构建 =====================
 
82
  def build_interface():
83
  """
84
  构建 Gradio 界面布局,包括文本聊天、chatgpt-4o-mini 和图像生成模块。
85
  """
86
+ with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial, sans-serif;}") as demo:
87
+ gr.Markdown("<h1 style='text-align: center; color: #4A90E2;'>智能助手</h1>")
88
+ status_output = gr.Textbox(label="服务状态", value=service_status, interactive=False, elem_id="status_output")
89
+
90
  # 文本聊天模块
91
  with gr.Tab("Llama3.2-11B"):
92
+ chatbox_input = gr.Textbox(label="输入你的问题", placeholder="请提问...", elem_id="chatbox_input")
93
+ chatbox_output = gr.Textbox(label="回答", interactive=False, elem_id="chatbox_output")
94
+ chatbox_button = gr.Button("发送", elem_id="chatbox_button")
95
 
96
  def chat_handler(user_input):
97
  messages = [{"role": "user", "content": user_input}]
 
101
 
102
  # chatgpt-4o-mini 模块
103
  with gr.Tab("gpt4o"):
104
+ system_prompt_input = gr.Textbox(label="自定义系统提示词", placeholder="输入系统提示词...", elem_id="system_prompt_input")
105
+ chatgpt_input = gr.Textbox(label="输入你的问题", placeholder="请提问...", elem_id="chatgpt_input")
106
+ chatgpt_output = gr.Textbox(label="回答", interactive=False, elem_id="chatgpt_output")
107
+ chatgpt_button = gr.Button("发送", elem_id="chatgpt_button")
108
 
109
+ def chatgpt_handler(system_prompt, user_input):
110
+ return chatgpt_4o_mini(system_prompt, user_input)
111
 
112
+ chatgpt_button.click(chatgpt_handler, inputs=[system_prompt_input, chatgpt_input], outputs=chatgpt_output)
113
 
114
  # 图像生成模块
115
  with gr.Tab("图像生成"):
116
+ image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像", elem_id="image_prompt")
 
 
117
  with gr.Row():
118
  image_output_1 = gr.Image(label="服务一生成的图像", elem_id="image_1", interactive=True)
119
  image_output_2 = gr.Image(label="服务二生成的图像", elem_id="image_2", interactive=True)
120
 
121
+ image_button = gr.Button("生成图像", elem_id="image_button")
122
 
 
123
  def image_handler(prompt):
124
  img_1, img_2 = image_gen(prompt)
125
  return img_1, img_2
126
 
127
  image_button.click(image_handler, inputs=image_prompt, outputs=[image_output_1, image_output_2])
128
 
129
+ gr.Markdown("<h3 style='text-align: center;'>使用说明</h3>")
130
+ gr.Markdown("<p style='text-align: center;'>本助手支持文本聊天、chatgpt-4o-mini 和图像生成功能,使用上方选项卡切换不同功能。</p>")
131
 
132
  return demo
133
 
app.py_v2 ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ from huggingface_hub import InferenceClient
4
+ from PIL import Image
5
+ import requests
6
+ import json
7
+ # ===================== 核心逻辑模块 =====================
8
+
9
+ # 初始化模型客户端
10
+ try:
11
+ # 文本聊天模型
12
+ client_text = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
13
+
14
+ # 图片生成模型 1
15
+ client_image_1 = InferenceClient()
16
+
17
+ # 图片生成模型 2 (FLUX)
18
+ client_image_2 = InferenceClient("black-forest-labs/FLUX.1-dev")
19
+
20
+ # 更新状态为服务已启动
21
+ service_status = "服务已启动,您可以开始使用!"
22
+ except Exception as e:
23
+ print(f"Error initializing clients: {e}")
24
+ service_status = "服务初始化失败,请稍后再试。"
25
+
26
+ # ---------- 文本聊天模块 ----------
27
+ def chat_with_model(messages):
28
+ """
29
+ 调用文本聊天模型生成对话内容。
30
+ """
31
+ try:
32
+ response = client_text.chat_completion(messages, max_tokens=100)
33
+ return response["choices"][0]["message"]["content"]
34
+ except Exception as e:
35
+ print(f"Chat generation failed: {e}")
36
+ return "聊天生成失败,请稍后再试。"
37
+
38
+ # ---------- chatgpt-4o-mini 模块 ----------
39
+
40
+ def chatgpt_4o_mini(Query):
41
+ url = 'https://sanbo1200-duck2api.hf.space/completions'
42
+ headers = {'Content-Type': 'application/json'}
43
+ data = {
44
+ "model": "gpt-4o-mini",
45
+ "messages": [
46
+ {"role": "system", "content": "你是一个辅助机器人"},
47
+ {"role": "user", "content": Query}
48
+ ],
49
+ "stream": False
50
+ }
51
+
52
+ # 发起 HTTP 请求
53
+ response = requests.post(url, json=data, headers=headers, stream=True)
54
+ response.encoding = 'utf-8'
55
+ if response.status_code!= 200:
56
+ return "请求失败"
57
+ else:
58
+ json_data = response.json()
59
+ return json_data['choices'][0]['message']['content']
60
+
61
+
62
+ # ---------- 图像生成模块 ----------
63
+ def image_gen(prompt):
64
+ """
65
+ 调用两个图像生成模型,生成两个图像。
66
+ """
67
+ try:
68
+ # 使用服务一 (默认模型)
69
+ print(f"Generating image from service 1 with prompt: {prompt}")
70
+ image_1 = client_image_1.text_to_image(prompt)
71
+ if image_1 is None:
72
+ print("Service 1 returned no image.")
73
+
74
+ # 使用服务二 (FLUX 模型)
75
+ print(f"Generating image from service 2 with prompt: {prompt}")
76
+ image_2 = client_image_2.text_to_image(prompt)
77
+ if image_2 is None:
78
+ print("Service 2 returned no image.")
79
+
80
+ return image_1, image_2 # 返回两个生成的图像
81
+ except Exception as e:
82
+ print(f"Image generation failed: {e}")
83
+ return None, None # 如果生成失败,返回两个空值
84
+
85
+ # ===================== Gradio 界面构建 =====================
86
+
87
+ def build_interface():
88
+ """
89
+ 构建 Gradio 界面布局,包括文本聊天、chatgpt-4o-mini 和图像生成模块。
90
+ """
91
+ with gr.Blocks() as demo:
92
+ # 服务状态显示区域
93
+ status_output = gr.Textbox(label="服务状态", value=service_status, interactive=False)
94
+
95
+ # 文本聊天模块
96
+ with gr.Tab("Llama3.2-11B"):
97
+ chatbox_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
98
+ chatbox_output = gr.Textbox(label="回答")
99
+ chatbox_button = gr.Button("发送")
100
+
101
+ def chat_handler(user_input):
102
+ messages = [{"role": "user", "content": user_input}]
103
+ return chat_with_model(messages)
104
+
105
+ chatbox_button.click(chat_handler, inputs=chatbox_input, outputs=chatbox_output)
106
+
107
+ # chatgpt-4o-mini 模块
108
+ with gr.Tab("gpt4o"):
109
+ chatgpt_input = gr.Textbox(label="输入你的问题", placeholder="请提问...")
110
+ chatgpt_output = gr.Textbox(label="回答")
111
+ chatgpt_button = gr.Button("发送")
112
+
113
+ def chatgpt_handler(user_input):
114
+ return chatgpt_4o_mini(user_input)
115
+
116
+ chatgpt_button.click(chatgpt_handler, inputs=chatgpt_input, outputs=chatgpt_output)
117
+
118
+ # 图像生成模块
119
+ with gr.Tab("图像生成"):
120
+ image_prompt = gr.Textbox(label="图像提示词", placeholder="描述你想生成的图像")
121
+
122
+ # 创建 Row 布局,左右分布图像
123
+ with gr.Row():
124
+ image_output_1 = gr.Image(label="服务一生成的图像", elem_id="image_1", interactive=True)
125
+ image_output_2 = gr.Image(label="服务二生成的图像", elem_id="image_2", interactive=True)
126
+
127
+ image_button = gr.Button("生成图像")
128
+
129
+ # 处理图像生成请求
130
+ def image_handler(prompt):
131
+ img_1, img_2 = image_gen(prompt)
132
+ return img_1, img_2
133
+
134
+ image_button.click(image_handler, inputs=image_prompt, outputs=[image_output_1, image_output_2])
135
+
136
+ gr.Markdown("### 使用说明")
137
+ gr.Markdown("本助手支持文本聊天、chatgpt-4o-mini 和图像生成功能,使用上方选项卡切换不同功能。")
138
+
139
+ return demo
140
+
141
+ # 启动 Gradio 界面
142
+ if __name__ == "__main__":
143
+ demo = build_interface()
144
+ demo.launch()