<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Siddharth</title>
    <description>The latest articles on Forem by Siddharth (@siddharth0x).</description>
    <link>https://forem.com/siddharth0x</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F637336%2F23ea990c-39e5-4e6f-b26a-f190fe5cc373.jpeg</url>
      <title>Forem: Siddharth</title>
      <link>https://forem.com/siddharth0x</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/siddharth0x"/>
    <language>en</language>
    <item>
      <title>How I created a kids’ story creator using GPT3 ?</title>
      <dc:creator>Siddharth</dc:creator>
      <pubDate>Thu, 24 Nov 2022 19:01:54 +0000</pubDate>
      <link>https://forem.com/siddharth0x/how-i-created-a-kids-story-creator-using-gpt3--4400</link>
      <guid>https://forem.com/siddharth0x/how-i-created-a-kids-story-creator-using-gpt3--4400</guid>
      <description>&lt;p&gt;Being an active twitter user, I came across a plethora of AI tools and side projects people made primarily using stable diffusion and GPT3. I realized the huge potential of all these algorithms, and found the side projects to be really fun experiments. I decided to deep dive into GPT3, and use it to create an app which creates stories based on genres and characters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://gpt3stories.vercel.app/"&gt;Try it out here&lt;/a&gt; — &lt;a href="https://gpt3stories.vercel.app/"&gt;https://gpt3stories.vercel.app/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;OpenAI provides two types of AI models, GPT3 — which is a text generation model and DALL-E which is an image generation model. I read the GPT3 docs which are pretty succinct and easy to understand. OpenAI also provides 18$ worth of credits for the GPT3 models, which makes it easy to experiment and build projects. The key to getting the desired results from either of these models depends on how well you write the prompts.&lt;/p&gt;

&lt;p&gt;Prompt is basically a statement explaining what you want the algorithm to create.&lt;/p&gt;

&lt;p&gt;For e.g&lt;/p&gt;

&lt;p&gt;A prompt given to DALL-E could be “Create a Van Gogh style painting of a Coca-Cola can”.&lt;/p&gt;

&lt;p&gt;A prompt given to GPT-3 could be “Explain the history of Greece to a 5 year old”.&lt;/p&gt;

&lt;p&gt;OpenAI provides a playground where you can type prompts, see the results and tweak accordingly.&lt;/p&gt;

&lt;p&gt;I wanted to get stories from different genres having different characters, and to abstract away the requirement of typing a prompt which is still pretty esoteric for people not from a tech background had to design a UI which should appear self explanatory.&lt;/p&gt;

&lt;p&gt;I started designing in Figma, and choose to use simple and familiar radio buttons and checkboxes for the user to select a genre and a maximum of 2 characters.&lt;/p&gt;

&lt;p&gt;I choose to code the app in NextJS, it’s a full stack React framework and I could write an API which uses the openAI SDK to create results without the need to create a seperate expressJS project and host it.&lt;/p&gt;

&lt;p&gt;I have also used framer motion to create subtle animations. Further the background image that I have used is generated by DALL-E&lt;/p&gt;

&lt;p&gt;It’s incredibly easy to use the openAI nodejs SDK.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.API_KEY,
});

const openai = new OpenAIApi(configuration);

export default async function handler(req, res) {
  const body = req.body;
  console.log(body);
  const prompt = `Write a long form ${body.genre} story which has a ${body.characters[0]} and a ${body.characters[1]}`;
  console.log(prompt);
  const response = await openai.createCompletion({
    model: "text-davinci-002",
    prompt: prompt,
    temperature: 0.7,
    max_tokens: 3000,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0,
  });
  return res.status(200).json({ data: response.data.choices[0].text });
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s pretty much how you pass a dynamic prompt using string interpolation and return the response.&lt;/p&gt;

&lt;p&gt;Would love to collaborate on more such projects, you can find me on &lt;a href="https://twitter.com/siddharth0x"&gt;twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**&lt;br&gt;
&lt;a href="https://gpt3stories.vercel.app/"&gt;Try it out here&lt;/a&gt; — &lt;a href="https://gpt3stories.vercel.app/**"&gt;https://gpt3stories.vercel.app/**&lt;/a&gt;&lt;/p&gt;

</description>
      <category>openai</category>
      <category>gpt3</category>
      <category>nextjs</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Framer Motion - AnimatePresence</title>
      <dc:creator>Siddharth</dc:creator>
      <pubDate>Wed, 09 Nov 2022 09:45:12 +0000</pubDate>
      <link>https://forem.com/siddharth0x/framer-motion-animatepresence-33l2</link>
      <guid>https://forem.com/siddharth0x/framer-motion-animatepresence-33l2</guid>
      <description>&lt;p&gt;This is the third post in the series where I have been sharing my learnings as I learn FramerMotion. You can check out the first two posts here — (&lt;a href="https://dev.to/siddharth0x/framer-motion-layout-animations-50kh"&gt;post 1&lt;/a&gt;, &lt;a href="https://dev.to/siddharth0x/framer-motion-usemotionvalue-usetransform-1hml"&gt;post 2&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AnimatePresence **component facilitates running exit animations as elements are removed from the react tree(&lt;/strong&gt;unmounted**). This is actually pretty handy because react does not provide any lifecycle method to run some logic before a component is unmounted and delay the unmounting before the logic has completed running.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/animatepresence-1-9ibhqy"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work ?
&lt;/h2&gt;

&lt;p&gt;In the above example, the 2 divs have the same animation in animate prop. The second motion.div is wrapped within the AnimatePresence Component. We are toggling a state variable on button click to make the divs mount and unmount.&lt;/p&gt;

&lt;p&gt;AnimatePresence works by detecting the unmounting of the &lt;strong&gt;direct child or children. All the direct children should have a unique key prop, the significance of which we will discuss below.&lt;/strong&gt; If the direct child unmounts (the 2nd motion.div )(on button click in this example), the animation provided in the exit prop runs.&lt;/p&gt;

&lt;p&gt;The exit prop can even be provided to all the motion components within the direct child and those animations will run too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Animating Page transitions — React router Dom
&lt;/h2&gt;

&lt;p&gt;While transitioning from PAGE-1 to PAGE-2, we can run exit animations on PAGE — 1 and vice versa.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/animatepresence-2-roqmzt"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In the above example we have 2 routes, “/” which renders Home page, and “/&lt;strong&gt;profile&lt;/strong&gt;”, which renders profile page.&lt;/p&gt;

&lt;p&gt;As you switch between the pages, you would notice that the opacity of the word — “&lt;strong&gt;Home or Profile&lt;/strong&gt;” is animated to 0 (for, e.g. when going from home to profile, the word “home” disappears and then the page transition occurs).&lt;/p&gt;

&lt;p&gt;The routes are defined in a separate component called AnimatedRoutes, since we need to use the useLocation hook, which is only available in components wrapped under BrowserRouter.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export default function AnimatedRoutes() {&lt;br&gt;
const location = useLocation();&lt;br&gt;
console.log(location.pathname);&lt;br&gt;
return (&lt;br&gt;
&amp;lt;AnimatePresence mode="wait"&amp;gt;&lt;br&gt;
&amp;lt;Routes location={location} key={location.pathname}&amp;gt;&lt;br&gt;
&amp;lt;Route path="/" element={&amp;lt;Home /&amp;gt;} /&amp;gt;&lt;br&gt;
&amp;lt;Route path="/profile" element={&amp;lt;Profile /&amp;gt;} /&amp;gt;&lt;br&gt;
&amp;lt;/Routes&amp;gt;&lt;br&gt;
&amp;lt;/AnimatePresence&amp;gt;&lt;br&gt;
);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have wrapped the Routes component with AnimatePresence.&lt;/p&gt;

&lt;p&gt;mode=”wait” means the old component is animated out before the new component enters.&lt;/p&gt;

&lt;p&gt;As mentioned above, we need to give a &lt;strong&gt;unique key prop&lt;/strong&gt; to the direct child of AnimatePresence. &lt;strong&gt;location.pathname&lt;/strong&gt; (2 possible values in this example / or “/profile”) is used as the key. Whenever a component’s key changes, React considers that a new component, so the old component is unmounted and the new one is added to the react tree.&lt;/p&gt;

&lt;p&gt;With this info, let’s see how the exit animation works in the above example during page transitions.&lt;/p&gt;

&lt;p&gt;1 — When the current page is home, key’s value is “/”&lt;/p&gt;

&lt;p&gt;2 — When you navigate to profile, key’s value becomes “/profile”&lt;/p&gt;

&lt;p&gt;3 — As key changes, React unmounts the old component which is the home page&lt;/p&gt;

&lt;p&gt;4 — AnimatePresence detects this unmounting and runs exit animations on all the motion components nested inside the homepage (all which have the exit prop), in this case we have exit prop on 1 motion.div inside Home component.&lt;/p&gt;

&lt;p&gt;5 — Once the animation is complete, the page is transitioned to “/profile”.&lt;/p&gt;

&lt;p&gt;I will be sharing more such posts as I continue to explore Framer motion.&lt;/p&gt;

&lt;p&gt;Open to discussions on &lt;a href="https://twitter.com/siddharth0x"&gt;twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Framer motion — useMotionValue &amp; useTransform</title>
      <dc:creator>Siddharth</dc:creator>
      <pubDate>Thu, 03 Nov 2022 11:46:54 +0000</pubDate>
      <link>https://forem.com/siddharth0x/framer-motion-usemotionvalue-usetransform-1hml</link>
      <guid>https://forem.com/siddharth0x/framer-motion-usemotionvalue-usetransform-1hml</guid>
      <description>&lt;p&gt;Continuing the series of posts that I am planning to write as I explore framer motion (check out the previous post &lt;a href="https://dev.to/siddharth0x/framer-motion-layout-animations-50kh"&gt;here&lt;/a&gt;), this one explains my understanding of useMotionValue &amp;amp; useTransform using a few simple examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useMotionValue&lt;/strong&gt;&lt;br&gt;
In framer motion, to animate any element we use motion components, the motion component for a &lt;/p&gt; would be , for h1   and “motion” is imported from framer motion — import {motion} from “framer-motion”

&lt;p&gt;Internally framer motion uses motion values to track the state and velocity of all motion components. State is basically the value which is being a animated or in other words the value which changes to cause the animation.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/usemotionvalue-and-usetransform-basic-p6o88i"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In example 1 above, you can drag the div horizontally(along x axis), the drag constraints and 0 and 400px, so it cannot be dragged left, but can be dragged right till it reaches 400px.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fduqojm3j0plgpo9f8vv3.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fduqojm3j0plgpo9f8vv3.JPG" alt="Image showing css translateX property"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above picture shows the css styles applied to the the example 1 div, so framer motion internally uses translateX property to slide it along the X direction.&lt;/p&gt;

&lt;p&gt;so this translateX value is being tracked in a motion value created automatically.&lt;/p&gt;

&lt;p&gt;useMotionValue allows us to create a motion value manually and inject it into a motion component into the style attribute.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const x = useMotionValue(0);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates a motionvalue stored in x(could be named anything) and injected into the style attribute of the motion.div.&lt;/p&gt;

&lt;p&gt;so now the motionvalue x is in sync with the translateX value.&lt;/p&gt;

&lt;p&gt;We can listen to the changes in x and perform some actions.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect(() =&amp;gt; {&lt;br&gt;
x.onChange(() =&amp;gt; {&lt;br&gt;
console.log(x.get());&lt;br&gt;
});&lt;br&gt;
}, []);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have attached a listener to x using &lt;strong&gt;onChange **method and are printing the value (accessed using **x.get()&lt;/strong&gt;) to the console. so as you drag the div, you will values in the range of 0 to 400 being printed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyutamgsfnxcpdt1eu3ac.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyutamgsfnxcpdt1eu3ac.JPG" alt="Image showing the console output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The second example in above codesandbox shows how we can manually set the motion value and see changes in the UI. We are using a state variable which increases by 10 every time you click the button, causing a useEffect to run where we assign the state value to motion value using set method. useSpring can also be used in place on useMotionValue(remove the comment and try), the transition will be a more smoother spring animation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the codesandbox above, as you drag the div to right, it’s opacity starts reducing and once the div reaches the constraint of 400px, it’s 0.&lt;/p&gt;

&lt;p&gt;useMotionValue along with useTransform. useTransform transforms one or more motion values to give new motion values.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const x1 = useMotionValue(0);&lt;br&gt;
const opacity = useTransform(x1, [0, 400], [1, 0]);&lt;br&gt;
// useTransform(motion value, range of motion values, range of output values);&lt;br&gt;
x1 = 0 -&amp;gt; opacity = 1&lt;br&gt;
x1 = 400 -&amp;gt; opacity = 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have both x1 and opacity are then injected into the style attribute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So we are able to update visual properties (opacity in this case) without triggering react re-renders.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here we are using motion value to synchronize motion across 2 divs. As you drag the 1st div to right, the progress bar is updated.&lt;/p&gt;

&lt;p&gt;We are using useTransform to map the x value to the width of the progress bar.&lt;/p&gt;

&lt;p&gt;If you have any made any interactions using framer motion, do share them in the comments, would love to learn from those. Also you can DM me on &lt;a href="https://twitter.com/siddharth0x" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PS — the embedded codesandbox has all 4 examples.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Framer motion - Layout Animations</title>
      <dc:creator>Siddharth</dc:creator>
      <pubDate>Wed, 02 Nov 2022 06:26:39 +0000</pubDate>
      <link>https://forem.com/siddharth0x/framer-motion-layout-animations-50kh</link>
      <guid>https://forem.com/siddharth0x/framer-motion-layout-animations-50kh</guid>
      <description>&lt;p&gt;I have been learning framer motion lately, and came across this concept of layout animations which is simple yet effective in a way that you can add subtle animations to make layout changes look elegant and smooth.&lt;/p&gt;

&lt;p&gt;Before sharing some examples of how layout animations work, let me briefly describe what &lt;a href="https://www.framer.com/motion/"&gt;framer motion&lt;/a&gt; is — quoting their landing page, it’s a “A production-ready motion library for React” which helps react developers create declarative animations.&lt;/p&gt;




&lt;h2&gt;
  
  
  What are layout animations ?
&lt;/h2&gt;

&lt;p&gt;Layout animations basically animate any layout changes which occur when a component is re-rendered.&lt;/p&gt;

&lt;p&gt;For example, a change in the width or height of a div, flex items moving due to a change in justify-content property, re ordering of a list, etc.&lt;/p&gt;

&lt;p&gt;In order to add layout animation to any element, we just need to add the **layout **prop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s see some examples (these can also be found in the official docs).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a basic expanding div — where the width and height are changing when you click the div. A state variable tracks whether the div is expanded or not and based on the data attribute being true, the width and height are changed in the CSS. A caveat is, the layout prop is added even to the child div, when there’s no change in it’s layout, not adding it would cause it to distort during the animation, try removing it and clicking on the it. To overcome this, framer recommends to add layout prop to the child as well.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/layout-animation-1-kpzh7u"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This illustrates a change in layout caused due to a change in flexbox’s justify-content property. The value toggles between flex-start and flex-end based the state variable which toggles as you click on the div.&lt;/p&gt;

&lt;p&gt;The layout animation duration can be set in the transition prop.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/layout-animation-2-1bmigz"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Example 3&lt;/p&gt;

&lt;p&gt;This one shows how to implement shared layout animations.&lt;/p&gt;

&lt;p&gt;So the animation has 3 tabs, as you select any one of these the underline slides below the selected one.&lt;/p&gt;

&lt;p&gt;The underline div which gets re rendered every time the selected tab state changes has a prop called layoutId. Adding a layoutId prop causes the old component to automatically animate out.&lt;/p&gt;

&lt;p&gt;So when tomato is selected the underline below it the “current” component which has a layoutId value of “underline”, as you select lettuce, the UI is re-rendered, the underline below tomato becomes the old component as it is not rendered and is “animated out”, and it moves under lettuce.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/layout-animation-3-o38q6x"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I will be writing more such posts, as I explore more of framer-motion.&lt;/p&gt;

&lt;p&gt;You can find me on &lt;a href="https://twitter.com/siddharth0x"&gt;twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
