Browse Source

feat: imagemaker

危齐晟 3 months ago
parent
commit
323a857271

+ 47 - 0
poem-life-app/src/app/page-createpic/page-createpic.component.html

@@ -0,0 +1,47 @@
+<ion-header [translucent]="true">
+  <ion-toolbar color="primary">
+    <ion-title>
+      古诗云想 | 图片生成进度: {{imagineWork?.progress || 0}}%
+    </ion-title>
+  </ion-toolbar>
+</ion-header>
+
+<ion-content [fullscreen]="true" style="background-color: #f9f9f9; padding: 16px;">
+  <!-- 生成提示词 -->
+  <h2 style="text-align: center; color: #5a5a5a;">请输入您的古诗</h2>
+  <ion-textarea
+    [value]="userPrompt"
+    (ionInput)="promptInput($event)"
+    placeholder="在此输入您想要的古诗..."
+    autoGrow="true"
+    style="background-color: white; color: black; border-radius: 8px; margin-bottom: 16px;"
+  ></ion-textarea>
+
+  <ion-button (click)="createImage()" expand="full" color="secondary">生成图片</ion-button>
+
+  <!-- 诗歌分析结果 -->
+  <div *ngIf="responseMsg" style="margin-top: 20px; padding: 10px; background: #e6f7ff; border-radius: 5px;">
+    <h3 style="color: #007bff;">诗歌分析结果:</h3>
+    <p>{{ responseMsg }}</p>
+  </div>
+
+  <!-- 生成结果 -->
+  <div *ngIf="images.length; else noImages">
+    <h2 style="text-align: center; color: #5a5a5a;">生成的图片</h2>
+    <div style="display: flex; flex-wrap: wrap; justify-content: center;">
+      <img *ngFor="let imageUrl of images" [src]="imageUrl" alt="生成的图片" style="max-width: 100%; height: auto; margin: 10px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
+    </div>
+  </div>
+
+  <!-- 生成状态 -->
+  <ng-template #noImages>
+    <div style="text-align: center; margin-top: 20px;">
+      <ng-container *ngIf="imagineWork; else notStarted">
+        <h1>生成中...</h1>
+      </ng-container>
+      <ng-template #notStarted>
+        <h1>未开始</h1>
+      </ng-template>
+    </div>
+  </ng-template>
+</ion-content>

+ 0 - 0
poem-life-app/src/app/page-createpic/page-createpic.component.scss


+ 22 - 0
poem-life-app/src/app/page-createpic/page-createpic.component.spec.ts

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

+ 80 - 0
poem-life-app/src/app/page-createpic/page-createpic.component.ts

@@ -0,0 +1,80 @@
+import { CommonModule } from '@angular/common';
+import { Component, OnInit } from '@angular/core';
+import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
+import { IonInput, IonTextarea, IonButton } from "@ionic/angular/standalone";
+import { DalleOptions, FmodeChatCompletion, ImagineWork, MarkdownPreviewModule } from 'fmode-ng';
+
+@Component({
+  selector: 'app-page-createpic',
+  templateUrl: './page-createpic.component.html',
+  styleUrls: ['./page-createpic.component.scss'],
+  standalone: true,
+  imports: [
+    IonHeader, IonToolbar, IonTitle, IonContent,
+    IonButton, IonInput,
+    IonTextarea, MarkdownPreviewModule,CommonModule
+  ],
+})
+export class PageCreatepicComponent implements OnInit {
+
+  userPrompt: string = "云想衣裳花想容,春风拂槛露华浓。若非群玉山头见,会向瑶台月下逢";
+  promptInput(ev: any) {
+    this.userPrompt = ev.detail.value;
+  }
+
+  imagineWork: ImagineWork | undefined;
+  images: Array<string> = [];
+  responseMsg: string | undefined; // 添加用于接收生成的消息内容
+
+  constructor() {
+    this.imagineWork = new ImagineWork(); // 初始化 ImagineWork 实例
+  }
+
+  async createImage() {
+    let PromptTemplate = `
+    请您作为一位专业的画家,分析以下古诗中的情感、意象和色彩。请考虑以下要素,并将其转化为视觉艺术的表现:
+
+    1. **情感表达**:这首诗传达了哪些情感?是快乐、悲伤、怀念还是其他?
+    2. **意象描绘**:诗中使用了哪些意象?它们分别代表什么?
+    3. **色彩运用**:根据诗的情感和意象,您会选择哪些颜色进行表达?
+    4. **画面构图**:您如何想象画面的构图?有哪些元素值得突出?
+
+    古诗:
+    ${this.userPrompt}
+    `;
+
+
+    let completion = new FmodeChatCompletion([
+      { role: "system", content: "" },
+      { role: "user", content: PromptTemplate }
+    ]);
+
+    completion.sendCompletion().subscribe({
+      next: (message: any) => {
+        console.log(message.content);
+        this.responseMsg = message.content;
+        if (message?.complete && this.imagineWork) { // 确保 imagineWork 已初始化
+          let options: DalleOptions = { prompt: this.userPrompt };
+          this.imagineWork.draw(options).subscribe({
+            next: (work) => {
+              console.log("imagineWork", work?.toJSON());
+              if (work?.get("images")?.length) {
+                this.images = work.get("images");
+              }
+            },
+            error: (err) => {
+              console.error("Error drawing image: ", err);
+            }
+          });
+        } else {
+          console.error("ImagineWork is not initialized or message is not complete.");
+        }
+      },
+      error: (err) => {
+        console.error("Error sending completion: ", err);
+      }
+    });
+  }
+
+  ngOnInit() {}
+}

+ 1 - 0
poem-life-app/src/app/tab4/tab4.page.html

@@ -7,5 +7,6 @@
 <ion-content [fullscreen]="true">
   <ion-list>
     <ion-button (click)="goToCreate()"><span>AI创作</span></ion-button>
+    <ion-button (click)="goToCreatePic()"><span>AI绘图</span></ion-button>
   </ion-list>
 </ion-content>

+ 3 - 0
poem-life-app/src/app/tab4/tab4.page.ts

@@ -19,6 +19,9 @@ export class Tab4Page implements OnInit {
   goToCreate(){
     this.router.navigateByUrl('/tabs/create')
   }
+  goToCreatePic(){
+    this.router.navigateByUrl('/tabs/createpic')
+  }
   ngOnInit() {
   }
 

+ 4 - 0
poem-life-app/src/app/tabs/tabs.routes.ts

@@ -29,6 +29,10 @@ export const routes: Routes = [
         path: 'create',
         loadComponent: () => import('../page-create/page-create.component').then( m => m.PageCreateComponent)
       },
+      {
+        path: 'createpic',
+        loadComponent: () => import('../page-createpic/page-createpic.component').then( m => m.PageCreatepicComponent)
+      },
       {
         path: '',
         redirectTo: '/tabs/tab1',