Spaces:
Running
Running
File size: 7,925 Bytes
64db5cc 8e0957b a2a351d 1538aa3 4a52fb2 1538aa3 a2a351d 8e0957b a2a351d 8e0957b 1538aa3 8e0957b a2a351d 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b a2a351d 8e0957b 64db5cc 8e0957b 64db5cc 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b 1538aa3 8e0957b a2a351d 8e0957b 4a52fb2 a2a351d 8e0957b 1538aa3 8e0957b 1538aa3 16826f3 1538aa3 8e0957b 64db5cc 8e0957b 1609496 8e0957b 0ab3f48 f8c9e01 0ab3f48 f8c9e01 8e0957b |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
import { useEffect, useRef, useState } from 'react';
import { CONFIG } from '../config';
import {
getBlogPrompt,
getPromptGeneratePodcastScript,
} from '../utils/prompts';
//import { getSSEStreamAsync } from '../utils/utils';
import { EXAMPLES } from '../examples';
import { HfInference } from '@huggingface/inference';
import { isBlogMode } from '../utils/utils';
interface SplitContent {
thought: string;
codeBlock: string;
}
const getFromTo = (content: string, from: string, to: string): string => {
const firstSplit = content.split(from, 2);
if (firstSplit[1] !== undefined) {
const secondSplit = firstSplit[1].split(to, 1);
return secondSplit[0];
} else {
return '';
}
};
const splitContent = (content: string): SplitContent => {
return {
thought: getFromTo(content, '<think>', '</think>').trim(),
codeBlock: getFromTo(content, '```yaml', '```').trim(),
};
};
export const ScriptMaker = ({
setScript,
setBlogURL,
setBusy,
busy,
hfToken,
}: {
setScript: (script: string) => void;
setBlogURL: (url: string) => void;
setBusy: (busy: boolean) => void;
busy: boolean;
hfToken: string;
}) => {
const [model, setModel] = useState<string>(CONFIG.inferenceProviderModels[0]);
const [customModel, setCustomModel] = useState<string>(
CONFIG.inferenceProviderModels[0]
);
const usingModel = model === 'custom' ? customModel : model;
const [input, setInput] = useState<string>('');
const [note, setNote] = useState<string>(isBlogMode ? getBlogPrompt() : '');
const [thought, setThought] = useState<string>('');
const [isGenerating, setIsGenerating] = useState<boolean>(false);
const refThought = useRef<HTMLTextAreaElement | null>(null);
useEffect(() => {
setBusy(isGenerating);
}, [isGenerating]);
useEffect(() => {
setTimeout(() => {
// auto scroll
if (refThought.current) {
refThought.current.scrollTop = refThought.current.scrollHeight;
}
}, 10);
}, [thought]);
const generate = async () => {
setIsGenerating(true);
setThought('');
try {
let responseContent = '';
/*
const fetchResponse = await fetch(CONFIG.llmEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${hfToken}`,
},
body: JSON.stringify({
model: usingModel,
messages: [
{
role: 'user',
content: getPromptGeneratePodcastScript(input, note),
},
],
temperature: 0.3,
stream: true,
provider: CONFIG.inferenceProvider,
}),
});
if (fetchResponse.status !== 200) {
const body = await fetchResponse.json();
throw new Error(body?.error?.message || body?.error || 'Unknown error');
}
const chunks = getSSEStreamAsync(fetchResponse);
*/
const client = new HfInference(hfToken);
const chunks = client.chatCompletionStream({
model: usingModel,
messages: [
{
role: 'user',
content: getPromptGeneratePodcastScript(input, note),
},
],
temperature: 0.3,
stream: true,
provider: CONFIG.inferenceProvider,
});
for await (const chunk of chunks) {
// const stop = chunk.stop;
//if (chunk.error) {
// throw new Error(chunk.error?.message || 'Unknown error');
//}
const addedContent = chunk.choices[0].delta.content;
responseContent += addedContent;
const { thought, codeBlock } = splitContent(responseContent);
setThought(thought);
if (codeBlock.length > 0) {
setScript(codeBlock);
}
}
} catch (error) {
console.error(error);
alert(`ERROR: ${error}`);
}
setIsGenerating(false);
setTimeout(() => {
const generatePodcastBtn = document.getElementById(
'btn-generate-podcast'
);
generatePodcastBtn?.click();
}, 50);
};
return (
<div className="card bg-base-100 w-full shadow-xl">
<div className="card-body">
<h2 className="card-title">Step 1: Input information</h2>
<select
className="select select-bordered w-full"
disabled={isGenerating || busy}
onChange={(e) => {
const idx = parseInt(e.target.value);
const ex = EXAMPLES[idx];
if (ex) {
setInput(ex.input);
setNote(ex.note);
}
}}
>
<option selected disabled value={-1}>
Try one of these examples!!
</option>
{EXAMPLES.map((example, index) => (
<option key={index} value={index}>
{example.name}
</option>
))}
</select>
{isBlogMode && (
<>
<input
type="text"
placeholder="Blog URL"
className="input input-bordered w-full"
onChange={(e) => setBlogURL(e.target.value)}
/>
</>
)}
<textarea
className="textarea textarea-bordered w-full h-72 p-2"
placeholder="Type your input information here (an article, a document, etc)..."
value={input}
onChange={(e) => setInput(e.target.value)}
disabled={isGenerating || busy}
></textarea>
<textarea
className="textarea textarea-bordered w-full h-24 p-2"
placeholder="Optional note (the theme, tone, etc)..."
value={note}
onChange={(e) => setNote(e.target.value)}
disabled={isGenerating || busy}
></textarea>
<select
className="select select-bordered"
value={model}
onChange={(e) => setModel(e.target.value)}
disabled={isGenerating || busy}
>
{CONFIG.inferenceProviderModels.map((s) => (
<option key={s} value={s}>
{s}
</option>
))}
<option value="custom">Custom</option>
</select>
{model === 'custom' && (
<input
type="text"
placeholder="Use a custom model from HF Hub (must be supported by Inference Providers)"
className="input input-bordered w-full"
value={customModel}
onChange={(e) => setCustomModel(e.target.value)}
/>
)}
{thought.length > 0 && (
<>
<p>Thought process:</p>
<textarea
className="textarea textarea-bordered w-full h-24 p-2"
value={thought}
ref={refThought}
readOnly
></textarea>
</>
)}
<button
className="btn btn-primary mt-2"
onClick={generate}
disabled={isGenerating || busy || input.length < 10}
>
{isGenerating ? (
<>
<span className="loading loading-spinner loading-sm"></span>
Generating...
</>
) : (
'Generate script'
)}
</button>
<div role="alert" className="alert text-sm">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
className="stroke-info h-6 w-6 shrink-0"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
></path>
</svg>
<span>
The LLM may generate an incorrect YAML. If it fails on Step 2,
re-generate the script or adding a note to force it to follow YAML
format.
</span>
</div>
</div>
</div>
);
};
|