use-react-summary
Version:
A powerful React hook for text summarization using Google's Generative AI API. Easily integrate advanced text summarization capabilities into your React applications.
2 lines (1 loc) • 1.06 kB
JavaScript
import e,{useState as t,useEffect as r}from'react';import{GoogleGenerativeAI as o}from'@google/generative-ai';import a from'react-markdown';const s=({text:s,words:n})=>{const[i,E]=t(''),[A,_]=t(!1),[l,m]=t(null);r((()=>{if(''===s)return;let e;n&&(e+=`within ${n} words`);(async()=>{_(!0),m(null);const t=new o('AIzaSyBLATho6gfsZok6xCgXwzxcZQrEyUKzifE').getGenerativeModel({model:'gemini-2.0-flash'}).startChat({generationConfig:{temperature:.9,topK:1,topP:1,maxOutputTokens:2048},safetySettings:[{category:'HARM_CATEGORY_HARASSMENT',threshold:'BLOCK_MEDIUM_AND_ABOVE'},{category:'HARM_CATEGORY_HATE_SPEECH',threshold:'BLOCK_MEDIUM_AND_ABOVE'},{category:'HARM_CATEGORY_SEXUALLY_EXPLICIT',threshold:'BLOCK_MEDIUM_AND_ABOVE'},{category:'HARM_CATEGORY_DANGEROUS_CONTENT',threshold:'BLOCK_MEDIUM_AND_ABOVE'}]});try{const r=(await t.sendMessage(`please summarize this text ${e}: ${s}`)).response;E(r.text())}catch(e){m('Error summarizing text')}finally{_(!1)}})()}),[s,n]);return{summarizeText:e.createElement(a,null,i),isLoading:A,error:l}};export{s as useSummary};