Spaces:
Sleeping
Sleeping
feat: add image.py
Browse files
coze.py
CHANGED
|
@@ -16,6 +16,11 @@ TOOLS_CONFIG = {
|
|
| 16 |
"plugin_id": 7382514781015572487,
|
| 17 |
"api_name": "create_full_story",
|
| 18 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
|
|
@@ -48,38 +53,36 @@ async def run_workflow(parameters, api_name):
|
|
| 48 |
"accept": "*/*",
|
| 49 |
},
|
| 50 |
) as response:
|
|
|
|
|
|
|
| 51 |
async for line in response.content:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
decoded_line = line.decode("utf-8")
|
| 62 |
-
|
| 63 |
-
if decoded_line.startswith("event:"):
|
| 64 |
-
is_message_delta = decoded_line == "event:conversation.message.delta\n"
|
| 65 |
-
return None
|
| 66 |
|
| 67 |
-
if decoded_line.startswith("data:") and is_message_delta:
|
| 68 |
-
message = json.loads(decoded_line[5:])
|
| 69 |
-
if message.get("role") == "assistant" and message.get("type") == "answer":
|
| 70 |
-
return message.get("content")
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
async def generate_story(query, history=None):
|
| 76 |
parameters = {"query": query}
|
| 77 |
full_response = ""
|
| 78 |
-
async for
|
| 79 |
-
|
| 80 |
-
if
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
async def main():
|
|
|
|
| 16 |
"plugin_id": 7382514781015572487,
|
| 17 |
"api_name": "create_full_story",
|
| 18 |
},
|
| 19 |
+
"Stable Diffusion:textToImage": {
|
| 20 |
+
# https://www.coze.com/store/plugin/7340166026262331400
|
| 21 |
+
"plugin_id": 7348854576939810834,
|
| 22 |
+
"api_name": "textToImage",
|
| 23 |
+
},
|
| 24 |
}
|
| 25 |
|
| 26 |
|
|
|
|
| 53 |
"accept": "*/*",
|
| 54 |
},
|
| 55 |
) as response:
|
| 56 |
+
event = None
|
| 57 |
+
data = None
|
| 58 |
async for line in response.content:
|
| 59 |
+
line = line.decode("utf-8").strip()
|
| 60 |
+
if line.startswith("event:"):
|
| 61 |
+
event = line[6:]
|
| 62 |
+
if line.startswith("data:"):
|
| 63 |
+
data = line[5:]
|
| 64 |
+
if data and event:
|
| 65 |
+
yield {"event": event, "data": data}
|
| 66 |
+
event = None
|
| 67 |
+
data = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
async def call_plugin(parameters, api_name):
|
| 71 |
+
async for message in run_workflow(parameters, api_name):
|
| 72 |
+
yield message
|
| 73 |
|
| 74 |
|
| 75 |
async def generate_story(query, history=None):
|
| 76 |
parameters = {"query": query}
|
| 77 |
full_response = ""
|
| 78 |
+
async for message in run_workflow(parameters, "self:create_full_story"):
|
| 79 |
+
event = message.get("event")
|
| 80 |
+
if event == "conversation.message.delta":
|
| 81 |
+
data = json.loads(message.get("data"))
|
| 82 |
+
content = data.get("content")
|
| 83 |
+
if content:
|
| 84 |
+
full_response += content
|
| 85 |
+
yield full_response # 使用生成器逐步输出内容
|
| 86 |
|
| 87 |
|
| 88 |
async def main():
|
image.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from coze import call_plugin
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
async def generate_image(query):
|
| 8 |
+
parameters = {
|
| 9 |
+
"text_prompts": [{"text": query}],
|
| 10 |
+
# Stable Diffusion XL v1.0
|
| 11 |
+
"model_type": 2,
|
| 12 |
+
"width": 1344,
|
| 13 |
+
"height": 768,
|
| 14 |
+
}
|
| 15 |
+
async for message in call_plugin(parameters, "Stable Diffusion:textToImage"):
|
| 16 |
+
event = message.get("event")
|
| 17 |
+
data = message.get("data")
|
| 18 |
+
|
| 19 |
+
if event == "conversation.message.completed" and data:
|
| 20 |
+
try:
|
| 21 |
+
data = json.loads(data) # 尝试解析 JSON 数据
|
| 22 |
+
response_type = data.get("type")
|
| 23 |
+
if response_type == "tool_response":
|
| 24 |
+
content = json.loads(data.get("content"))
|
| 25 |
+
image_url = content.get("data").get("images")[0]
|
| 26 |
+
if image_url:
|
| 27 |
+
yield f'<img src="{image_url})" />'
|
| 28 |
+
except json.JSONDecodeError:
|
| 29 |
+
yield "Error: Failed to decode JSON data."
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
prompt = gr.Textbox(label="输入描述")
|
| 34 |
+
output = gr.HTML()
|
| 35 |
+
generate_button = gr.Button("生成图片")
|
| 36 |
+
generate_button.click(fn=generate_image, inputs=prompt, outputs=output)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|
main.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
from coze import run_workflow
|
| 4 |
|
| 5 |
|
| 6 |
async def create_full_story(query, history=None):
|
| 7 |
parameters = {"query": query}
|
| 8 |
full_response = ""
|
| 9 |
-
async for
|
| 10 |
-
|
| 11 |
-
if
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import json
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
from coze import run_workflow
|
| 5 |
|
| 6 |
|
| 7 |
async def create_full_story(query, history=None):
|
| 8 |
parameters = {"query": query}
|
| 9 |
full_response = ""
|
| 10 |
+
async for message in run_workflow(parameters, "self:create_full_story"):
|
| 11 |
+
event = message.get("event")
|
| 12 |
+
if event == "conversation.message.delta":
|
| 13 |
+
data = json.loads(message.get("data"))
|
| 14 |
+
content = data.get("content")
|
| 15 |
+
if content:
|
| 16 |
+
full_response += content
|
| 17 |
+
yield full_response
|
| 18 |
|
| 19 |
|
| 20 |
if __name__ == "__main__":
|