Compare commits
7 Commits
9558ea4b35
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6b2a378d1 | ||
|
|
88bb27569a | ||
|
|
5b91e90d45 | ||
|
|
c9550f8a0d | ||
|
|
e728cd1075 | ||
|
|
0774ba5c9e | ||
|
|
2392d0d705 |
41
main.py
41
main.py
@@ -119,22 +119,21 @@ def process_message(msg, display, image_state, image_data_list, printer_uart=Non
|
|||||||
if isinstance(msg, (bytes, bytearray)):
|
if isinstance(msg, (bytes, bytearray)):
|
||||||
if image_state == IMAGE_STATE_RECEIVING:
|
if image_state == IMAGE_STATE_RECEIVING:
|
||||||
try:
|
try:
|
||||||
if len(image_data_list) < 3:
|
if len(image_data_list) < 2:
|
||||||
# 异常情况,重置
|
# 异常情况,重置
|
||||||
return IMAGE_STATE_IDLE, None
|
return IMAGE_STATE_IDLE, None
|
||||||
|
|
||||||
width = image_data_list[0]
|
img_size = image_data_list[0]
|
||||||
height = image_data_list[1]
|
current_offset = image_data_list[1]
|
||||||
current_offset = image_data_list[2]
|
|
||||||
|
|
||||||
# Stream directly to display
|
# Stream directly to display
|
||||||
if display and display.tft:
|
if display and display.tft:
|
||||||
x = (240 - width) // 2
|
x = (240 - img_size) // 2
|
||||||
y = (240 - height) // 2
|
y = (240 - img_size) // 2
|
||||||
display.show_image_chunk(x, y, width, height, msg, current_offset)
|
display.show_image_chunk(x, y, img_size, img_size, msg, current_offset)
|
||||||
|
|
||||||
# Update offset
|
# Update offset
|
||||||
image_data_list[2] += len(msg)
|
image_data_list[1] += len(msg)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Stream image error: {e}")
|
print(f"Stream image error: {e}")
|
||||||
@@ -196,28 +195,22 @@ def process_message(msg, display, image_state, image_data_list, printer_uart=Non
|
|||||||
try:
|
try:
|
||||||
parts = msg.split(":")
|
parts = msg.split(":")
|
||||||
size = int(parts[1])
|
size = int(parts[1])
|
||||||
|
img_size = int(parts[2]) if len(parts) > 2 else 64
|
||||||
|
model_name = parts[3] if len(parts) > 3 else "Unknown Model"
|
||||||
|
|
||||||
width = 64
|
print(f"Image start, size: {size}, img_size: {img_size}")
|
||||||
height = 64
|
convert_img.print_model_info(model_name)
|
||||||
|
|
||||||
if len(parts) >= 4:
|
|
||||||
width = int(parts[2])
|
|
||||||
height = int(parts[3])
|
|
||||||
elif len(parts) == 3:
|
|
||||||
width = int(parts[2])
|
|
||||||
height = int(parts[2]) # assume square
|
|
||||||
|
|
||||||
print(f"Image start, size: {size}, dim: {width}x{height}")
|
|
||||||
image_data_list.clear()
|
image_data_list.clear()
|
||||||
image_data_list.append(width) # index 0
|
image_data_list.append(img_size) # Store metadata at index 0
|
||||||
image_data_list.append(height) # index 1
|
image_data_list.append(0) # Store current received bytes offset at index 1
|
||||||
image_data_list.append(0) # index 2: offset
|
|
||||||
|
|
||||||
# Prepare display for streaming
|
# Prepare display for streaming
|
||||||
if display and display.tft:
|
if display and display.tft:
|
||||||
# Clear screen area where image will be
|
# Calculate position
|
||||||
# optional, but good practice if new image is smaller
|
x = (240 - img_size) // 2
|
||||||
pass
|
y = (240 - img_size) // 2
|
||||||
|
# Pre-set window (this will be done in first chunk call)
|
||||||
|
|
||||||
return IMAGE_STATE_RECEIVING, None
|
return IMAGE_STATE_RECEIVING, None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -8,5 +8,6 @@ services:
|
|||||||
- "8811:8000"
|
- "8811:8000"
|
||||||
volumes:
|
volumes:
|
||||||
- ./output_images:/app/output_images
|
- ./output_images:/app/output_images
|
||||||
|
- ./media:/app/media
|
||||||
- ./.env:/app/.env
|
- ./.env:/app/.env
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
@@ -10,16 +10,17 @@ from dashscope import ImageSynthesis, Generation
|
|||||||
load_dotenv()
|
load_dotenv()
|
||||||
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
|
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
|
||||||
|
|
||||||
|
|
||||||
class ImageGenerator:
|
class ImageGenerator:
|
||||||
def __init__(self, provider="dashscope", model=None):
|
def __init__(self, provider="dashscope", model=None):
|
||||||
self.provider = provider
|
self.provider = provider
|
||||||
self.model = model
|
self.model = model
|
||||||
self.api_key = None
|
self.api_key = None
|
||||||
|
|
||||||
if provider == "doubao":
|
if provider == "doubao":
|
||||||
self.api_key = os.getenv("volcengine_API_KEY")
|
self.api_key = os.getenv("volcengine_API_KEY")
|
||||||
if not self.model:
|
if not self.model:
|
||||||
self.model = "doubao-seedream-5-0-260128" # Default model from user input
|
self.model = "doubao-seedream-4.0"
|
||||||
elif provider == "dashscope":
|
elif provider == "dashscope":
|
||||||
self.api_key = os.getenv("DASHSCOPE_API_KEY")
|
self.api_key = os.getenv("DASHSCOPE_API_KEY")
|
||||||
if not self.model:
|
if not self.model:
|
||||||
@@ -28,49 +29,57 @@ class ImageGenerator:
|
|||||||
def optimize_prompt(self, asr_text, progress_callback=None):
|
def optimize_prompt(self, asr_text, progress_callback=None):
|
||||||
"""Use LLM to optimize the prompt"""
|
"""Use LLM to optimize the prompt"""
|
||||||
print(f"Optimizing prompt for: {asr_text}")
|
print(f"Optimizing prompt for: {asr_text}")
|
||||||
|
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(0, "正在准备优化提示词...")
|
progress_callback(0, "正在准备优化提示词...")
|
||||||
|
|
||||||
system_prompt = """你是一个AI图像提示词优化专家。你的任务是将用户的语音识别结果转化为适合生成"黑白线稿"的提示词。
|
system_prompt = """你是一个AI图像提示词优化专家。你的任务是将用户的语音识别结果转化为适合生成"黑白线稿"的提示词。
|
||||||
关键要求:
|
关键要求:
|
||||||
1. 风格必须是:简单的黑白线稿、简笔画、图标风格 (Line art, Sketch, Icon style)。
|
1. 风格必须是:简单的黑白线稿、简笔画、图标风格 (Line art, Sketch, Icon style)。
|
||||||
2. 画面必须清晰、线条粗壮,适合低分辨率热敏打印机打印。
|
2. 画面必须清晰、线条粗壮,适合低分辨率热敏打印机打印,用来生成标签贴纸。
|
||||||
3. 绝对不要有复杂的阴影、渐变、黑白线条描述。
|
3. 绝对不要有复杂的阴影、渐变、黑白线条描述。
|
||||||
4. 背景必须是纯白 (White background)。
|
4. 背景必须是纯白 (White background)。
|
||||||
5. 提示词内容请使用中文描述,因为绘图模型对中文生成要更准确。
|
5. 提示词内容请使用英文描述,因为绘图模型对英文生成要更准确。
|
||||||
6. 尺寸比例遵循宽48mm:高30mm (约 1.6:1)。
|
6. 尺寸比例遵循宽48mm:高30mm (约 1.6:1)。
|
||||||
7. 直接输出优化后的提示词,不要包含任何解释。
|
7. 直接输出优化后的提示词,不要包含任何解释。
|
||||||
如果用户要求输入文字,则用```把文字包裹起来,文字是中文
|
如果用户要求输入文字,则用```把文字包裹起来,如果用户有中文文字,则用中文包裹起来。所有文字都是中文,描述都是英文。
|
||||||
"房子的旁边有一个小孩,黑白线稿画作,卡通形象, 文字:```中国人```在下方。"
|
example:
|
||||||
|
"A house with a child on the side, black and white line art, cartoon style, text:```中国人``` below."
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(10, "正在调用AI优化提示词...")
|
progress_callback(10, "正在调用AI优化提示词...")
|
||||||
|
|
||||||
# Currently using Qwen-Turbo for all providers for prompt optimization
|
# Currently using Qwen-Turbo for all providers for prompt optimization
|
||||||
# You can also decouple this if needed
|
# You can also decouple this if needed
|
||||||
response = Generation.call(
|
response = Generation.call(
|
||||||
model='qwen3.5-plus',
|
model="qwen-plus",
|
||||||
prompt=f'{system_prompt}\n\n用户语音识别结果:{asr_text}\n\n优化后的提示词:',
|
prompt=f"{system_prompt}\n\n用户语音识别结果:{asr_text}\n\n优化后的提示词:",
|
||||||
max_tokens=200,
|
max_tokens=200,
|
||||||
temperature=0.8
|
temperature=0.8,
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
if hasattr(response, 'output') and response.output and \
|
if (
|
||||||
hasattr(response.output, 'choices') and response.output.choices and \
|
hasattr(response, "output")
|
||||||
len(response.output.choices) > 0:
|
and response.output
|
||||||
|
and hasattr(response.output, "choices")
|
||||||
|
and response.output.choices
|
||||||
|
and len(response.output.choices) > 0
|
||||||
|
):
|
||||||
optimized = response.output.choices[0].message.content.strip()
|
optimized = response.output.choices[0].message.content.strip()
|
||||||
print(f"Optimized prompt: {optimized}")
|
print(f"Optimized prompt: {optimized}")
|
||||||
|
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(30, f"提示词优化完成: {optimized[:50]}...")
|
progress_callback(30, f"提示词优化完成: {optimized[:50]}...")
|
||||||
|
|
||||||
return optimized
|
return optimized
|
||||||
elif hasattr(response, 'output') and response.output and hasattr(response.output, 'text'):
|
elif (
|
||||||
|
hasattr(response, "output")
|
||||||
|
and response.output
|
||||||
|
and hasattr(response.output, "text")
|
||||||
|
):
|
||||||
optimized = response.output.text.strip()
|
optimized = response.output.text.strip()
|
||||||
print(f"Optimized prompt (direct text): {optimized}")
|
print(f"Optimized prompt (direct text): {optimized}")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
@@ -82,11 +91,13 @@ class ImageGenerator:
|
|||||||
progress_callback(0, "提示词优化响应格式错误")
|
progress_callback(0, "提示词优化响应格式错误")
|
||||||
return asr_text
|
return asr_text
|
||||||
else:
|
else:
|
||||||
print(f"Prompt optimization failed: {response.code} - {response.message}")
|
print(
|
||||||
|
f"Prompt optimization failed: {response.code} - {response.message}"
|
||||||
|
)
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(0, f"提示词优化失败: {response.message}")
|
progress_callback(0, f"提示词优化失败: {response.message}")
|
||||||
return asr_text
|
return asr_text
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error optimizing prompt: {e}")
|
print(f"Error optimizing prompt: {e}")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
@@ -104,48 +115,50 @@ class ImageGenerator:
|
|||||||
|
|
||||||
def _generate_dashscope(self, prompt, progress_callback=None):
|
def _generate_dashscope(self, prompt, progress_callback=None):
|
||||||
print(f"Generating image with DashScope for prompt: {prompt}")
|
print(f"Generating image with DashScope for prompt: {prompt}")
|
||||||
|
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(35, "正在请求DashScope生成图片...")
|
progress_callback(35, "正在请求DashScope生成图片...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = ImageSynthesis.call(
|
response = ImageSynthesis.call(
|
||||||
model=self.model,
|
model=self.model, prompt=prompt, size="1280*720"
|
||||||
prompt=prompt,
|
|
||||||
size='1280*720'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
if not response.output:
|
if not response.output:
|
||||||
print("Error: response.output is None")
|
print("Error: response.output is None")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
task_status = response.output.get('task_status')
|
task_status = response.output.get("task_status")
|
||||||
|
|
||||||
if task_status == 'PENDING' or task_status == 'RUNNING':
|
if task_status == "PENDING" or task_status == "RUNNING":
|
||||||
print("Waiting for image generation to complete...")
|
print("Waiting for image generation to complete...")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(45, "AI正在生成图片中...")
|
progress_callback(45, "AI正在生成图片中...")
|
||||||
|
|
||||||
task_id = response.output.get('task_id')
|
task_id = response.output.get("task_id")
|
||||||
max_wait = 120
|
max_wait = 120
|
||||||
waited = 0
|
waited = 0
|
||||||
while waited < max_wait:
|
while waited < max_wait:
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
waited += 2
|
waited += 2
|
||||||
task_result = ImageSynthesis.fetch(task_id)
|
task_result = ImageSynthesis.fetch(task_id)
|
||||||
if task_result.output.task_status == 'SUCCEEDED':
|
if task_result.output.task_status == "SUCCEEDED":
|
||||||
response.output = task_result.output
|
response.output = task_result.output
|
||||||
break
|
break
|
||||||
elif task_result.output.task_status == 'FAILED':
|
elif task_result.output.task_status == "FAILED":
|
||||||
error_msg = task_result.output.message if hasattr(task_result.output, 'message') else 'Unknown error'
|
error_msg = (
|
||||||
|
task_result.output.message
|
||||||
|
if hasattr(task_result.output, "message")
|
||||||
|
else "Unknown error"
|
||||||
|
)
|
||||||
print(f"Image generation failed: {error_msg}")
|
print(f"Image generation failed: {error_msg}")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(35, f"图片生成失败: {error_msg}")
|
progress_callback(35, f"图片生成失败: {error_msg}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if response.output.get('task_status') == 'SUCCEEDED':
|
if response.output.get("task_status") == "SUCCEEDED":
|
||||||
image_url = response.output['results'][0]['url']
|
image_url = response.output["results"][0]["url"]
|
||||||
print(f"Image generated, url: {image_url}")
|
print(f"Image generated, url: {image_url}")
|
||||||
return image_url
|
return image_url
|
||||||
else:
|
else:
|
||||||
@@ -154,7 +167,7 @@ class ImageGenerator:
|
|||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(35, f"图片生成失败: {error_msg}")
|
progress_callback(35, f"图片生成失败: {error_msg}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error generating image: {e}")
|
print(f"Error generating image: {e}")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
@@ -163,55 +176,57 @@ class ImageGenerator:
|
|||||||
|
|
||||||
def _generate_doubao(self, prompt, progress_callback=None):
|
def _generate_doubao(self, prompt, progress_callback=None):
|
||||||
print(f"Generating image with Doubao for prompt: {prompt}")
|
print(f"Generating image with Doubao for prompt: {prompt}")
|
||||||
|
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(35, "正在请求豆包生成图片...")
|
progress_callback(35, "正在请求豆包生成图片...")
|
||||||
|
|
||||||
url = "https://ark.cn-beijing.volces.com/api/v3/images/generations"
|
url = "https://ark.cn-beijing.volces.com/api/v3/images/generations"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {self.api_key}"
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
}
|
}
|
||||||
data = {
|
data = {
|
||||||
"model": self.model,
|
"model": self.model,
|
||||||
"prompt": prompt,
|
"prompt": prompt,
|
||||||
"sequential_image_generation": "disabled",
|
"sequential_image_generation": "disabled",
|
||||||
"response_format": "url",
|
"response_format": "url",
|
||||||
"size": "2K", # Doubao supports different sizes, user example used 2K. But we might want something smaller if possible to save bandwidth/time?
|
"size": "2K", # Doubao supports different sizes, user example used 2K. But we might want something smaller if possible to save bandwidth/time?
|
||||||
# User's curl says "2K". I will stick to it or maybe check docs.
|
# User's curl says "2K". I will stick to it or maybe check docs.
|
||||||
# Actually for thermal printer, we don't need 2K. But let's follow user example.
|
# Actually for thermal printer, we don't need 2K. But let's follow user example.
|
||||||
"stream": False,
|
"stream": False,
|
||||||
"watermark": True
|
"watermark": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.post(url, headers=headers, json=data, timeout=60)
|
response = requests.post(url, headers=headers, json=data, timeout=60)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
result = response.json()
|
result = response.json()
|
||||||
# Check format of result
|
# Check format of result
|
||||||
# Typically OpenAI compatible or similar
|
# Typically OpenAI compatible or similar
|
||||||
# User example didn't show response format, but usually it's "data": [{"url": "..."}]
|
# User example didn't show response format, but usually it's "data": [{"url": "..."}]
|
||||||
|
|
||||||
if "data" in result and len(result["data"]) > 0:
|
if "data" in result and len(result["data"]) > 0:
|
||||||
image_url = result["data"][0]["url"]
|
image_url = result["data"][0]["url"]
|
||||||
print(f"Image generated, url: {image_url}")
|
print(f"Image generated, url: {image_url}")
|
||||||
return image_url
|
return image_url
|
||||||
elif "error" in result:
|
elif "error" in result:
|
||||||
error_msg = result["error"].get("message", "Unknown error")
|
error_msg = result["error"].get("message", "Unknown error")
|
||||||
print(f"Doubao API error: {error_msg}")
|
print(f"Doubao API error: {error_msg}")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(35, f"图片生成失败: {error_msg}")
|
progress_callback(35, f"图片生成失败: {error_msg}")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
print(f"Unexpected response format: {result}")
|
print(f"Unexpected response format: {result}")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
print(f"Doubao API failed with status {response.status_code}: {response.text}")
|
print(
|
||||||
|
f"Doubao API failed with status {response.status_code}: {response.text}"
|
||||||
|
)
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(35, f"图片生成失败: {response.status_code}")
|
progress_callback(35, f"图片生成失败: {response.status_code}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error calling Doubao API: {e}")
|
print(f"Error calling Doubao API: {e}")
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
364
websocket_server/templates/admin.html
Normal file
364
websocket_server/templates/admin.html
Normal file
@@ -0,0 +1,364 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>AI Image Generator Admin</title>
|
||||||
|
<style>
|
||||||
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #1a1a2e; color: #eee; min-height: 100vh; padding: 20px; }
|
||||||
|
.container { max-width: 1100px; margin: 0 auto; }
|
||||||
|
h1 { text-align: center; margin-bottom: 30px; color: #00d4ff; }
|
||||||
|
.card { background: #16213e; border-radius: 12px; padding: 24px; margin-bottom: 20px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); }
|
||||||
|
.card h2 { margin-bottom: 16px; color: #00d4ff; font-size: 18px; }
|
||||||
|
.current-status { background: #0f3460; padding: 16px; border-radius: 8px; margin-bottom: 16px; }
|
||||||
|
.current-status .label { color: #888; font-size: 14px; }
|
||||||
|
.current-status .value { color: #00d4ff; font-size: 20px; font-weight: bold; margin-top: 4px; }
|
||||||
|
.form-group { margin-bottom: 16px; }
|
||||||
|
.form-group label { display: block; margin-bottom: 8px; color: #ccc; }
|
||||||
|
select, input[type="number"] { width: 100%; padding: 12px; border-radius: 8px; border: 1px solid #333; background: #0f3460; color: #fff; font-size: 16px; }
|
||||||
|
select:focus, input:focus { outline: none; border-color: #00d4ff; }
|
||||||
|
input[type="checkbox"] { width: 20px; height: 20px; margin-right: 8px; }
|
||||||
|
.btn { display: inline-block; padding: 12px 24px; border-radius: 8px; border: none; cursor: pointer; font-size: 16px; font-weight: bold; transition: all 0.3s; }
|
||||||
|
.btn-primary { background: #00d4ff; color: #1a1a2e; }
|
||||||
|
.btn-primary:hover { background: #00b8e6; }
|
||||||
|
.btn-danger { background: #e74c3c; color: #fff; }
|
||||||
|
.btn-danger:hover { background: #c0392b; }
|
||||||
|
.btn-small { padding: 6px 12px; font-size: 14px; }
|
||||||
|
.model-list { margin-top: 16px; }
|
||||||
|
.model-item { background: #0f3460; padding: 12px 16px; border-radius: 8px; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; }
|
||||||
|
.model-item.active { border: 2px solid #00d4ff; }
|
||||||
|
.model-item .name { font-weight: bold; }
|
||||||
|
.model-item .provider { color: #888; font-size: 14px; }
|
||||||
|
.test-section { margin-top: 20px; }
|
||||||
|
.test-input { width: 100%; padding: 12px; border-radius: 8px; border: 1px solid #333; background: #0f3460; color: #fff; font-size: 14px; resize: vertical; min-height: 80px; margin-bottom: 12px; }
|
||||||
|
.test-input:focus { outline: none; border-color: #00d4ff; }
|
||||||
|
.message { padding: 12px; border-radius: 8px; margin-top: 12px; display: none; }
|
||||||
|
.message.success { background: #27ae60; display: block; }
|
||||||
|
.message.error { background: #e74c3c; display: block; }
|
||||||
|
.loading { text-align: center; padding: 20px; display: none; }
|
||||||
|
.loading.show { display: block; }
|
||||||
|
.spinner { border: 3px solid #333; border-top: 3px solid #00d4ff; border-radius: 50%; width: 30px; height: 30px; animation: spin 1s linear infinite; margin: 0 auto; }
|
||||||
|
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
||||||
|
|
||||||
|
.auto-delete-settings { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
|
||||||
|
.auto-delete-settings label { display: flex; align-items: center; color: #ccc; }
|
||||||
|
.auto-delete-settings input[type="number"] { width: 80px; }
|
||||||
|
|
||||||
|
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-top: 16px; }
|
||||||
|
.gallery-item { background: #0f3460; border-radius: 8px; overflow: hidden; position: relative; }
|
||||||
|
.gallery-item img { width: 100%; height: 180px; object-fit: cover; display: block; }
|
||||||
|
.gallery-item .info { padding: 12px; }
|
||||||
|
.gallery-item .filename { font-size: 12px; color: #888; word-break: break-all; }
|
||||||
|
.gallery-item .size { font-size: 12px; color: #666; margin-top: 4px; }
|
||||||
|
.gallery-item .delete-btn { position: absolute; top: 8px; right: 8px; background: rgba(231,76,60,0.9); color: white; border: none; border-radius: 50%; width: 28px; height: 28px; cursor: pointer; font-size: 16px; line-height: 28px; }
|
||||||
|
.gallery-item .delete-btn:hover { background: #c0392b; }
|
||||||
|
.empty-gallery { text-align: center; padding: 40px; color: #666; }
|
||||||
|
.flex-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
|
||||||
|
.tab-nav { display: flex; gap: 4px; margin-bottom: 20px; background: #0f3460; border-radius: 8px; padding: 4px; }
|
||||||
|
.tab-nav button { flex: 1; padding: 12px; border: none; background: transparent; color: #888; cursor: pointer; border-radius: 6px; font-size: 16px; transition: all 0.3s; }
|
||||||
|
.tab-nav button.active { background: #00d4ff; color: #1a1a2e; font-weight: bold; }
|
||||||
|
.tab-content { display: none; }
|
||||||
|
.tab-content.active { display: block; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h1>AI Image Generator Admin</h1>
|
||||||
|
|
||||||
|
<div class="tab-nav">
|
||||||
|
<button class="active" onclick="showTab('settings')">设置</button>
|
||||||
|
<button onclick="showTab('gallery')">图片库</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="tab-settings" class="tab-content active">
|
||||||
|
<div class="card">
|
||||||
|
<h2>当前状态</h2>
|
||||||
|
<div class="current-status">
|
||||||
|
<div class="label">当前 Provider</div>
|
||||||
|
<div class="value" id="currentProvider">加载中...</div>
|
||||||
|
<div class="label" style="margin-top: 12px;">当前模型</div>
|
||||||
|
<div class="value" id="currentModel">加载中...</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>切换 Provider</h2>
|
||||||
|
<div class="form-group">
|
||||||
|
<select id="providerSelect">
|
||||||
|
<option value="doubao">豆包 (Doubao)</option>
|
||||||
|
<option value="dashscope">阿里云 (DashScope)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" onclick="switchProvider()">切换 Provider</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>豆包模型</h2>
|
||||||
|
<div class="model-list">
|
||||||
|
<div class="model-item" data-provider="doubao" data-model="doubao-seedream-4.0">
|
||||||
|
<div>
|
||||||
|
<div class="name">doubao-seedream-4.0</div>
|
||||||
|
<div class="provider">豆包</div>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" onclick="setModel('doubao', 'doubao-seedream-4.0')">使用</button>
|
||||||
|
</div>
|
||||||
|
<div class="model-item" data-provider="doubao" data-model="doubao-seedream-5-0-260128">
|
||||||
|
<div>
|
||||||
|
<div class="name">doubao-seedream-5-0-260128</div>
|
||||||
|
<div class="provider">豆包</div>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" onclick="setModel('doubao', 'doubao-seedream-5-0-260128')">使用</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>阿里云模型 (DashScope)</h2>
|
||||||
|
<div class="model-list">
|
||||||
|
<div class="model-item" data-provider="dashscope" data-model="wanx2.0-t2i-turbo">
|
||||||
|
<div>
|
||||||
|
<div class="name">wanx2.0-t2i-turbo</div>
|
||||||
|
<div class="provider">阿里云</div>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" onclick="setModel('dashscope', 'wanx2.0-t2i-turbo')">使用</button>
|
||||||
|
</div>
|
||||||
|
<div class="model-item" data-provider="dashscope" data-model="qwen-image-plus">
|
||||||
|
<div>
|
||||||
|
<div class="name">qwen-image-plus</div>
|
||||||
|
<div class="provider">阿里云</div>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" onclick="setModel('dashscope', 'qwen-image-plus')">使用</button>
|
||||||
|
</div>
|
||||||
|
<div class="model-item" data-provider="dashscope" data-model="qwen-image-v1">
|
||||||
|
<div>
|
||||||
|
<div class="name">qwen-image-v1</div>
|
||||||
|
<div class="provider">阿里云</div>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" onclick="setModel('dashscope', 'qwen-image-v1')">使用</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>测试图片生成</h2>
|
||||||
|
<textarea class="test-input" id="testPrompt" placeholder="输入提示词...">A cute cat, black and white line art, cartoon style</textarea>
|
||||||
|
<button class="btn btn-primary" onclick="testGenerate()">生成图片</button>
|
||||||
|
<div class="loading" id="loading">
|
||||||
|
<div class="spinner"></div>
|
||||||
|
<p style="margin-top: 10px;">生成中...</p>
|
||||||
|
</div>
|
||||||
|
<div class="message" id="message"></div>
|
||||||
|
<div id="resultArea" style="margin-top: 16px; display: none;">
|
||||||
|
<img id="resultImage" style="max-width: 100%; max-height: 300px; border-radius: 8px;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="tab-gallery" class="tab-content">
|
||||||
|
<div class="card">
|
||||||
|
<h2>图片库</h2>
|
||||||
|
<div class="auto-delete-settings">
|
||||||
|
<label><input type="checkbox" id="autoDeleteEnabled" onchange="updateAutoDelete()"> 自动删除</label>
|
||||||
|
<label><input type="number" id="autoDeleteHours" min="1" max="168" value="24" onchange="updateAutoDelete()"> 小时后删除</label>
|
||||||
|
<button class="btn btn-primary btn-small" onclick="loadGallery()">刷新</button>
|
||||||
|
<button class="btn btn-danger btn-small" onclick="deleteAllImages()">删除全部</button>
|
||||||
|
</div>
|
||||||
|
<div class="gallery" id="gallery"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function showTab(tab) {
|
||||||
|
document.querySelectorAll('.tab-content').forEach(el => el.classList.remove('active'));
|
||||||
|
document.querySelectorAll('.tab-nav button').forEach(el => el.classList.remove('active'));
|
||||||
|
document.getElementById('tab-' + tab).classList.add('active');
|
||||||
|
event.target.classList.add('active');
|
||||||
|
if (tab === 'gallery') loadGallery();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadStatus() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/status');
|
||||||
|
const data = await res.json();
|
||||||
|
document.getElementById('currentProvider').textContent = data.provider;
|
||||||
|
document.getElementById('currentModel').textContent = data.model;
|
||||||
|
document.getElementById('providerSelect').value = data.provider;
|
||||||
|
updateActiveModel(data.provider, data.model);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to load status:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadAutoDelete() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/auto-delete');
|
||||||
|
const data = await res.json();
|
||||||
|
document.getElementById('autoDeleteEnabled').checked = data.enabled;
|
||||||
|
document.getElementById('autoDeleteHours').value = data.hours;
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to load auto-delete settings:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateAutoDelete() {
|
||||||
|
const enabled = document.getElementById('autoDeleteEnabled').checked;
|
||||||
|
const hours = document.getElementById('autoDeleteHours').value;
|
||||||
|
try {
|
||||||
|
await fetch('/api/admin/auto-delete', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ enabled, hours: parseInt(hours) })
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to update auto-delete:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateActiveModel(provider, model) {
|
||||||
|
document.querySelectorAll('.model-item').forEach(item => {
|
||||||
|
item.classList.remove('active');
|
||||||
|
if (item.dataset.provider === provider && item.dataset.model === model) {
|
||||||
|
item.classList.add('active');
|
||||||
|
item.querySelector('button').textContent = '使用中';
|
||||||
|
item.querySelector('button').disabled = true;
|
||||||
|
} else {
|
||||||
|
item.querySelector('button').textContent = '使用';
|
||||||
|
item.querySelector('button').disabled = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function switchProvider() {
|
||||||
|
const provider = document.getElementById('providerSelect').value;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/switch', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ provider })
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
showMessage(data.message, data.success);
|
||||||
|
if (data.success) loadStatus();
|
||||||
|
} catch (e) {
|
||||||
|
showMessage('切换失败: ' + e.message, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setModel(provider, model) {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/model', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ provider, model })
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
showMessage(data.message, data.success);
|
||||||
|
if (data.success) loadStatus();
|
||||||
|
} catch (e) {
|
||||||
|
showMessage('设置失败: ' + e.message, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testGenerate() {
|
||||||
|
const prompt = document.getElementById('testPrompt').value;
|
||||||
|
if (!prompt) return;
|
||||||
|
|
||||||
|
document.getElementById('loading').classList.add('show');
|
||||||
|
document.getElementById('message').style.display = 'none';
|
||||||
|
document.getElementById('resultArea').style.display = 'none';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/test-generate', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ prompt })
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.success && data.image_url) {
|
||||||
|
document.getElementById('resultImage').src = data.image_url;
|
||||||
|
document.getElementById('resultArea').style.display = 'block';
|
||||||
|
showMessage('生成成功', true);
|
||||||
|
} else {
|
||||||
|
showMessage(data.message || '生成失败', false);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
showMessage('生成失败: ' + e.message, false);
|
||||||
|
} finally {
|
||||||
|
document.getElementById('loading').classList.remove('show');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadGallery() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/images');
|
||||||
|
const data = await res.json();
|
||||||
|
const gallery = document.getElementById('gallery');
|
||||||
|
|
||||||
|
if (!data.images || data.images.length === 0) {
|
||||||
|
gallery.innerHTML = '<div class="empty-gallery">暂无图片</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gallery.innerHTML = data.images.map(img => `
|
||||||
|
<div class="gallery-item">
|
||||||
|
<button class="delete-btn" onclick="deleteImage('${img.name}')">×</button>
|
||||||
|
<img src="${img.url}" alt="${img.name}" onclick="window.open('${img.url}', '_blank')">
|
||||||
|
<div class="info">
|
||||||
|
<div class="filename">${img.name}</div>
|
||||||
|
<div class="size">${formatSize(img.size)}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to load gallery:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteImage(filename) {
|
||||||
|
if (!confirm('确定要删除这张图片吗?')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/admin/images/${encodeURIComponent(filename)}`, { method: 'DELETE' });
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.success) {
|
||||||
|
loadGallery();
|
||||||
|
} else {
|
||||||
|
alert(data.message);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
alert('删除失败: ' + e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteAllImages() {
|
||||||
|
if (!confirm('确定要删除所有图片吗?此操作不可恢复!')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/images');
|
||||||
|
const data = await res.json();
|
||||||
|
for (const img of data.images) {
|
||||||
|
await fetch(`/api/admin/images/${encodeURIComponent(img.name)}`, { method: 'DELETE' });
|
||||||
|
}
|
||||||
|
loadGallery();
|
||||||
|
} catch (e) {
|
||||||
|
alert('删除失败: ' + e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatSize(bytes) {
|
||||||
|
if (bytes < 1024) return bytes + ' B';
|
||||||
|
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
||||||
|
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
|
||||||
|
}
|
||||||
|
|
||||||
|
function showMessage(msg, success) {
|
||||||
|
const el = document.getElementById('message');
|
||||||
|
el.textContent = msg;
|
||||||
|
el.className = 'message ' + (success ? 'success' : 'error');
|
||||||
|
}
|
||||||
|
|
||||||
|
loadStatus();
|
||||||
|
loadAutoDelete();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user