poem-picture.component.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Component, OnInit } from '@angular/core';
  2. import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
  3. import { IonTextarea, IonButton,IonInput } from "@ionic/angular/standalone";
  4. import { DalleOptions, ImagineWork, FmodeChatCompletion } from 'fmode-ng';
  5. @Component({
  6. selector: 'app-poem-picture',
  7. templateUrl: './poem-picture.component.html',
  8. styleUrls: ['./poem-picture.component.scss'],
  9. standalone: true,
  10. imports: [
  11. IonHeader, IonToolbar, IonTitle, IonContent,
  12. IonButton,
  13. IonInput,
  14. IonTextarea
  15. ],
  16. })
  17. export class PoemPictureComponent implements OnInit {
  18. /**
  19. * 用户输入的文本
  20. */
  21. userPrompt:string = ""
  22. promptInput(ev:any){
  23. console.log(ev.detail.value)
  24. if (ev.detail.value.length > 0){
  25. this.userPrompt = ev.detail.value;
  26. }
  27. }
  28. imagineWork:ImagineWork
  29. images:Array<string> = []
  30. constructor(
  31. ){
  32. // 示例任务,自己生成图片后请存储新的ID
  33. this.imagineWork = new ImagineWork("4mPR0IW1i5");
  34. this.imagineWork.fetchTask().then(work=>{
  35. if(work){
  36. this.imagineWork.id = work.id
  37. }
  38. this.images = this.imagineWork?.images || [];
  39. })
  40. }
  41. PictureDescResult:string = `` // 画面描述结果
  42. async createImage(){
  43. this.imagineWork = new ImagineWork();
  44. if (this.userPrompt.length > 0){
  45. // 文本生成
  46. let PromptTemplate = `您是一名专业的美术画家,请您根据下面提供的的内容,将其描述的画面、场景、人物、物品等用最简短的语言表达,直接写出画面,并且以中国的古风意境为主
  47. 诗文如下:
  48. ${this.userPrompt}
  49. `
  50. let completion = new FmodeChatCompletion([
  51. {role:"system",content:""},
  52. {role:"user",content:PromptTemplate}
  53. ])
  54. completion.sendCompletion().subscribe((message:any)=>{
  55. // 打印消息体
  56. console.log(message.content)
  57. // 赋值消息内容给组件内属性
  58. this.PictureDescResult = message.content
  59. if(message?.complete){ // 判断message为完成状态,则设置isComplete为完成
  60. // 图片生成
  61. let PicturePrompt = `${this.PictureDescResult}\n风格:中国古风。画面不带任何文字。突出色彩。`
  62. let options:DalleOptions = {prompt:PicturePrompt}
  63. this.imagineWork?.draw(options).subscribe(work=>{
  64. console.log("imagineWork",work?.toJSON())
  65. console.log("images",work?.get("images"))
  66. if(work?.get("images")?.length){
  67. this.images = work?.get("images");
  68. }
  69. })
  70. }
  71. })
  72. }
  73. else{
  74. this.userPrompt = "请提供您想要描述的诗文内容,我将根据其意境为您创作一幅古风画面的简短描述。"
  75. }
  76. }
  77. ngOnInit() {}
  78. }