picture.component.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Component, OnInit } from '@angular/core';
  2. import { IonHeader, IonToolbar, IonTitle, IonContent, IonItem, IonIcon, IonButtons } from '@ionic/angular/standalone';
  3. import { IonTextarea, IonButton,IonInput } from "@ionic/angular/standalone";
  4. import { DalleOptions, ImagineWork, FmodeChatCompletion } from 'fmode-ng';
  5. import { Router } from '@angular/router';
  6. import {chevronBack} from 'ionicons/icons';
  7. addIcons({chevronBack})
  8. import { addIcons } from 'ionicons';
  9. import { CloudObject, CloudUser } from 'src/lib/ncloud';
  10. @Component({
  11. selector: 'picture',
  12. templateUrl: './picture.component.html',
  13. styleUrls: ['./picture.component.scss'],
  14. standalone: true,
  15. imports: [
  16. IonHeader, IonToolbar, IonTitle, IonContent,
  17. IonButton,
  18. IonInput,
  19. IonTextarea,IonItem,IonIcon,IonButtons
  20. ],
  21. })
  22. export class PictureComponent implements OnInit {
  23. userPrompt: string = "请描述您的绘画想法"; // 用户输入的绘画想法
  24. promptInput(ev: any) {
  25. this.userPrompt = ev.detail.value; // 更新用户输入的绘画想法
  26. }
  27. imagineWork: ImagineWork | undefined; // 用于处理图像生成的实例
  28. images: Array<string> = []; // 存储生成的图像数组
  29. constructor(private router: Router) {
  30. // 初始化ImagineWork实例并获取初始任务
  31. this.imagineWork = new ImagineWork("");
  32. this.imagineWork.fetchTask().then(work => {
  33. this.images = this.imagineWork?.images || []; // 获取已有图像
  34. this.currentadivse = new CloudObject('padvise');
  35. });
  36. }
  37. PictureDescResult: string = ""; // 存储画面描述结果
  38. async createImage() {
  39. this.imagineWork = new ImagineWork(); // 创建新的ImagineWork实例
  40. // 文本生成
  41. let PromptTemplate = `您是一名专业的美术画家,请您根据用户的绘画想法,将其描述的画面、场景、人物、物品等用最简短的语言表达,直接写出画面。
  42. 用户的绘画想法如下:
  43. ${this.userPrompt}
  44. `;
  45. let completion = new FmodeChatCompletion([
  46. { role: "system", content: "" },
  47. { role: "user", content: PromptTemplate }
  48. ]);
  49. // 发送请求并处理响应
  50. completion.sendCompletion().subscribe((message: any) => {
  51. // 打印消息体
  52. console.log(message.content);
  53. // 赋值消息内容给组件内属性
  54. this.PictureDescResult = message.content;
  55. // 判断消息是否完成
  56. if (message?.complete) {
  57. // 图片生成
  58. let PicturePrompt = `${this.PictureDescResult}\n画面不带任何文字。`;
  59. let options: DalleOptions = { prompt: PicturePrompt };
  60. // 生成图像
  61. this.imagineWork?.draw(options).subscribe(work => {
  62. console.log("imagineWork", work?.toJSON());
  63. console.log("images", work?.get("images"));
  64. // 更新生成的图像
  65. if (work?.get("images")?.length) {
  66. this.images = work?.get("images");
  67. this.addadvise();
  68. }
  69. });
  70. }
  71. });
  72. }
  73. ngOnInit() {}
  74. gopage(){
  75. this.router.navigate(['/tabs/tab1'])
  76. }
  77. currentadivse: CloudObject | undefined; //
  78. advisedata: any = {};
  79. async addadvise() {
  80. let currentUser = new CloudUser();
  81. if (currentUser?.id) {
  82. this.currentadivse?.addPointer('user', '_User', currentUser.id);
  83. }
  84. if(this.PictureDescResult){
  85. this.advisedata['content']=this.PictureDescResult;
  86. this.advisedata['type']='picture';
  87. this.advisedata['idea']=this.userPrompt;
  88. }
  89. if(this.images){
  90. this.advisedata['picture']=this.images[this.images.length-1];
  91. }
  92. this.currentadivse?.set(this.advisedata);
  93. await this.currentadivse?.save();
  94. }
  95. }