Spaces:
Sleeping
Sleeping
新增海报生成器
Browse files- README.md +1 -1
- local_demo.py +73 -0
- posterGn.py +94 -0
README.md
CHANGED
|
@@ -5,6 +5,6 @@ colorFrom: blue
|
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.19.2
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.19.2
|
| 8 |
+
app_file: posterGn.py
|
| 9 |
pinned: false
|
| 10 |
---
|
local_demo.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import sseclient
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
COZE_API_URL = "https://api.coze.cn/v3/chat"
|
| 8 |
+
API_KEY = "pat_egQpN6qGjxzhy9RFwMsNOgQKJ2bNiQrdFJZBmVch5rhFkYUDPkIGBMxGXUIhlxgB" # 请替换为您的实际API密钥
|
| 9 |
+
BOT_ID = "7430829668795760640" # 请替换为您的实际Bot ID
|
| 10 |
+
|
| 11 |
+
def chat_stream(message, history):
|
| 12 |
+
headers = {
|
| 13 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 14 |
+
"Content-Type": "application/json"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# 构建包含历史记录的消息列表
|
| 18 |
+
messages = []
|
| 19 |
+
for human, assistant in history:
|
| 20 |
+
messages.append({"role": "user", "content": human, "content_type": "text"})
|
| 21 |
+
messages.append({"role": "assistant", "content": assistant, "content_type": "text"})
|
| 22 |
+
messages.append({"role": "user", "content": message, "content_type": "text"})
|
| 23 |
+
|
| 24 |
+
data = {
|
| 25 |
+
"bot_id": BOT_ID,
|
| 26 |
+
"user_id": "123123***", # 可以根据需要修改用户ID
|
| 27 |
+
"stream": True,
|
| 28 |
+
"auto_save_history": True,
|
| 29 |
+
"additional_messages": messages
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
print(f"发送到API的数据: {json.dumps(data, ensure_ascii=False)}", file=sys.stderr)
|
| 33 |
+
|
| 34 |
+
response = requests.post(COZE_API_URL, headers=headers, data=json.dumps(data), stream=True)
|
| 35 |
+
client = sseclient.SSEClient(response)
|
| 36 |
+
|
| 37 |
+
full_response = ""
|
| 38 |
+
for event in client.events():
|
| 39 |
+
if event.data == "[DONE]":
|
| 40 |
+
break
|
| 41 |
+
try:
|
| 42 |
+
chunk = json.loads(event.data)
|
| 43 |
+
if isinstance(chunk, dict) and "content" in chunk and chunk.get("type") == "answer":
|
| 44 |
+
full_response = chunk["content"]
|
| 45 |
+
except json.JSONDecodeError:
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
print(f"完整的响应: {full_response}", file=sys.stderr)
|
| 49 |
+
return full_response
|
| 50 |
+
|
| 51 |
+
iface = gr.ChatInterface(
|
| 52 |
+
chat_stream,
|
| 53 |
+
chatbot=gr.Chatbot(height=400, label="提示词助手"),
|
| 54 |
+
textbox=gr.Textbox(placeholder="在这里输入您的消息...", container=False, scale=7),
|
| 55 |
+
title="米你AI 提示词助手",
|
| 56 |
+
description="帮助你生成提示词,输入一个主题,我会帮你联想合适的提示词",
|
| 57 |
+
theme="soft",
|
| 58 |
+
examples=[
|
| 59 |
+
"一个站在树上的小猴子",
|
| 60 |
+
"今天天气怎么样?",
|
| 61 |
+
"给我讲个笑话"
|
| 62 |
+
],
|
| 63 |
+
cache_examples=False,
|
| 64 |
+
retry_btn="重试",
|
| 65 |
+
undo_btn="撤销",
|
| 66 |
+
clear_btn="清除",
|
| 67 |
+
submit_btn="发送",
|
| 68 |
+
stop_btn="停止",
|
| 69 |
+
autofocus=True
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
iface.launch()
|
posterGn.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import sseclient
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
COZE_API_URL = "https://api.coze.cn/v3/chat"
|
| 9 |
+
API_KEY = os.environ.get("COZE_API_KEY", "your_default_key")
|
| 10 |
+
BOT_ID = os.environ.get("COZE_BOT_ID", "your_default_bot_id")
|
| 11 |
+
|
| 12 |
+
def chat_stream(message, history):
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# 构建包含历史记录的消息列表
|
| 19 |
+
messages = []
|
| 20 |
+
for human, assistant in history:
|
| 21 |
+
messages.append({"role": "user", "content": human, "content_type": "text"})
|
| 22 |
+
messages.append({"role": "assistant", "content": assistant, "content_type": "text"})
|
| 23 |
+
messages.append({"role": "user", "content": message, "content_type": "text"})
|
| 24 |
+
|
| 25 |
+
data = {
|
| 26 |
+
"bot_id": BOT_ID,
|
| 27 |
+
"user_id": "123123***",
|
| 28 |
+
"stream": True,
|
| 29 |
+
"auto_save_history": True,
|
| 30 |
+
"additional_messages": messages
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
print(f"\n[请求] 发送到API的数据: {json.dumps(data, ensure_ascii=False)}", file=sys.stderr)
|
| 34 |
+
|
| 35 |
+
response = requests.post(COZE_API_URL, headers=headers, data=json.dumps(data), stream=True)
|
| 36 |
+
client = sseclient.SSEClient(response)
|
| 37 |
+
|
| 38 |
+
full_response = ""
|
| 39 |
+
for event in client.events():
|
| 40 |
+
if event.data == "[DONE]":
|
| 41 |
+
break
|
| 42 |
+
try:
|
| 43 |
+
chunk = json.loads(event.data)
|
| 44 |
+
print(f"\n[调试] 收到的事件数据: {event.data[:200]}...", file=sys.stderr)
|
| 45 |
+
|
| 46 |
+
if isinstance(chunk, dict):
|
| 47 |
+
if chunk.get("type") == "answer":
|
| 48 |
+
content = json.loads(chunk["content"])
|
| 49 |
+
print("\n[处理] 解析 content 内容", file=sys.stderr)
|
| 50 |
+
|
| 51 |
+
if content.get("card_type") == 3:
|
| 52 |
+
data_str = content.get("data", "")
|
| 53 |
+
try:
|
| 54 |
+
data_json = json.loads(data_str)
|
| 55 |
+
if "variables" in data_json:
|
| 56 |
+
for variable in data_json["variables"].values():
|
| 57 |
+
if variable.get("name") == "img_url":
|
| 58 |
+
img_url = variable.get("defaultValue")
|
| 59 |
+
if img_url:
|
| 60 |
+
full_response = f""
|
| 61 |
+
print(f"\n[成功] 提取到图片URL: {img_url}", file=sys.stderr)
|
| 62 |
+
print(f"\n[成功] 构建的图片标签: {full_response}", file=sys.stderr)
|
| 63 |
+
break
|
| 64 |
+
except json.JSONDecodeError as e:
|
| 65 |
+
print(f"\n[错误] 解析 data 字段失败: {e}", file=sys.stderr)
|
| 66 |
+
except json.JSONDecodeError as e:
|
| 67 |
+
print(f"\n[错误] 解析事件数据失败: {e}", file=sys.stderr)
|
| 68 |
+
|
| 69 |
+
print(f"\n[输出] 最终返回到对话框的响应: {full_response}", file=sys.stderr)
|
| 70 |
+
return full_response
|
| 71 |
+
|
| 72 |
+
iface = gr.ChatInterface(
|
| 73 |
+
chat_stream,
|
| 74 |
+
chatbot=gr.Chatbot(height=800, label="提示词助手"),
|
| 75 |
+
textbox=gr.Textbox(placeholder="在这里输入您的消息...", container=False, scale=7),
|
| 76 |
+
title="米你AI 海报助手",
|
| 77 |
+
description="告诉我你的海报需求,我会帮你提取关键信息、生成海报",
|
| 78 |
+
theme="soft",
|
| 79 |
+
examples=[
|
| 80 |
+
"苹果滞销!老农都急哭了!现在 5 折甩卖。写实风格",
|
| 81 |
+
"中秋节到了,团建活动来啦!我们提供了丰富的食物和游戏",
|
| 82 |
+
"儿童滑板大酬宾啦,现在下单,优惠多多!"
|
| 83 |
+
],
|
| 84 |
+
cache_examples=False,
|
| 85 |
+
retry_btn="重试",
|
| 86 |
+
undo_btn="撤销",
|
| 87 |
+
clear_btn="清除",
|
| 88 |
+
submit_btn="发送",
|
| 89 |
+
stop_btn="停止",
|
| 90 |
+
autofocus=True
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
iface.launch(share=True)
|