Skip to content

Vaibhav99mishra/Tweet_Generator_Using_Replicate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Tweet_Generator_Using_Replicate

How to Build an AI Tweet Generator using Next.js and Replicate and Meta’s llama model

Social media platforms like Twitter have become an integral part of our lives, where brevity is key to make an impact. In this tutorial, we'll explore how to create an AI tweet generator using Next.js, a popular React framework for server-side rendering, and Replicate, a platform that provides access to powerful language models.

Prerequisites

To follow along with this tutorial, you should have the following prerequisites:

  1. Node.js and npm (Node Package Manager) installed on your machine.
  2. Basic knowledge of JavaScript and React.
  3. An active Replicate account and an API token. If you don't have one, you can sign up for an account on the Replicate website.

Setting Up the Next.js Project

Let's start by setting up a new Next.js project. Open your terminal and execute the following commands:

npx create-next-app ai-tweet-generator

✔ Would you like to use TypeScript with this project? … No
✔ Would you like to use ESLint with this project? … Yes
✔ Would you like to use Tailwind CSS with this project? … Yes
✔ Would you like to use `src/` directory with this project? … Yes
✔ Use App Router (recommended)? … No
✔ Would you like to customize the default import alias? … No

This will create a new Next.js project in a directory named ai-tweet-generator. Next, open the project in your preferred code editor.

cd ai-tweet-generator

Installing Dependencies

Next, we need to install the required dependencies for our project. In the terminal, navigate to the project directory and run the following command:

npm install replicate

Now, create a new file called .env in the root of your project and add the following environment variables:

REPLICATE_API_TOKEN=<paste-your-token-here>

Retrieve your API token from your Replicate account settings.

Creating the Backend

In your project, create a new file called tweet-generator.js inside the src/pages/api directory. This file will contain the code to generate AI tweets using the Replicate API.

Add the following code to tweet-generator.js:

import Replicate from "replicate";

export default async function handler(req, res) {
  const { topic, mood } = req.body;

  const replicate = new Replicate({
    auth: process.env.REPLICATE_API_TOKEN,
  });

  try {
    const output = await replicate.run(
      "a16z-infra/llama13b-v2-chat:6b4da803a2382c08868c5af10a523892f38e2de1aafb2ee55b020d9efef2fdb8",
      {
        input: {
          prompt: `Can you write a tweet on ${topic} and make it ${mood}`,
          system_prompt: "You write viral tweets, your write tweets that the twitter algorithm needs and every tweet goes virals, you use very less hashtags or sometimes don't even use hashtags. Most of the tweets you write are shitpost, it means they are not always factually correct but it doesn't matter as long as it goes viral"
        }
      }
    );

    res.status(200).json({ tweet: output });
  } catch (error) {
    console.error("AI tweet generation failed:", error);
    res.status(500).json({ error: "AI tweet generation failed" });
  }
}

The code sets up a handler function that initializes the Replicate client using your API token. It then extracts the topic and mood from the request body and calls the run method to generate tweets using the specified model.

If the generation is successful, the generated tweet is returned as a response. Otherwise, an error message is returned.

Link to model - Replicate

Creating the Frontend

Next, let's create a frontend interface to interact with our AI tweet generator. Open the src/pages/index.js file and replace its content with the following code:

import { useState } from "react";

export default function Home() {
  const [topic, setTopic] = useState("");
  const [mood, setMood] = useState("");
  const [tweet, setTweet] = useState("");
  const [loading, setLoading] = useState(false);

  const generateTweet = async () => {
    try {
      setLoading(true);
      const response = await fetch("/api/tweet-generator", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ topic, mood }),
      });

      const { tweet } = await response.json();
      setTweet(tweet);
    } catch (error) {
      console.error("Failed to generate tweet:", error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="min-h-screen flex items-center justify-center bg-gray-100">
      <div className="bg-white shadow-lg rounded-lg p-6 space-y-4 w-96">
        <h1 className="text-3xl font-semibold text-center">AI Tweet Generator</h1>
        <div className="flex flex-col space-y-2">
          <label htmlFor="topic" className="text-lg">
            Topic:
          </label>
          <input
            type="text"
            id="topic"
            value={topic}
            onChange={(e) => setTopic(e.target.value)}
            className="border rounded-md py-2 px-3 focus:outline-none focus:ring-2 focus:ring-blue-400"
          />
        </div>
        <div className="flex flex-col space-y-2">
          <label htmlFor="mood" className="text-lg">
            Mood:
          </label>
          <input
            type="text"
            id="mood"
            value={mood}
            onChange={(e) => setMood(e.target.value)}
            className="border rounded-md py-2 px-3 focus:outline-none focus:ring-2 focus:ring-blue-400"
          />
        </div>
        <button
          onClick={generateTweet}
          className="bg-blue-500 text-white rounded-md py-2 px-4 w-full hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-400"
          disabled={loading}
        >
          {loading ? "Generating..." : "Generate Tweet"}
        </button>
        {tweet && <p className="text-center text-lg font-medium">{tweet}</p>}
      </div>
    </div>
  );
}

This code sets up a basic React component for the homepage. It includes inputs for the topic and mood of the tweet and a button to trigger the tweet generation. The generated tweet is displayed below the button.

When the "Generate Tweet" button is clicked, a request is sent to the /api/tweet-generator endpoint we created earlier. The topic and mood are sent as JSON in the request body. If the request is successful, the generated tweet is displayed.

Starting the Development Server

With the code in place, we can now start the Next.js development server. Run the following command in your terminal:

npm run dev

Once the server starts, open your browser and navigate to http://localhost:3000. You should see the "AI Tweet Generator" heading and the input fields for the topic and mood.

Enter a topic and a mood for the tweet, then click the "Generate Tweet" button. After a short delay, the generated tweet will appear below the button.

Congratulations! You have successfully created an AI tweet generator using Next.js and Replicate.

Conclusion

In this tutorial, we explored how to build an AI tweet generator using Next.js and Replicate. We learned how to authenticate with the Replicate API, generate AI tweets, and create a frontend interface to interact with the AI model.

With this foundation, you can further explore the capabilities of different language models, experiment with diverse prompts, and create a wide range of AI-generated tweets. Now you have the power to create engaging and creative tweets with the help of AI. Happy tweeting!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published