Explorar el Código

feat: new consult picture with aigc

cainiao-hue hace 4 meses
padre
commit
9fb3bce08a

+ 35 - 0
soul-app/src/app/consult-picture/consult-picture.component.html

@@ -0,0 +1,35 @@
+<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
soul-app/src/app/consult-picture/consult-picture.component.scss


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

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

+ 65 - 0
soul-app/src/app/consult-picture/consult-picture.component.ts

@@ -0,0 +1,65 @@
+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-consult-picture',
+  templateUrl: './consult-picture.component.html',
+  styleUrls: ['./consult-picture.component.scss'],
+  standalone: true,
+  imports: [
+    IonHeader, IonToolbar, IonTitle, IonContent, 
+    IonButton,IonInput,
+    IonTextarea
+  ],
+})
+export class ConsultPictureComponent  implements OnInit {
+
+  userPrompt:string = "一个智能咨询师机器人头像,要求小巧可爱,比例协调,圆润的线条,温和的主色调,眼睛设计成半圆形,面带微笑,加点耳机或者帽子装饰,简约的机器人线条,要有手有脚,给用户一种交互的感觉,加点语气泡泡"
+  promptInput(ev:any){
+    this.userPrompt = ev.detail.value;
+  }
+  imagineWork:ImagineWork|undefined
+  images:Array<string> = []
+  constructor(
+  ){
+    // 示例任务,自己生成图片后请存储新的ID
+    this.imagineWork = new ImagineWork("lpJGiFwWeA");
+    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 options:DalleOptions = {prompt:this.userPrompt}
+      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
soul-app/src/app/tab1/tab1.page.ts

@@ -79,7 +79,7 @@ export class Tab1Page {
   
   clickToConsult() {
     // 点击咨询
-    this.router.navigate(['tabs/consult'])
+    this.router.navigate(['tabs/page-consult'])
   }
   
   viewDetails(topicId: number): void {
@@ -112,4 +112,5 @@ export class Tab1Page {
   createFilledArray(length: number): any[] {
     return Array(length).fill(null);
   }
+  ngOnInit() {}
 }

+ 6 - 1
soul-app/src/app/tabs/tabs.routes.ts

@@ -37,10 +37,15 @@ export const routes: Routes = [
           import('../topic-detail3/topic-detail3.component').then((m) => m.TopicDetail3Component),
       },
       {
-        path: 'consult',
+        path: 'page-consult',
         loadComponent: () =>
           import('../page-consult/page-consult.component').then((m) => m.PageConsultComponent),
       },
+      {
+        path: 'consult-picture',
+        loadComponent: () =>
+          import('../consult-picture/consult-picture.component').then((m) => m.ConsultPictureComponent),
+      },
       {
         path: '',
         redirectTo: '/tabs/tab1',