File size: 4,719 Bytes
62fead0 |
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
<script lang="ts">
import type { GradioClient, CaptionParams, CaptionResult, CaptionType, CaptionLength } from '$lib/types';
interface Props {
client: GradioClient | null;
currentImage?: Blob | null;
}
let { client = null, currentImage = null }: Props = $props();
let imageInput: HTMLInputElement;
let uploadedImage: File | null = $state(null);
let captionType: CaptionType = $state("Descriptive");
let captionLength: CaptionLength = $state("long");
let isGenerating = $state(false);
let result: CaptionResult | null = $state(null);
let error: string | null = $state(null);
const captionTypes: CaptionType[] = [
"Descriptive",
"Descriptive (Informal)",
"Training Prompt",
"MidJourney",
"Booru tag list",
"Booru-like tag list",
"Art Critic",
"Product Listing",
"Social Media Post"
];
const captionLengths: CaptionLength[] = [
"any",
"very short",
"short",
"medium-length",
"long",
"very long"
];
function handleFileChange(e: Event) {
const target = e.target as HTMLInputElement;
if (target.files && target.files[0]) {
uploadedImage = target.files[0];
}
}
async function handleSubmit(e: Event) {
e.preventDefault();
const imageToCaption = uploadedImage || currentImage;
if (!client || !imageToCaption) {
error = "Please upload an image or generate one above.";
return;
}
isGenerating = true;
error = null;
result = null;
try {
const output = await client.predict("/stream_chat", [
imageToCaption,
captionType,
captionLength,
[], // extra_options
"", // name_input
"" // custom_prompt
]);
const [prompt, caption] = output.data;
result = {
caption,
type: captionType,
length: captionLength
};
} catch (err) {
console.error(err);
error = `Caption generation failed: ${err}`;
} finally {
isGenerating = false;
}
}
</script>
<form class="caption-form" onsubmit={handleSubmit}>
<h3>Caption Image</h3>
<label for="imageInput">
Upload Image {currentImage ? 'or use generated image' : ''}
</label>
<input
type="file"
id="imageInput"
accept="image/*"
onchange={handleFileChange}
bind:this={imageInput}
disabled={isGenerating}
/>
<label for="captionType">Caption Type</label>
<select
id="captionType"
bind:value={captionType}
disabled={isGenerating}
>
{#each captionTypes as type}
<option value={type}>{type}</option>
{/each}
</select>
<label for="captionLength">Caption Length</label>
<select
id="captionLength"
bind:value={captionLength}
disabled={isGenerating}
>
{#each captionLengths as length}
<option value={length}>
{length.charAt(0).toUpperCase() + length.slice(1).replace('-', ' ')}
</option>
{/each}
</select>
<button
type="submit"
class="generate-button"
disabled={isGenerating || !client}
>
{isGenerating ? 'Generating Caption…' : 'Generate Caption'}
</button>
</form>
{#if error}
<div class="error-message">{error}</div>
{/if}
{#if result}
<div class="caption-result">
<h4>Generated Caption</h4>
<p><strong>Type:</strong> {result.type}</p>
<p><strong>Length:</strong> {result.length}</p>
<p><strong>Caption:</strong></p>
<p class="caption-text">{result.caption}</p>
</div>
{/if}
<style>
.caption-form {
margin-top: 2rem;
padding-top: 2rem;
border-top: 1px solid #eee;
}
h3 {
margin-top: 0;
margin-bottom: 1.5rem;
}
label {
font-weight: 600;
margin-bottom: 0.25rem;
display: block;
}
input[type="file"],
select {
width: 100%;
padding: 0.5rem 0.75rem;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-bottom: 1rem;
}
.generate-button {
background: #007bff;
color: #fff;
border: none;
padding: 0.6rem 1.4rem;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.2s;
}
.generate-button:hover:not(:disabled) {
background: #0056b3;
}
.generate-button:disabled {
background: #9ac7ff;
cursor: not-allowed;
}
.caption-result {
background: #f8f9fa;
padding: 1rem;
border-radius: 6px;
margin-top: 1rem;
}
.caption-result h4 {
margin-top: 0;
}
.caption-text {
font-style: italic;
}
.error-message {
color: #dc3545;
margin-top: 1rem;
padding: 0.5rem;
background: #f8d7da;
border-radius: 4px;
}
</style> |