Skip to content

taishikato/Tweet-Emotion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tweet Emotion 😆😶😢

This is a chrome extension to show the sentiment of each tweet on your timeline :)

This is build with GPT-3 by Open AI.

For demonstration, it shows below in the demo video.

  • Orange: Positive
  • Gray: Neutral
  • Blue: Negative

Demo Video

2-converted.mp4

How to install

  1. Create an API
  2. Open chrome://extensions/
  3. Click "Load unpacked" button on the top left.
  4. Choose the directry where you cloned this repository.

Create an API to call OpenAI

You need to build an API to call GPT-3 and put it here.

I quickly made it on Deno Deploy (I really wanna use Kuiq, but Supabase Edge Functions (Kuiq uses it behind the scene) currently doesn't recognize XMLHttpRequest even with import "https://deno.land/x/xhr@0.1.0/mod.ts", so I can't use it 🥲).

This is my code for your reference.

import "https://deno.land/x/xhr@0.1.0/mod.ts";
import { Configuration, OpenAIApi } from "https://esm.sh/openai@3.1.0"
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";

const router = new Router();
router.get('/', async (context) => {
  const configuration = new Configuration({
    apiKey: "YOUR OPEN AI API KEY"
  });
  const openai = new OpenAIApi(configuration);

  const u = new URL(context.request.url);
  const tweet = u.searchParams.get('tweet')

  if (!tweet) {
    context.response.body = 'no query'
    return
  }

  try {
    const response = await openai.createCompletion({
    model: "text-davinci-002",
    prompt: `Decide whether a Tweet's sentiment is positive, neutral, or negative.\n\nTweet: \"${tweet}\"\nSentiment:`,
    temperature: 0,
    max_tokens: 60,
    top_p: 1,
    frequency_penalty: 0.5,
    presence_penalty: 0,
  });

    context.response.body = response
  } catch(err) {
    context.response.body = {status: 'error', detail: err.message}
  }
})

const app = new Application();
app.use(oakCors()); // Enable CORS for All Routes
app.use(router.routes());

console.info("CORS-enabled web server listening on port 8000");
await app.listen({ port: 8000 });