File size: 5,152 Bytes
e6ce630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { useState, useEffect, useCallback } from 'react';
import { Loader2 } from 'lucide-react';
import ModelSelector from './ModelSelector';
import PromptInput from './PromptInput';
import AspectRatioSelector from './AspectRatioSelector';
import ImageCountSlider from './ImageCountSlider';
import { Button } from '../ui/button';
import { v4 as uuidv4 } from 'uuid';

interface Image {
  url: string;
}

interface Batch {
  id: number | string; // Allow string for tempId
  prompt: string;
  width: number;
  height: number;
  model: string;
  images: Image[];
  status?: string;
  tempId?: string;
}

export default function GeneratorForm({ onGenerate, remixBatch }: { onGenerate: (batch: Batch, isPlaceholder: boolean) => void, remixBatch: Batch | null }) {
  const [prompt, setPrompt] = useState('');
  const [model, setModel] = useState('runware:100@1'); // FLUX SCHNELL as default
  const [aspectRatio, setAspectRatio] = useState('square');
  const [imageCount, setImageCount] = useState(1);
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    if (remixBatch) {
      setPrompt(remixBatch.prompt);
      setModel(remixBatch.model);
      setAspectRatio(getAspectRatioFromDimensions(remixBatch.width, remixBatch.height));
      setImageCount(remixBatch.images.length);
    }
  }, [remixBatch]);

  const getAspectRatioFromDimensions = (width: number, height: number) => {
    if (width === height) return 'square';
    if (width === 832 && height === 1216) return 'portrait';
    if (width === 1216 && height === 832) return 'landscape';
    return 'square'; // Default to square if dimensions don't match known ratios
  };

  const aspectRatios: { [key: string]: { width: number; height: number } } = {
    square: { width: 1024, height: 1024 },
    landscape: { width: 1216, height: 832 },
    portrait: { width: 832, height: 1216 }
  };

  const handleGenerate = useCallback(async () => {
    setError(null);
    const placeholderId = uuidv4();
    const placeholderBatch: Batch = {
      id: placeholderId,
      prompt,
      width: aspectRatios[aspectRatio as keyof typeof aspectRatios].width,
      height: aspectRatios[aspectRatio as keyof typeof aspectRatios].height,
      model,
      images: Array(imageCount).fill({ url: '/placeholder-image.png' }),
      status: 'pending',
      tempId: placeholderId
    };
    onGenerate(placeholderBatch, true);

    try {
      const response = await fetch('/api/generate-image', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          prompt,
          width: placeholderBatch.width,
          height: placeholderBatch.height,
          model,
          number_results: imageCount,
          placeholderId,
        }),
      });
      
      if (!response.ok) {
        throw new Error('Failed to generate image');
      }

      const reader = response.body?.getReader();
      if (!reader) {
        throw new Error('Failed to read response');
      }

      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
        
        const chunk = new TextDecoder().decode(value);
        const data = JSON.parse(chunk);
        
        if (data.batch) {
          onGenerate({ ...data.batch, tempId: placeholderId }, false);
        }
      }
    } catch (error) {
      console.error('Error generating image:', error);
      setError(error instanceof Error ? error.message : 'An unknown error occurred');
      onGenerate({ ...placeholderBatch, status: 'error' }, false);
    }
  }, [aspectRatio, prompt, model, imageCount, onGenerate, aspectRatios]);

  useEffect(() => {
    const handleKeyDown = (event: KeyboardEvent) => {
      if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
        event.preventDefault();
        handleGenerate();
      }
    };

    document.addEventListener('keydown', handleKeyDown);
    return () => {
      document.removeEventListener('keydown', handleKeyDown);
    };
  }, [handleGenerate]);

  return (
    <div className="layout-content-container flex flex-col w-full md:w-80">
      <PromptInput value={prompt} onChange={setPrompt} />
      <ModelSelector value={model} onChange={setModel} />
      <AspectRatioSelector value={aspectRatio} onChange={setAspectRatio} />
      <ImageCountSlider value={imageCount} onChange={setImageCount} />
      {error && (
        <div className="px-4 py-2 mb-3 text-red-500 bg-red-100 dark:bg-red-900 dark:text-red-100 rounded-md">
          {error}
        </div>
      )}
      <div className="flex px-4 py-3">
        <Button 
          variant="outline" 
          className="w-full justify-center bg-white dark:bg-gray-800 text-[#141414] dark:text-white font-bold"
          onClick={handleGenerate}
          disabled={isLoading}
        >
          {isLoading ? (
            <>
              <Loader2 className="mr-2 size-4 animate-spin" />
              Generating...
            </>
          ) : (
            'Generate'
          )}
        </Button>
      </div>
    </div>
  );
}