page.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. export default function Img2ImgPage() {
  41. const [formData, setFormData] = useLocalStorage<Img2ImgFormData>(
  42. 'img2img-form-data',
  43. defaultFormData
  44. );
  45. const [loading, setLoading] = useState(false);
  46. const [error, setError] = useState<string | null>(null);
  47. const [jobInfo, setJobInfo] = useState<JobInfo | null>(null);
  48. const [generatedImages, setGeneratedImages] = useState<string[]>([]);
  49. const [previewImage, setPreviewImage] = useState<string | null>(null);
  50. const [loraModels, setLoraModels] = useState<string[]>([]);
  51. const [embeddings, setEmbeddings] = useState<string[]>([]);
  52. const [selectedImage, setSelectedImage] = useState<File | string | null>(null);
  53. const [imageValidation, setImageValidation] = useState<any>(null);
  54. const [originalImage, setOriginalImage] = useState<string | null>(null);
  55. const [isResizing, setIsResizing] = useState(false);
  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 job = await apiClient.img2img(formData);
  216. setJobInfo(job);
  217. const jobId = job.request_id || job.id;
  218. if (jobId) {
  219. await pollJobStatus(jobId);
  220. } else {
  221. setError('No job ID returned from server');
  222. setLoading(false);
  223. }
  224. } catch (err) {
  225. setError(err instanceof Error ? err.message : 'Failed to generate image');
  226. setLoading(false);
  227. }
  228. };
  229. const handleCancel = async () => {
  230. const jobId = jobInfo?.request_id || jobInfo?.id;
  231. if (jobId) {
  232. try {
  233. await apiClient.cancelJob(jobId);
  234. setLoading(false);
  235. setError('Generation cancelled');
  236. } catch (err) {
  237. console.error('Failed to cancel job:', err);
  238. }
  239. }
  240. };
  241. return (
  242. <AppLayout>
  243. <Header title="Image to Image" description="Transform images with AI using text prompts" />
  244. <div className="container mx-auto p-6">
  245. <div className="grid gap-6 lg:grid-cols-2">
  246. {/* Left Panel - Form */}
  247. <Card>
  248. <CardContent className="pt-6">
  249. <form onSubmit={handleGenerate} className="space-y-4">
  250. <div className="space-y-2">
  251. <Label>Source Image *</Label>
  252. <ImageInput
  253. value={selectedImage}
  254. onChange={handleImageChange}
  255. onValidation={handleImageValidation}
  256. disabled={loading}
  257. maxSize={10 * 1024 * 1024} // 10MB
  258. accept="image/*"
  259. placeholder="Enter image URL or select a file"
  260. showPreview={true}
  261. />
  262. </div>
  263. <div className="space-y-2">
  264. <Label htmlFor="prompt">Prompt *</Label>
  265. <PromptTextarea
  266. value={formData.prompt}
  267. onChange={(value) => setFormData({ ...formData, prompt: value })}
  268. placeholder="Describe the transformation you want..."
  269. rows={3}
  270. loras={loraModels}
  271. embeddings={embeddings}
  272. />
  273. <p className="text-xs text-muted-foreground">
  274. Tip: Use &lt;lora:name:weight&gt; for LoRAs and embedding names directly
  275. </p>
  276. </div>
  277. <div className="space-y-2">
  278. <Label htmlFor="negative_prompt">Negative Prompt</Label>
  279. <PromptTextarea
  280. value={formData.negative_prompt || ''}
  281. onChange={(value) => setFormData({ ...formData, negative_prompt: value })}
  282. placeholder="What to avoid..."
  283. rows={2}
  284. loras={loraModels}
  285. embeddings={embeddings}
  286. />
  287. </div>
  288. <div className="space-y-2">
  289. <Label htmlFor="strength">
  290. Strength: {formData.strength.toFixed(2)}
  291. </Label>
  292. <Input
  293. id="strength"
  294. name="strength"
  295. type="range"
  296. value={formData.strength}
  297. onChange={handleInputChange}
  298. min={0}
  299. max={1}
  300. step={0.05}
  301. />
  302. <p className="text-xs text-muted-foreground">
  303. Lower values preserve more of the original image
  304. </p>
  305. </div>
  306. <div className="grid grid-cols-2 gap-4">
  307. <div className="space-y-2">
  308. <Label htmlFor="width">Width</Label>
  309. <Input
  310. id="width"
  311. name="width"
  312. type="number"
  313. value={formData.width}
  314. onChange={handleInputChange}
  315. step={64}
  316. min={256}
  317. max={2048}
  318. disabled={isResizing}
  319. />
  320. </div>
  321. <div className="space-y-2">
  322. <Label htmlFor="height">Height</Label>
  323. <Input
  324. id="height"
  325. name="height"
  326. type="number"
  327. value={formData.height}
  328. onChange={handleInputChange}
  329. step={64}
  330. min={256}
  331. max={2048}
  332. disabled={isResizing}
  333. />
  334. </div>
  335. </div>
  336. {isResizing && (
  337. <div className="text-sm text-muted-foreground flex items-center gap-2">
  338. <Loader2 className="h-4 w-4 animate-spin" />
  339. Resizing image...
  340. </div>
  341. )}
  342. <div className="grid grid-cols-2 gap-4">
  343. <div className="space-y-2">
  344. <Label htmlFor="steps">Steps</Label>
  345. <Input
  346. id="steps"
  347. name="steps"
  348. type="number"
  349. value={formData.steps}
  350. onChange={handleInputChange}
  351. min={1}
  352. max={150}
  353. />
  354. </div>
  355. <div className="space-y-2">
  356. <Label htmlFor="cfg_scale">CFG Scale</Label>
  357. <Input
  358. id="cfg_scale"
  359. name="cfg_scale"
  360. type="number"
  361. value={formData.cfg_scale}
  362. onChange={handleInputChange}
  363. step={0.5}
  364. min={1}
  365. max={30}
  366. />
  367. </div>
  368. </div>
  369. <div className="space-y-2">
  370. <Label htmlFor="seed">Seed (optional)</Label>
  371. <Input
  372. id="seed"
  373. name="seed"
  374. value={formData.seed}
  375. onChange={handleInputChange}
  376. placeholder="Leave empty for random"
  377. />
  378. </div>
  379. <div className="space-y-2">
  380. <Label htmlFor="sampling_method">Sampling Method</Label>
  381. <select
  382. id="sampling_method"
  383. name="sampling_method"
  384. value={formData.sampling_method}
  385. onChange={handleInputChange}
  386. className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
  387. >
  388. <option value="euler">Euler</option>
  389. <option value="euler_a">Euler A</option>
  390. <option value="heun">Heun</option>
  391. <option value="dpm2">DPM2</option>
  392. <option value="dpm++2s_a">DPM++ 2S A</option>
  393. <option value="dpm++2m">DPM++ 2M</option>
  394. <option value="dpm++2mv2">DPM++ 2M V2</option>
  395. <option value="lcm">LCM</option>
  396. </select>
  397. </div>
  398. <div className="flex gap-2">
  399. <Button type="submit" disabled={loading || !formData.image || (imageValidation && !imageValidation.isValid)} className="flex-1">
  400. {loading ? (
  401. <>
  402. <Loader2 className="h-4 w-4 animate-spin" />
  403. Generating...
  404. </>
  405. ) : (
  406. 'Generate'
  407. )}
  408. </Button>
  409. {loading && (
  410. <Button type="button" variant="destructive" onClick={handleCancel}>
  411. <X className="h-4 w-4" />
  412. Cancel
  413. </Button>
  414. )}
  415. </div>
  416. {error && (
  417. <div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
  418. {error}
  419. </div>
  420. )}
  421. {loading && jobInfo && (
  422. <div className="rounded-md bg-muted p-3 text-sm">
  423. <p>Job ID: {jobInfo.id || jobInfo.request_id || 'N/A'}</p>
  424. <p>Status: {jobInfo.status}</p>
  425. {jobInfo.progress !== undefined && (
  426. <p>Progress: {Math.round(jobInfo.progress * 100)}%</p>
  427. )}
  428. </div>
  429. )}
  430. </form>
  431. </CardContent>
  432. </Card>
  433. {/* Right Panel - Generated Images */}
  434. <Card>
  435. <CardContent className="pt-6">
  436. <div className="space-y-4">
  437. <h3 className="text-lg font-semibold">Generated Images</h3>
  438. {generatedImages.length === 0 ? (
  439. <div className="flex h-96 items-center justify-center rounded-lg border-2 border-dashed border-border">
  440. <p className="text-muted-foreground">
  441. {loading ? 'Generating...' : 'Generated images will appear here'}
  442. </p>
  443. </div>
  444. ) : (
  445. <div className="grid gap-4">
  446. {generatedImages.map((image, index) => (
  447. <div key={index} className="relative group">
  448. <img
  449. src={image}
  450. alt={`Generated ${index + 1}`}
  451. className="w-full rounded-lg border border-border"
  452. />
  453. <Button
  454. size="icon"
  455. variant="secondary"
  456. className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity"
  457. onClick={() => {
  458. const authToken = localStorage.getItem('auth_token');
  459. const unixUser = localStorage.getItem('unix_user');
  460. downloadAuthenticatedImage(image, `img2img-${Date.now()}-${index}.png`, authToken || undefined, unixUser || undefined)
  461. .catch(err => {
  462. console.error('Failed to download image:', err);
  463. // Fallback to regular download if authenticated download fails
  464. downloadImage(image, `img2img-${Date.now()}-${index}.png`);
  465. });
  466. }}
  467. >
  468. <Download className="h-4 w-4" />
  469. </Button>
  470. </div>
  471. ))}
  472. </div>
  473. )}
  474. </div>
  475. </CardContent>
  476. </Card>
  477. </div>
  478. </div>
  479. </AppLayout>
  480. );
  481. }