Forráskód Böngészése

feat:ImagineWork的图片生成

yt 7 hónapja
szülő
commit
5bc34d6afd

+ 29 - 0
smarthotel-app/src/app/hotel-picture/hotel-picture.component.html

@@ -0,0 +1,29 @@
+<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>
+  <!-- 生成结果 -->
+  @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
smarthotel-app/src/app/hotel-picture/hotel-picture.component.scss


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

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

+ 67 - 0
smarthotel-app/src/app/hotel-picture/hotel-picture.component.ts

@@ -0,0 +1,67 @@
+import { Component, OnInit } from '@angular/core';
+import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
+import { IonTextarea, IonButton } from "@ionic/angular/standalone";
+import { DalleOptions, FmodeChatCompletion, ImagineWork } from 'fmode-ng';
+
+@Component({
+  selector: 'app-hotel-picture',
+  templateUrl: './hotel-picture.component.html',
+  styleUrls: ['./hotel-picture.component.scss'],
+  standalone: true,
+  imports: [
+    IonHeader, IonToolbar, IonTitle, IonContent, 
+    IonButton,
+    IonTextarea
+  ],
+})
+export class HotelPictureComponent 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 PromptTemple = `您是一名专业的美术画家,请您根据用户描述的房间类型和需求直接写出画面,并且以写实的风格为主。
+    房间描述如下:
+    ${this.userPrompt}
+    `
+    
+    let completion = new FmodeChatCompletion([
+      {role:"system",content:""},
+      {role:"user",content:PromptTemple}
+    ])
+    completion.sendCompletion().subscribe((message:any)=>{
+      // 打印消息体
+      console.log(message.content)
+      // 赋值消息内容给组件内属性
+      this.PictureDescResult = message.content
+      if(message?.complete){
+      let picturePrompt = `${this.PictureDescResult}`//画面描述结果
+      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 - 3
smarthotel-app/src/app/tab1/tab1.page.html

@@ -12,7 +12,7 @@
 
 <ion-content [fullscreen]="true">
   <div class="welcome-banner">
-    <h2>欢迎来到智宿管家,您的智慧住宿助手!</h2>
+    <h2>欢迎来到智宿管家,您的智慧住宿助手!</h2>
     <ion-button expand="full" color="primary">开始体验</ion-button>
   </div>
 
@@ -33,8 +33,7 @@
         <ion-card-title>房间图片生成</ion-card-title>
       </ion-card-header>
       <ion-card-content>
-        <ion-button expand="full" (click)="generateRoomImage()">生成房间图片</ion-button>
-        <ion-img *ngIf="roomImage" [src]="roomImage"></ion-img>
+        <ion-button expand="full" (click)="goToPage2()">生成房间图片</ion-button>
       </ion-card-content>
     </ion-card>
   </div>

+ 2 - 4
smarthotel-app/src/app/tab1/tab1.page.ts

@@ -31,10 +31,8 @@ export class Tab1Page {
     this.router.navigate(['/tabs/page-test']);
   }
 
-  // 生成房间图片的方法
-  generateRoomImage() {
-    // 这里可以添加逻辑,例如根据用户输入生成房间图片
-    this.roomImage = 'assets/generated-room-image.png'; // 示例路径,实际应为生成的图片路径
+  goToPage2(){
+    this.router.navigate(['/tabs/hotel-picture']);
   }
 
   // 提交用户反馈的方法

+ 6 - 0
smarthotel-app/src/app/tabs/tabs.routes.ts

@@ -1,5 +1,6 @@
 import { Routes } from '@angular/router';
 import { TabsPage } from './tabs.page';
+import { HotelPictureComponent } from '../hotel-picture/hotel-picture.component';
 
 export const routes: Routes = [
   {
@@ -26,6 +27,11 @@ export const routes: Routes = [
         loadComponent: () =>
           import('../page-test/page-test.component').then((m) => m.PageTestComponent),
       },
+      {
+        path: 'hotel-picture',
+        loadComponent: () =>
+          import('../hotel-picture/hotel-picture.component').then((m) => HotelPictureComponent),
+      },
       {
         path: '',
         redirectTo: '/tabs/tab1',