page.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. 'use client';
  2. import { useState, useRef, useEffect } from 'react';
  3. import { Header } from '@/components/header';
  4. import { AppLayout } from '@/components/layout';
  5. import { Button } from '@/components/ui/button';
  6. import { Input } from '@/components/ui/input';
  7. import { Textarea } from '@/components/ui/textarea';
  8. import { PromptTextarea } from '@/components/prompt-textarea';
  9. import { Label } from '@/components/ui/label';
  10. import { Card, CardContent } from '@/components/ui/card';
  11. import { ImageInput } from '@/components/ui/image-input';
  12. import { apiClient, type JobInfo } from '@/lib/api';
  13. import { Loader2, Download, X } from 'lucide-react';
  14. import { downloadImage, downloadAuthenticatedImage, fileToBase64 } from '@/lib/utils';
  15. import { useLocalStorage } from '@/lib/hooks';
  16. type Img2ImgFormData = {
  17. prompt: string;
  18. negative_prompt: string;
  19. image: string;
  20. strength: number;
  21. steps: number;
  22. cfg_scale: number;
  23. seed: string;
  24. sampling_method: string;
  25. width?: number;
  26. height?: number;
  27. };
  28. const defaultFormData: Img2ImgFormData = {
  29. prompt: '',
  30. negative_prompt: '',
  31. image: '',
  32. strength: 0.75,
  33. steps: 20,
  34. cfg_scale: 7.5,
  35. seed: '',
  36. sampling_method: 'euler_a',
  37. width: 512,
  38. height: 512,
  39. };
  40. function Img2ImgForm() {
  41. const [formData, setFormData] = useLocalStorage<Img2ImgFormData>(
  42. 'img2img-form-data',
  43. defaultFormData
  44. );
  45. const [loading, setLoading] = useState(false);
  46. const [jobInfo, setJobInfo] = useState<JobInfo | null>(null);
  47. const [generatedImages, setGeneratedImages] = useState<string[]>([]);
  48. const [previewImage, setPreviewImage] = useState<string | null>(null);
  49. const [loraModels, setLoraModels] = useState<string[]>([]);
  50. const [embeddings, setEmbeddings] = useState<string[]>([]);
  51. const [selectedImage, setSelectedImage] = useState<File | string | null>(null);
  52. const [imageValidation, setImageValidation] = useState<any>(null);
  53. const [originalImage, setOriginalImage] = useState<string | null>(null);
  54. const [isResizing, setIsResizing] = useState(false);
  55. const [error, setError] = useState<string | null>(null);
  56. useEffect(() => {
  57. const loadModels = async () => {
  58. try {
  59. const [loras, embeds] = await Promise.all([
  60. apiClient.getModels('lora'),
  61. apiClient.getModels('embedding'),
  62. ]);
  63. setLoraModels(loras.models.map(m => m.name));
  64. setEmbeddings(embeds.models.map(m => m.name));
  65. } catch (err) {
  66. console.error('Failed to load models:', err);
  67. }
  68. };
  69. loadModels();
  70. }, []);
  71. const handleInputChange = (
  72. e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>
  73. ) => {
  74. const { name, value } = e.target;
  75. setFormData((prev) => ({
  76. ...prev,
  77. [name]: name === 'prompt' || name === 'negative_prompt' || name === 'seed' || name === 'sampling_method'
  78. ? value
  79. : Number(value),
  80. }));
  81. };
  82. const handleImageChange = async (image: File | string | null) => {
  83. setSelectedImage(image);
  84. setError(null);
  85. if (!image) {
  86. setFormData(prev => ({ ...prev, image: '' }));
  87. setPreviewImage(null);
  88. setImageValidation(null);
  89. setOriginalImage(null);
  90. return;
  91. }
  92. try {
  93. let imageBase64: string;
  94. let previewUrl: string;
  95. if (image instanceof File) {
  96. // Convert File to base64
  97. imageBase64 = await fileToBase64(image);
  98. previewUrl = imageBase64;
  99. } else {
  100. // Use URL directly
  101. imageBase64 = image;
  102. previewUrl = image;
  103. }
  104. // Store original image for resizing
  105. setOriginalImage(imageBase64);
  106. setFormData(prev => ({ ...prev, image: imageBase64 }));
  107. setPreviewImage(previewUrl);
  108. } catch (err) {
  109. setError('Failed to process image');
  110. console.error('Image processing error:', err);
  111. }
  112. };
  113. // Auto-resize image when width or height changes
  114. useEffect(() => {
  115. const resizeImage = async () => {
  116. if (!originalImage || !formData.width || !formData.height) {
  117. return;
  118. }
  119. // Don't resize if we're already resizing
  120. if (isResizing) {
  121. return;
  122. }
  123. try {
  124. setIsResizing(true);
  125. const result = await apiClient.resizeImage(originalImage, formData.width, formData.height);
  126. setFormData(prev => ({ ...prev, image: result.image }));
  127. setPreviewImage(result.image);
  128. } catch (err) {
  129. console.error('Failed to resize image:', err);
  130. setError('Failed to resize image');
  131. } finally {
  132. setIsResizing(false);
  133. }
  134. };
  135. resizeImage();
  136. }, [formData.width, formData.height, originalImage]);
  137. const handleImageValidation = (result: any) => {
  138. setImageValidation(result);
  139. if (!result.isValid) {
  140. setError(result.error || 'Invalid image');
  141. } else {
  142. setError(null);
  143. }
  144. };
  145. const pollJobStatus = async (jobId: string) => {
  146. const maxAttempts = 300;
  147. let attempts = 0;
  148. const poll = async () => {
  149. try {
  150. const status = await apiClient.getJobStatus(jobId);
  151. setJobInfo(status);
  152. if (status.status === 'completed') {
  153. let imageUrls: string[] = [];
  154. // Handle both old format (result.images) and new format (outputs)
  155. if (status.outputs && status.outputs.length > 0) {
  156. // New format: convert output URLs to authenticated image URLs with cache-busting
  157. imageUrls = status.outputs.map((output: any) => {
  158. const filename = output.filename;
  159. return apiClient.getImageUrl(jobId, filename);
  160. });
  161. } else if (status.result?.images && status.result.images.length > 0) {
  162. // Old format: convert image URLs to authenticated URLs
  163. imageUrls = status.result.images.map((imageUrl: string) => {
  164. // Extract filename from URL if it's already a full URL
  165. if (imageUrl.includes('/output/')) {
  166. const parts = imageUrl.split('/output/');
  167. if (parts.length === 2) {
  168. const filename = parts[1].split('?')[0]; // Remove query params
  169. return apiClient.getImageUrl(jobId, filename);
  170. }
  171. }
  172. // If it's just a filename, convert it directly
  173. return apiClient.getImageUrl(jobId, imageUrl);
  174. });
  175. }
  176. // Create a new array to trigger React re-render
  177. setGeneratedImages([...imageUrls]);
  178. setLoading(false);
  179. } else if (status.status === 'failed') {
  180. setError(status.error || 'Generation failed');
  181. setLoading(false);
  182. } else if (status.status === 'cancelled') {
  183. setError('Generation was cancelled');
  184. setLoading(false);
  185. } else if (attempts < maxAttempts) {
  186. attempts++;
  187. setTimeout(poll, 2000);
  188. } else {
  189. setError('Job polling timeout');
  190. setLoading(false);
  191. }
  192. } catch (err) {
  193. setError(err instanceof Error ? err.message : 'Failed to check job status');
  194. setLoading(false);
  195. }
  196. };
  197. poll();
  198. };
  199. const handleGenerate = async (e: React.FormEvent) => {
  200. e.preventDefault();
  201. if (!formData.image) {
  202. setError('Please upload or select an image first');
  203. return;
  204. }
  205. // Check if image validation passed
  206. if (imageValidation && !imageValidation.isValid) {
  207. setError('Please fix the image validation errors before generating');
  208. return;
  209. }
  210. setLoading(true);
  211. setError(null);
  212. setGeneratedImages([]);
  213. setJobInfo(null);
  214. try {
  215. const requestData = {
  216. ...formData,
  217. };
  218. const job = await apiClient.img2img(requestData);
  219. setJobInfo(job);
  220. const jobId = job.request_id || job.id;
  221. if (jobId) {
  222. await pollJobStatus(jobId);
  223. } else {
  224. setError('No job ID returned from server');
  225. setLoading(false);
  226. }
  227. } catch (err) {
  228. setError(err instanceof Error ? err.message : 'Failed to generate image');
  229. setLoading(false);
  230. }
  231. };
  232. const handleCancel = async () => {
  233. const jobId = jobInfo?.request_id || jobInfo?.id;
  234. if (jobId) {
  235. try {
  236. await apiClient.cancelJob(jobId);
  237. setLoading(false);
  238. setError('Generation cancelled');
  239. } catch (err) {
  240. console.error('Failed to cancel job:', err);
  241. }
  242. }
  243. };
  244. return (
  245. <AppLayout>
  246. <Header title="Image to Image" description="Transform images with AI using text prompts" />
  247. <div className="container mx-auto p-6">
  248. <div className="grid gap-6 lg:grid-cols-2">
  249. {/* Left Panel - Form */}
  250. <Card>
  251. <CardContent className="pt-6">
  252. <form onSubmit={handleGenerate} className="space-y-4">
  253. <div className="space-y-2">
  254. <Label>Source Image *</Label>
  255. <ImageInput
  256. value={selectedImage}
  257. onChange={handleImageChange}
  258. onValidation={handleImageValidation}
  259. disabled={loading}
  260. maxSize={10 * 1024 * 1024} // 10MB
  261. accept="image/*"
  262. placeholder="Enter image URL or select a file"
  263. showPreview={true}
  264. />
  265. </div>
  266. <div className="space-y-2">
  267. <Label htmlFor="prompt">Prompt *</Label>
  268. <PromptTextarea
  269. value={formData.prompt}
  270. onChange={(value) => setFormData({ ...formData, prompt: value })}
  271. placeholder="Describe the transformation you want..."
  272. rows={3}
  273. loras={loraModels}
  274. embeddings={embeddings}
  275. />
  276. <p className="text-xs text-muted-foreground">
  277. Tip: Use &lt;lora:name:weight&gt; for LoRAs and embedding names directly
  278. </p>
  279. </div>
  280. <div className="space-y-2">
  281. <Label htmlFor="negative_prompt">Negative Prompt</Label>
  282. <PromptTextarea
  283. value={formData.negative_prompt || ''}
  284. onChange={(value) => setFormData({ ...formData, negative_prompt: value })}
  285. placeholder="What to avoid..."
  286. rows={2}
  287. loras={loraModels}
  288. embeddings={embeddings}
  289. />
  290. </div>
  291. <div className="space-y-2">
  292. <Label htmlFor="strength">
  293. Strength: {formData.strength.toFixed(2)}
  294. </Label>
  295. <Input
  296. id="strength"
  297. name="strength"
  298. type="range"
  299. value={formData.strength}
  300. onChange={handleInputChange}
  301. min={0}
  302. max={1}
  303. step={0.05}
  304. />
  305. <p className="text-xs text-muted-foreground">
  306. Lower values preserve more of the original image
  307. </p>
  308. </div>
  309. <div className="grid grid-cols-2 gap-4">
  310. <div className="space-y-2">
  311. <Label htmlFor="width">Width</Label>
  312. <Input
  313. id="width"
  314. name="width"
  315. type="number"
  316. value={formData.width}
  317. onChange={handleInputChange}
  318. step={64}
  319. min={256}
  320. max={2048}
  321. disabled={isResizing}
  322. />
  323. </div>
  324. <div className="space-y-2">
  325. <Label htmlFor="height">Height</Label>
  326. <Input
  327. id="height"
  328. name="height"
  329. type="number"
  330. value={formData.height}
  331. onChange={handleInputChange}
  332. step={64}
  333. min={256}
  334. max={2048}
  335. disabled={isResizing}
  336. />
  337. </div>
  338. </div>
  339. {isResizing && (
  340. <div className="text-sm text-muted-foreground flex items-center gap-2">
  341. <Loader2 className="h-4 w-4 animate-spin" />
  342. Resizing image...
  343. </div>
  344. )}
  345. <div className="grid grid-cols-2 gap-4">
  346. <div className="space-y-2">
  347. <Label htmlFor="steps">Steps</Label>
  348. <Input
  349. id="steps"
  350. name="steps"
  351. type="number"
  352. value={formData.steps}
  353. onChange={handleInputChange}
  354. min={1}
  355. max={150}
  356. />
  357. </div>
  358. <div className="space-y-2">
  359. <Label htmlFor="cfg_scale">CFG Scale</Label>
  360. <Input
  361. id="cfg_scale"
  362. name="cfg_scale"
  363. type="number"
  364. value={formData.cfg_scale}
  365. onChange={handleInputChange}
  366. step={0.5}
  367. min={1}
  368. max={30}
  369. />
  370. </div>
  371. </div>
  372. <div className="space-y-2">
  373. <Label htmlFor="seed">Seed (optional)</Label>
  374. <Input
  375. id="seed"
  376. name="seed"
  377. value={formData.seed}
  378. onChange={handleInputChange}
  379. placeholder="Leave empty for random"
  380. />
  381. </div>
  382. <div className="space-y-2">
  383. <Label htmlFor="sampling_method">Sampling Method</Label>
  384. <select
  385. id="sampling_method"
  386. name="sampling_method"
  387. value={formData.sampling_method}
  388. onChange={handleInputChange}
  389. className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
  390. >
  391. <option value="euler">Euler</option>
  392. <option value="euler_a">Euler A</option>
  393. <option value="heun">Heun</option>
  394. <option value="dpm2">DPM2</option>
  395. <option value="dpm++2s_a">DPM++ 2S A</option>
  396. <option value="dpm++2m">DPM++ 2M</option>
  397. <option value="dpm++2mv2">DPM++ 2M V2</option>
  398. <option value="lcm">LCM</option>
  399. </select>
  400. </div>
  401. <div className="flex gap-2">
  402. <Button type="submit" disabled={loading || !formData.image || (imageValidation && !imageValidation.isValid)} className="flex-1">
  403. {loading ? (
  404. <>
  405. <Loader2 className="h-4 w-4 animate-spin" />
  406. Generating...
  407. </>
  408. ) : (
  409. 'Generate'
  410. )}
  411. </Button>
  412. {loading && (
  413. <Button type="button" variant="destructive" onClick={handleCancel}>
  414. <X className="h-4 w-4" />
  415. Cancel
  416. </Button>
  417. )}
  418. </div>
  419. {error && (
  420. <div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
  421. {error}
  422. </div>
  423. )}
  424. {loading && jobInfo && (
  425. <div className="rounded-md bg-muted p-3 text-sm">
  426. <p>Job ID: {jobInfo.id || jobInfo.request_id || 'N/A'}</p>
  427. <p>Status: {jobInfo.status}</p>
  428. {jobInfo.progress !== undefined && (
  429. <p>Progress: {Math.round(jobInfo.progress * 100)}%</p>
  430. )}
  431. </div>
  432. )}
  433. </form>
  434. </CardContent>
  435. </Card>
  436. {/* Right Panel - Generated Images */}
  437. <Card>
  438. <CardContent className="pt-6">
  439. <div className="space-y-4">
  440. <h3 className="text-lg font-semibold">Generated Images</h3>
  441. {generatedImages.length === 0 ? (
  442. <div className="flex h-96 items-center justify-center rounded-lg border-2 border-dashed border-border">
  443. <p className="text-muted-foreground">
  444. {loading ? 'Generating...' : 'Generated images will appear here'}
  445. </p>
  446. </div>
  447. ) : (
  448. <div className="grid gap-4">
  449. {generatedImages.map((image, index) => (
  450. <div key={index} className="relative group">
  451. <img
  452. src={image}
  453. alt={`Generated ${index + 1}`}
  454. className="w-full rounded-lg border border-border"
  455. />
  456. <Button
  457. size="icon"
  458. variant="secondary"
  459. className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity"
  460. onClick={() => {
  461. const authToken = localStorage.getItem('auth_token');
  462. const unixUser = localStorage.getItem('unix_user');
  463. downloadAuthenticatedImage(image, `img2img-${Date.now()}-${index}.png`, authToken || undefined, unixUser || undefined)
  464. .catch(err => {
  465. console.error('Failed to download image:', err);
  466. // Fallback to regular download if authenticated download fails
  467. downloadImage(image, `img2img-${Date.now()}-${index}.png`);
  468. });
  469. }}
  470. >
  471. <Download className="h-4 w-4" />
  472. </Button>
  473. </div>
  474. ))}
  475. </div>
  476. )}
  477. </div>
  478. </CardContent>
  479. </Card>
  480. </div>
  481. </div>
  482. </AppLayout>
  483. );
  484. }
  485. export default function Img2ImgPage() {
  486. return <Img2ImgForm />;
  487. }