Skip to content

Commit 22ca650

Browse files
author
Nirmalya Ghosh
authored
Merge pull request #413 from RapidAPI/new/ai-text-shortener-using-ai
📦 NEW: Add AI Text Shortener app
2 parents 01c35e7 + 0022a8e commit 22ca650

File tree

16 files changed

+4249
-0
lines changed

16 files changed

+4249
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

ai-text-shortener-app/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts

ai-text-shortener-app/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
![Preview Image](https://user-images.githubusercontent.com/6391763/231090503-08da6cf8-9ff3-46e2-8a89-9118eeca1624.png)
2+
3+
# Text Shortener using AI
4+
5+
This project is a **TypeScript**-based web application that shortens input text using an AI API from [Rapid's API Hub](https://rapidapi.com/hub). The application is built with **Next.js**, **React**, and **Tailwind CSS**.
6+
7+
## 👀 Live Preview
8+
9+
To view a live preview of the application, click [here](https://example.com).
10+
11+
## 📖 Guide
12+
13+
[Read how we build this application](https://rapidapi.com/guides/building-a-text-shortener-app-using-ai-api-with-react-and-nextjs).
14+
15+
## 🛠️ Installation Steps
16+
17+
1. Download the `ai-text-shortener-app` directory. Click [here](https://download-directory.github.io/?url=https://github.com/RapidAPI/DevRel-Examples-External/tree/main/ai-text-shortener-app) to download it.
18+
19+
2. Unzip the downloaded file and navigate to the working directory.
20+
21+
```bash
22+
cd RapidAPI\ DevRel-Examples-External\ main\ ai-text-shortener-app/
23+
```
24+
25+
3. Install dependencies
26+
27+
```bash
28+
npm install
29+
```
30+
31+
4. Create `.env.local` file in root and add your RapidAPI key.
32+
33+
```bash
34+
RAPID_API_KEY=YOUR_RAPID_API_KEY
35+
RAPID_API_HOST=YOUR_RAPID_API_HOST
36+
37+
```
38+
39+
5. Run the app
40+
41+
```bash
42+
npm run dev
43+
```
44+
45+
You are all set! Open [localhost:3000](http://localhost:3000/) to see the app.
46+
47+
## 💡 Usage
48+
49+
To shorten a piece of text, simply enter the text into the input field and click the "Summarize" button. The shortened text will be displayed in the output field below.
50+
51+
## 🚀 API Usage
52+
53+
This application uses the AI API from Rapid's API Hub to shorten text. You can find more information about the API [here](https://rapidapi.com/tldrthishq-tldrthishq-default/api/tldrthis).
54+
55+
## 💻 Technologies Used
56+
57+
- Next.js
58+
- React
59+
- Tailwind CSS
60+
- TypeScript
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
interface FormProps {
2+
text: string;
3+
setText: (text: string) => void;
4+
setSummary: (summary: string) => void;
5+
loading: boolean;
6+
setLoading: (loading: boolean) => void;
7+
}
8+
9+
export const Form = ({
10+
text,
11+
setText,
12+
setSummary,
13+
loading,
14+
setLoading,
15+
}: FormProps) => {
16+
const handleClick = async () => {
17+
setLoading(true);
18+
19+
try {
20+
const response = await fetch("/api/summarize", {
21+
method: "POST",
22+
headers: {
23+
"Content-Type": "application/json",
24+
},
25+
body: JSON.stringify({
26+
text: text,
27+
min_length: 100,
28+
max_length: 300,
29+
}),
30+
});
31+
32+
const data = await response.json();
33+
34+
setSummary(data.summary);
35+
} catch (error) {
36+
console.log(error);
37+
} finally {
38+
setLoading(false);
39+
}
40+
};
41+
42+
return (
43+
<div className="p-4 border rounded flex flex-col space-y-4 bg-gray-50">
44+
<textarea
45+
value={text}
46+
onChange={(e) => setText(e.target.value)}
47+
placeholder="Text"
48+
className="border rounded p-4"
49+
rows={10}
50+
disabled={loading}
51+
/>
52+
<button
53+
onClick={handleClick}
54+
className="bg-black hover:bg-black/80 text-white rounded px-4 py-2 disabled:opacity-50 disabled:cursor-not-allowed"
55+
disabled={loading || !text.trim().length}
56+
>
57+
{loading ? "Loading..." : "Summarize"}
58+
</button>
59+
</div>
60+
);
61+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
interface TweetProps {
2+
summary: string;
3+
loading: boolean;
4+
}
5+
6+
export const Tweet = ({ summary = "", loading }: TweetProps) => {
7+
if (!summary.length) {
8+
return null;
9+
}
10+
11+
if (loading) {
12+
return (
13+
<div className="p-2 border space-y-2 rounded bg-white">Loading...</div>
14+
);
15+
}
16+
17+
return (
18+
<div className="border space-y-2 rounded bg-white overflow-hidden">
19+
<div className="p-2 border-b flex justify-end items-center space-x-2">
20+
<button
21+
className="text-xs text-black border rounded-sm px-2 py-1"
22+
onClick={() => {
23+
navigator.clipboard.writeText(summary);
24+
}}
25+
>
26+
Copy
27+
</button>
28+
<a
29+
className="text-xs bg-blue-500 text-white rounded-sm px-2 py-1"
30+
href={`https://twitter.com/intent/tweet?text=${summary}`}
31+
target="_blank"
32+
>
33+
Tweet
34+
</a>
35+
</div>
36+
<div className="p-2">{summary}</div>
37+
</div>
38+
);
39+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
}
5+
6+
module.exports = nextConfig

0 commit comments

Comments
 (0)