Browse Source

修改页面样式

ysyyhhh 9 months ago
parent
commit
42f8e22c69
4 changed files with 54 additions and 23 deletions
  1. 1 1
      img.py
  2. 8 2
      kg.py
  3. 35 20
      main.py
  4. 10 0
      生成的绘本.md

+ 1 - 1
img.py

@@ -4,7 +4,7 @@ import os
 from dashscope import ImageSynthesis
 
 # 需要配置环境变量
-os.environ["DASHSCOPE_API_KEY"] = "sk-"
+os.environ["DASHSCOPE_API_KEY"] = ""
 
 
 def judge_image(url: str, text: str):

+ 8 - 2
kg.py

@@ -216,11 +216,17 @@ def story_to_imgs(story: str):
         print(f"对caption: {caption} 进行图片生成...")
         print("\n ------ ")
         url = generate_caption_image_iterate(caption["description"])
-        res.append({"caption": caption["caption"], "url": url})
+        res.append(
+            {
+                "caption": caption["caption"],
+                "url": url,
+                "description": caption["description"],
+            }
+        )
 
     print("\n绘本生成结果:")
     for item in res:
-        print(item["caption"], item["url"])
+        print(item["caption"], item["url"], item["description"])
 
         # url = item["url"]
         # # 保存到本地

+ 35 - 20
main.py

@@ -7,12 +7,14 @@ import gradio as gr
 def mock_img(text: str):
     return [
         {
-            "caption": "科学家们使用克隆技术",
-            "url": "https://image.limaogushi.com/file/picture-books/image/f2/d8/7ae32095-6e8c-4328-a218-6fe57e49bbc2.jpg",
+            "caption": "植物克隆过程示意图",
+            "url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/85/20240710/1b61f1c0/5a69fb02-051b-4708-9a19-83b8f1f5fce2-1.png?Expires=1720710640&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=lk4qpxmXBYPzWl%2FHdfpg4RnSgSU%3D",
+            "description": "图中展示了一棵植物和从植物上剪下的一小段枝条,该枝条被插入土壤中。过一段时间,这小段 枝条生根发芽,长成了一棵新的、和原植物一模一样的新植物。",
         },
         {
-            "caption": "克隆羊多利的诞生",
-            "url": "https://image.limaogushi.com/file/picture-books/image/97/98/75cf0a7b-cb27-44b3-beae-7ab24488ff8c.jpg",
+            "caption": "克隆羊多利的照片",
+            "url": " https://dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com/1d/63/20240710/522176a8/0572556b-f855-4fd4-bd02-aea8a72d7fd1-1.png?Expires=1720710681&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=RT21mWAWpFTwRA5LU20GomgBxuk%3D",
+            "description": " 图中展示了克隆羊多利站在草地上,她看起来非常健康,和普通的羊没有区别。背景是实验室的轮廓 ,暗示了她的诞生地。",
         },
     ]
 
@@ -31,14 +33,26 @@ def mock_story(query: str):
 
 from kg import story_to_imgs
 
+story_existed = False
+
 
 def generate_image(story):
 
-    res = story_to_imgs(story)
+    # res = story_to_imgs(story)
+    res = mock_img(story)
     md = ""
     # 改为markdown
     for i in range(len(res)):
-        md += f"{res[i]['caption']}\n![{res[i]['caption']}]({res[i]['url']})\n\n"
+        md += f"## {res[i]['caption']}\n\n"
+        md += f"{res[i]['description']} \n "
+        md += f"<img src=\"{res[i]['url']}\" width = \"500\" div align=center />\n"
+        if i != len(res) - 1:
+            md += "\n\n"
+        # md += "![{res[i]['caption']}]({res[i]['url']})\n\n"
+
+    # 保存为md文件
+    with open("生成的绘本.md", "w", encoding="utf-8") as f:
+        f.write(md)
     return md
 
 
@@ -46,28 +60,29 @@ from answerQuestions import generateStory
 
 
 def generate_story(query):
-
+    story_existed = True
+    return mock_story(query)
     return generateStory(query)
 
 
-
 with gr.Blocks() as demo:
     gr.Markdown("# 绘本生成器")
 
-    with gr.Row():
-        with gr.Column():
+    with gr.Column():
+        with gr.Row():
             input_text = gr.Textbox(label="输入")
-            with gr.Row():
-                clear_btn = gr.Button("清空")
-                story_btn = gr.Button("生成故事", variant="primary")
-            with gr.Row():
-                img_btn = gr.Button("生成绘本", variant="primary")
-        with gr.Column():
-            story_output = gr.Textbox(label="生成的故事", interactive=False, lines=10)
+        with gr.Row():
+            clear_btn = gr.Button("清空")
+            story_btn = gr.Button("生成故事", variant="primary")
+            img_btn = gr.Button(
+                "生成绘本", variant="primary", interactive=story_existed
+            )
+        with gr.Row():
+            story_output = gr.Textbox(label="故事", interactive=False)
             story_btn.click(generate_story, inputs=input_text, outputs=story_output)
-        with gr.Column():
-            gr.Markdown("## 生成的绘本")
-            img_output = gr.Markdown(label="生成的绘本")
+            # with gr.Row():
+
+            img_output = gr.Markdown(label="绘本")
             img_btn.click(generate_image, inputs=story_output, outputs=img_output)
 
         clear_btn.click(

+ 10 - 0
生成的绘本.md

@@ -0,0 +1,10 @@
+## 植物克隆过程示意图
+
+图中展示了一棵植物和从植物上剪下的一小段枝条,该枝条被插入土壤中。过一段时间,这小段 枝条生根发芽,长成了一棵新的、和原植物一模一样的新植物。 
+ <img src="https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/85/20240710/1b61f1c0/5a69fb02-051b-4708-9a19-83b8f1f5fce2-1.png?Expires=1720710640&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=lk4qpxmXBYPzWl%2FHdfpg4RnSgSU%3D" width = "500" div align=center />
+
+
+## 克隆羊多利的照片
+
+ 图中展示了克隆羊多利站在草地上,她看起来非常健康,和普通的羊没有区别。背景是实验室的轮廓 ,暗示了她的诞生地。 
+ <img src=" https://dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com/1d/63/20240710/522176a8/0572556b-f855-4fd4-bd02-aea8a72d7fd1-1.png?Expires=1720710681&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=RT21mWAWpFTwRA5LU20GomgBxuk%3D" width = "500" div align=center />