Skip to content

Commit

Permalink
feat:change the img output format
Browse files Browse the repository at this point in the history
  • Loading branch information
Onelevenvy committed Sep 28, 2024
1 parent a141d9c commit c0fac56
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion backend/app/core/tools/siliconflow/siliconflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def text2img(

response = requests.post(url, json=payload, headers=headers)

return response.json()
return response.json()["images"][0]["url"]

except Exception as e:
return json.dumps(f"There is a error occured . {e}")
Expand Down
50 changes: 28 additions & 22 deletions backend/app/core/tools/spark/spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,34 @@ def spark_response(text, appid, apisecret, apikey):


def img_generation(prompt):
response = spark_response(
text=prompt,
appid=os.environ.get("SPARK_APPID"),
apisecret=os.environ.get("SPARK_APISECRET"),
apikey=os.environ.get("SPARK_APIKEY"),
)
try:
data = json.loads(response)
code = data["header"]["code"]
if code != 0:
return f"error: {code}, {data}"
else:
text = data["payload"]["choices"]["text"]
image_content = text[0]
image_base = image_content["content"]
bs64data = "data:image/jpeg;base64," + image_base

return bs64data
# return aaa

except Exception as e:
return json.dumps(f"There is a error occured . {e}")
appid = os.environ.get("SPARK_APPID", "")
apisecret = os.environ.get("SPARK_APISECRET", "")
apikey = os.environ.get("SPARK_APIKEY", "")
if not appid or not apisecret or not apikey:
return "api key is not set or not correct"
else:
response = spark_response(
text=prompt,
appid=appid,
apisecret=apisecret,
apikey=apikey,
)
try:
data = json.loads(response)
code = data["header"]["code"]
if code != 0:
return f"error: {code}, {data}"
else:
text = data["payload"]["choices"]["text"]
image_content = text[0]
image_base = image_content["content"]
bs64data = "data:image/jpeg;base64," + image_base

return bs64data
# return aaa

except Exception as e:
return json.dumps(f"There is a error occured . {e}")


spark = StructuredTool.from_function(
Expand Down
8 changes: 7 additions & 1 deletion web/src/app/(applayout)/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import useAuth from "@/hooks/useAuth";
import { Box, Container, Text } from "@chakra-ui/react";
import { Box, Container, Image, Text } from "@chakra-ui/react";
import React from "react";

function Dashboard() {
Expand All @@ -14,6 +14,12 @@ function Dashboard() {
Hi, {currentUser?.full_name || currentUser?.email} 👋🏼
</Text>
<Text>Welcome back, nice to see you again!</Text>
<Image
src={'https://sf-maas-uat-prod.oss-cn-shanghai.aliyuncs.com/outputs/f967b5c9-c3c8-4f4b-9f1f-708e2dfda39c_0.png'}
alt="aaa"
width={"auto"}
height={"auto"}
/>
</Box>
</Container>
</>
Expand Down
24 changes: 9 additions & 15 deletions web/src/components/Playground/MessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,15 @@ const MessageBox = ({ message, onResume, isPlayground }: MessageBoxProps) => {
};

function isImag(content: any): boolean {
if (content) {
// 检查是否为字符串并以 "data:image/" 开头
if (typeof content === "string" && content.startsWith("data:image/")) {
return true;
}

// 检查是否为对象并且包含 'images' 键
if (typeof content === "object" && content.images) {
const images = content.images;
// 确保 'images' 是一个非空数组,并检查第一个元素是否包含 'url' 键
if (Array.isArray(images) && images.length > 0 && "url" in images[0]) {
return true;
}
}
if (typeof content === "string") {
// 检查是否为 data URL 或有效的图像 URL
return (
content.startsWith("data:image/") ||
content.startsWith("http://") ||
content.startsWith("https://")
);
}

return false;
}

Expand Down Expand Up @@ -277,7 +271,7 @@ const MessageBox = ({ message, onResume, isPlayground }: MessageBoxProps) => {
<AccordionPanel pb={4}>
{isImag(content) ? (
<Image
src="content"
src={content!}
alt="img"
width={"100%"}
height={"100%"}
Expand Down

0 comments on commit c0fac56

Please sign in to comment.