Browse Source

feat(book-app): 添加图片生成功能

18460000105 4 months ago
parent
commit
3206363cfa

+ 33 - 0
book-app/src/app/book-picture/book-picture.component.html

@@ -0,0 +1,33 @@
+<ion-header [translucent]="true">
+  <ion-toolbar>
+    <ion-title>
+      图片生成{{imagineWork?.progress || 0}}
+    </ion-title> 
+  </ion-toolbar>
+</ion-header>
+
+<ion-content [fullscreen]="true">
+  <!-- 生成提示词 -->
+  <ion-textarea [value]="userPrompt" (ionInput)="promptInput($event)" placeholder="古诗文填写" autoGrow="true"></ion-textarea>
+  <ion-button (click)="createImage()" expand="block">图片生成</ion-button>
+  <!-- 意境画面提示词 -->
+  <div>
+    {{PictureDescResult}}
+  </div>
+  <!-- 生成结果 -->
+  @if(images.length) {
+    @for(imageUrl of images;track imageUrl){
+      <img [src]="imageUrl" alt="" srcset="">
+    }
+  }
+  <!-- 生成状态 -->
+  @if(!images.length){
+    @if(imagineWork){
+      <h1>生成中</h1>
+    }
+    @if(!imagineWork){
+      <h1>未开始</h1>
+    }
+  }
+
+</ion-content>

+ 0 - 0
book-app/src/app/book-picture/book-picture.component.scss


+ 22 - 0
book-app/src/app/book-picture/book-picture.component.spec.ts

@@ -0,0 +1,22 @@
+import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
+
+import { BookPictureComponent } from './book-picture.component';
+
+describe('BookPictureComponent', () => {
+  let component: BookPictureComponent;
+  let fixture: ComponentFixture<BookPictureComponent>;
+
+  beforeEach(waitForAsync(() => {
+    TestBed.configureTestingModule({
+      imports: [BookPictureComponent],
+    }).compileComponents();
+
+    fixture = TestBed.createComponent(BookPictureComponent);
+    component = fixture.componentInstance;
+    fixture.detectChanges();
+  }));
+
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
+});

+ 71 - 0
book-app/src/app/book-picture/book-picture.component.ts

@@ -0,0 +1,71 @@
+import { Component, OnInit } from '@angular/core';
+import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
+import { IonTextarea, IonButton,IonInput } from "@ionic/angular/standalone";
+import { DalleOptions, ImagineWork, FmodeChatCompletion } from 'fmode-ng';
+
+@Component({
+  selector: 'app-book-picture',
+  templateUrl: './book-picture.component.html',
+  styleUrls: ['./book-picture.component.scss'],
+  standalone: true,
+  imports: [
+    IonHeader, IonToolbar, IonTitle, IonContent, 
+    IonButton,
+    IonInput,
+    IonTextarea
+  ],
+})
+export class BookPictureComponent  implements OnInit {
+
+
+  userPrompt:string = "一把充满雷霆的剑"
+  promptInput(ev:any){
+    this.userPrompt = ev.detail.value;
+  }
+  imagineWork:ImagineWork|undefined
+  images:Array<string> = []
+  constructor(
+  ){
+    // 示例任务,自己生成图片后请存储新的ID
+    this.imagineWork = new ImagineWork("7DNpEjmslp");
+    console.log('ImagineWork instance:', this.imagineWork);
+    this.imagineWork.fetchTask().then(work=>{
+      this.images = this.imagineWork?.images || [];
+    })
+  }
+
+  PictureDescResult:string = `` // 画面描述结果
+  async createImage(){
+    this.imagineWork = new ImagineWork();
+    // 文本生成
+    let PromptTemplate = `您是一名专业的美术画家,请您根据描述的内容,将其描述的画面、场景、人物、物品等用最简短的语言表达,直接写出画面
+    描述如下:
+    ${this.userPrompt}
+    `
+    let completion = new FmodeChatCompletion([
+      {role:"system",content:""},
+      {role:"user",content:PromptTemplate}
+    ])
+    completion.sendCompletion().subscribe((message:any)=>{
+      // 打印消息体
+      console.log(message.content)
+      // 赋值消息内容给组件内属性
+      this.PictureDescResult = message.content
+      if(message?.complete){ // 判断message为完成状态,则设置isComplete为完成
+        // 图片生成
+        let PicturePrompt = `${this.PictureDescResult}\n画面不带任何文字。突出色彩。`
+        let options:DalleOptions = {prompt:PicturePrompt}
+        this.imagineWork?.draw(options).subscribe(work=>{
+            console.log("imagineWork",work?.toJSON())
+            console.log("images",work?.get("images"))
+            if(work?.get("images")?.length){
+              this.images = work?.get("images");
+            }
+        })
+      }
+    })
+
+    
+  }
+  ngOnInit() {}
+}

+ 2 - 1
book-app/src/app/tab1/tab1.page.html

@@ -10,6 +10,7 @@
   <h1>
     生成器列表
   </h1>
+  
   <ion-button expand="full" class="custom-button">
     点击我
     <img src="1.png" alt="底部图片" class="button-image" />
@@ -33,7 +34,7 @@
 </div>
 
 <div class="inquery-human">
-    <ion-button expand="full" (click)="getPlantingPlan()">图片生成器</ion-button>
+    <ion-button expand="full" (click)="goToPage2()">图片生成器</ion-button>
   </div>
 </div>
 

+ 21 - 0
book-app/src/app/tab1/tab1.page.scss

@@ -38,4 +38,25 @@
     margin-top: 8px; /* 调整图片与按钮文本之间的间距 */
     width: 24px; /* 设置图片宽度 */
     height: auto; /* 保持图片比例 */
+  }
+  .main-functions {
+    padding: 16px;
+  
+    ion-button {
+      margin-bottom: 16px;
+    }
+  
+    .function-cards {
+      display: flex;
+      flex-direction: row;
+      justify-content: space-between;
+  
+      ion-card {
+        width: 48%;
+      }
+    }
+  }
+  
+  ion-list {
+    margin-top: 16px;
   }

+ 1 - 1
book-app/src/app/tab1/tab1.page.ts

@@ -21,7 +21,7 @@ export class Tab1Page {
     
     }
   goToPage2(){
-    this.router.navigate(['/tabs/page-test'])
+    this.router.navigate(['/tabs/picture'])
   }
   
  

+ 5 - 0
book-app/src/app/tabs/tabs.routes.ts

@@ -32,6 +32,11 @@ export const routes: Routes = [
         loadComponent: () =>
           import('../page-inquiry/page-inquiry.component').then((m) => m.PageInquiryComponent),
       },
+      {
+        path: 'picture',
+        loadComponent: () =>
+          import('../book-picture/book-picture.component').then((m) => m.BookPictureComponent),
+      },
       {
         path: '',
         redirectTo: '/tabs/tab1',