page.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. 'use client';
  2. import { useState, 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 { InpaintingCanvas } from '@/components/inpainting-canvas';
  12. import { apiClient, type JobInfo } from '@/lib/api';
  13. import { Loader2, X, Download } from 'lucide-react';
  14. import { downloadAuthenticatedImage } from '@/lib/utils';
  15. import { useLocalStorage } from '@/lib/hooks';
  16. type InpaintingFormData = {
  17. prompt: string;
  18. negative_prompt: string;
  19. source_image: string;
  20. mask_image: string;
  21. steps: number;
  22. cfg_scale: number;
  23. seed: string;
  24. sampling_method: string;
  25. strength: number;
  26. };
  27. const defaultFormData: InpaintingFormData = {
  28. prompt: '',
  29. negative_prompt: '',
  30. source_image: '',
  31. mask_image: '',
  32. steps: 20,
  33. cfg_scale: 7.5,
  34. seed: '',
  35. sampling_method: 'euler_a',
  36. strength: 0.75,
  37. };
  38. export default function InpaintingPage() {
  39. const [formData, setFormData] = useLocalStorage<InpaintingFormData>(
  40. 'inpainting-form-data',
  41. defaultFormData
  42. );
  43. const [loading, setLoading] = useState(false);
  44. const [error, setError] = useState<string | null>(null);
  45. const [jobInfo, setJobInfo] = useState<JobInfo | null>(null);
  46. const [generatedImages, setGeneratedImages] = useState<string[]>([]);
  47. const [loraModels, setLoraModels] = useState<string[]>([]);
  48. const [embeddings, setEmbeddings] = useState<string[]>([]);
  49. useEffect(() => {
  50. const loadModels = async () => {
  51. try {
  52. const [loras, embeds] = await Promise.all([
  53. apiClient.getModels('lora'),
  54. apiClient.getModels('embedding'),
  55. ]);
  56. setLoraModels(loras.map(m => m.name));
  57. setEmbeddings(embeds.map(m => m.name));
  58. } catch (err) {
  59. console.error('Failed to load models:', err);
  60. }
  61. };
  62. loadModels();
  63. }, []);
  64. const handleInputChange = (
  65. e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>
  66. ) => {
  67. const { name, value } = e.target;
  68. setFormData((prev) => ({
  69. ...prev,
  70. [name]: name === 'prompt' || name === 'negative_prompt' || name === 'seed' || name === 'sampling_method'
  71. ? value
  72. : Number(value),
  73. }));
  74. };
  75. const handleSourceImageChange = (image: string) => {
  76. setFormData((prev) => ({ ...prev, source_image: image }));
  77. setError(null);
  78. };
  79. const handleMaskImageChange = (image: string) => {
  80. setFormData((prev) => ({ ...prev, mask_image: image }));
  81. setError(null);
  82. };
  83. const pollJobStatus = async (jobId: string) => {
  84. const maxAttempts = 300;
  85. let attempts = 0;
  86. const poll = async () => {
  87. try {
  88. const status = await apiClient.getJobStatus(jobId);
  89. setJobInfo(status);
  90. if (status.status === 'completed') {
  91. let imageUrls: string[] = [];
  92. // Handle both old format (result.images) and new format (outputs)
  93. if (status.outputs && status.outputs.length > 0) {
  94. // New format: convert output URLs to authenticated image URLs with cache-busting
  95. imageUrls = status.outputs.map((output: any) => {
  96. const filename = output.filename;
  97. return apiClient.getImageUrl(jobId, filename);
  98. });
  99. } else if (status.result?.images && status.result.images.length > 0) {
  100. // Old format: convert image URLs to authenticated URLs
  101. imageUrls = status.result.images.map((imageUrl: string) => {
  102. // Extract filename from URL if it's already a full URL
  103. if (imageUrl.includes('/output/')) {
  104. const parts = imageUrl.split('/output/');
  105. if (parts.length === 2) {
  106. const filename = parts[1].split('?')[0]; // Remove query params
  107. return apiClient.getImageUrl(jobId, filename);
  108. }
  109. }
  110. // If it's just a filename, convert it directly
  111. return apiClient.getImageUrl(jobId, imageUrl);
  112. });
  113. }
  114. // Create a new array to trigger React re-render
  115. setGeneratedImages([...imageUrls]);
  116. setLoading(false);
  117. } else if (status.status === 'failed') {
  118. setError(status.error || 'Generation failed');
  119. setLoading(false);
  120. } else if (status.status === 'cancelled') {
  121. setError('Generation was cancelled');
  122. setLoading(false);
  123. } else if (attempts < maxAttempts) {
  124. attempts++;
  125. setTimeout(poll, 1000);
  126. } else {
  127. setError('Job polling timeout');
  128. setLoading(false);
  129. }
  130. } catch (err) {
  131. setError(err instanceof Error ? err.message : 'Failed to check job status');
  132. setLoading(false);
  133. }
  134. };
  135. poll();
  136. };
  137. const handleGenerate = async (e: React.FormEvent) => {
  138. e.preventDefault();
  139. if (!formData.source_image) {
  140. setError('Please upload a source image first');
  141. return;
  142. }
  143. if (!formData.mask_image) {
  144. setError('Please create a mask first');
  145. return;
  146. }
  147. setLoading(true);
  148. setError(null);
  149. setGeneratedImages([]);
  150. setJobInfo(null);
  151. try {
  152. const job = await apiClient.inpainting(formData);
  153. setJobInfo(job);
  154. const jobId = job.request_id || job.id;
  155. if (jobId) {
  156. await pollJobStatus(jobId);
  157. } else {
  158. setError('No job ID returned from server');
  159. setLoading(false);
  160. }
  161. } catch (err) {
  162. setError(err instanceof Error ? err.message : 'Failed to generate image');
  163. setLoading(false);
  164. }
  165. };
  166. const handleCancel = async () => {
  167. const jobId = jobInfo?.request_id || jobInfo?.id;
  168. if (jobId) {
  169. try {
  170. await apiClient.cancelJob(jobId);
  171. setLoading(false);
  172. setError('Generation cancelled');
  173. } catch (err) {
  174. console.error('Failed to cancel job:', err);
  175. }
  176. }
  177. };
  178. return (
  179. <AppLayout>
  180. <Header title="Inpainting" description="Edit images by masking areas and regenerating with AI" />
  181. <div className="container mx-auto p-6">
  182. <div className="grid gap-6 lg:grid-cols-2">
  183. {/* Left Panel - Canvas and Form */}
  184. <div className="space-y-6">
  185. <InpaintingCanvas
  186. onSourceImageChange={handleSourceImageChange}
  187. onMaskImageChange={handleMaskImageChange}
  188. />
  189. <Card>
  190. <CardContent className="pt-6">
  191. <form onSubmit={handleGenerate} className="space-y-4">
  192. <div className="space-y-2">
  193. <Label htmlFor="prompt">Prompt *</Label>
  194. <PromptTextarea
  195. value={formData.prompt}
  196. onChange={(value) => setFormData({ ...formData, prompt: value })}
  197. placeholder="Describe what to generate in the masked areas..."
  198. rows={3}
  199. loras={loraModels}
  200. embeddings={embeddings}
  201. />
  202. <p className="text-xs text-muted-foreground">
  203. Tip: Use {'<lora:name:weight>'} for LoRAs and embedding names directly
  204. </p>
  205. </div>
  206. <div className="space-y-2">
  207. <Label htmlFor="negative_prompt">Negative Prompt</Label>
  208. <PromptTextarea
  209. value={formData.negative_prompt || ''}
  210. onChange={(value) => setFormData({ ...formData, negative_prompt: value })}
  211. placeholder="What to avoid in the generated areas..."
  212. rows={2}
  213. loras={loraModels}
  214. embeddings={embeddings}
  215. />
  216. </div>
  217. <div className="space-y-2">
  218. <Label htmlFor="strength">
  219. Strength: {formData.strength.toFixed(2)}
  220. </Label>
  221. <Input
  222. id="strength"
  223. name="strength"
  224. type="range"
  225. value={formData.strength}
  226. onChange={handleInputChange}
  227. min={0}
  228. max={1}
  229. step={0.05}
  230. />
  231. <p className="text-xs text-muted-foreground">
  232. Lower values preserve more of the original image
  233. </p>
  234. </div>
  235. <div className="grid grid-cols-2 gap-4">
  236. <div className="space-y-2">
  237. <Label htmlFor="steps">Steps</Label>
  238. <Input
  239. id="steps"
  240. name="steps"
  241. type="number"
  242. value={formData.steps}
  243. onChange={handleInputChange}
  244. min={1}
  245. max={150}
  246. />
  247. </div>
  248. <div className="space-y-2">
  249. <Label htmlFor="cfg_scale">CFG Scale</Label>
  250. <Input
  251. id="cfg_scale"
  252. name="cfg_scale"
  253. type="number"
  254. value={formData.cfg_scale}
  255. onChange={handleInputChange}
  256. step={0.5}
  257. min={1}
  258. max={30}
  259. />
  260. </div>
  261. </div>
  262. <div className="space-y-2">
  263. <Label htmlFor="seed">Seed (optional)</Label>
  264. <Input
  265. id="seed"
  266. name="seed"
  267. value={formData.seed}
  268. onChange={handleInputChange}
  269. placeholder="Leave empty for random"
  270. />
  271. </div>
  272. <div className="space-y-2">
  273. <Label htmlFor="sampling_method">Sampling Method</Label>
  274. <select
  275. id="sampling_method"
  276. name="sampling_method"
  277. value={formData.sampling_method}
  278. onChange={handleInputChange}
  279. className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
  280. >
  281. <option value="euler">Euler</option>
  282. <option value="euler_a">Euler A</option>
  283. <option value="heun">Heun</option>
  284. <option value="dpm2">DPM2</option>
  285. <option value="dpm++2s_a">DPM++ 2S A</option>
  286. <option value="dpm++2m">DPM++ 2M</option>
  287. <option value="dpm++2mv2">DPM++ 2M V2</option>
  288. <option value="lcm">LCM</option>
  289. </select>
  290. </div>
  291. <div className="flex gap-2">
  292. <Button
  293. type="submit"
  294. disabled={loading || !formData.source_image || !formData.mask_image}
  295. className="flex-1"
  296. >
  297. {loading ? (
  298. <>
  299. <Loader2 className="h-4 w-4 animate-spin" />
  300. Generating...
  301. </>
  302. ) : (
  303. 'Generate'
  304. )}
  305. </Button>
  306. {loading && (
  307. <Button type="button" variant="destructive" onClick={handleCancel}>
  308. <X className="h-4 w-4" />
  309. Cancel
  310. </Button>
  311. )}
  312. </div>
  313. {error && (
  314. <div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
  315. {error}
  316. </div>
  317. )}
  318. {loading && jobInfo && (
  319. <div className="rounded-md bg-muted p-3 text-sm">
  320. <p>Job ID: {jobInfo.id || jobInfo.request_id || 'N/A'}</p>
  321. <p>Status: {jobInfo.status}</p>
  322. {jobInfo.progress !== undefined && (
  323. <p>Progress: {Math.round(jobInfo.progress * 100)}%</p>
  324. )}
  325. </div>
  326. )}
  327. </form>
  328. </CardContent>
  329. </Card>
  330. </div>
  331. {/* Right Panel - Generated Images */}
  332. <Card>
  333. <CardContent className="pt-6">
  334. <div className="space-y-4">
  335. <h3 className="text-lg font-semibold">Generated Images</h3>
  336. {generatedImages.length === 0 ? (
  337. <div className="flex h-96 items-center justify-center rounded-lg border-2 border-dashed border-border">
  338. <p className="text-muted-foreground">
  339. {loading ? 'Generating...' : 'Generated images will appear here'}
  340. </p>
  341. </div>
  342. ) : (
  343. <div className="grid gap-4">
  344. {generatedImages.map((image, index) => (
  345. <div key={index} className="relative group">
  346. <img
  347. src={image}
  348. alt={`Generated ${index + 1}`}
  349. className="w-full rounded-lg border border-border"
  350. />
  351. <Button
  352. size="icon"
  353. variant="secondary"
  354. className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity"
  355. onClick={() => {
  356. const authToken = localStorage.getItem('auth_token');
  357. const unixUser = localStorage.getItem('unix_user');
  358. downloadAuthenticatedImage(image, `inpainting-${Date.now()}-${index}.png`, authToken || undefined, unixUser || undefined)
  359. .catch(err => {
  360. console.error('Failed to download image:', err);
  361. });
  362. }}
  363. >
  364. <Download className="h-4 w-4" />
  365. </Button>
  366. </div>
  367. ))}
  368. </div>
  369. )}
  370. </div>
  371. </CardContent>
  372. </Card>
  373. </div>
  374. </div>
  375. </AppLayout>
  376. );
  377. }