Skip to content

Commit

Permalink
code dump for frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
anzal1 committed Nov 13, 2023
0 parents commit f685129
Show file tree
Hide file tree
Showing 49 changed files with 14,825 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Find your Replicate API token at https://www.replicate.com/account
REPLICATE_API_TOKEN=

# Planet Scale MySQL database URL
# Do not wrap the URL value in quotes, becaus the Prisma DB client doesn't like that
DATABASE_URL=

# Use ngrok to expose your local server to the internet
# so the Replicate API can send it webhooks
#
# e.g. https://8db01fea81ad.ngrok.io
NGROK_HOST=


# Optional: Set a value for this to run the private Replicate deployment of the model
# instead of the public ControlNet Scribble model.
USE_REPLICATE_DEPLOYMENT=
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "next/core-web-vitals",
"rules": {
"@next/next/no-img-element": "off"
}
}
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test

on:
pull_request:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm test
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local
.env

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 Zeke Sikelianos

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 🖍️ Scribble Diffusion

Try it out at [scribblediffusion.com](https://scribblediffusion.com)

## How it works

This app is powered by:

🚀 [Replicate](https://replicate.com/?utm_source=project&utm_campaign=scribblediffusion), a platform for running machine learning models in the cloud.

🖍️ [ControlNet](https://replicate.com/rossjillian/controlnet?utm_source=project&utm_campaign=scribblediffusion), an open-source machine learning model that generates images from text and scribbles.

[Vercel](https://vercel.com/), a platform for running web apps.

⚡️ Next.js [server-side API routes](pages/api), for talking to the Replicate API.

👀 Next.js React components, for the browser UI.

🍃 [Tailwind CSS](https://tailwindcss.com/), for styles.

## Development

1. Install a recent version of [Node.js](https://nodejs.org/)
1. Copy your [Replicate API token](https://replicate.com/account?utm_source=project&utm_campaign=scribblediffusion) and set it in your environment:
```
echo "REPLICATE_API_TOKEN=<your-token-here>" > .env.local
```
1. Install dependencies and run the server:
```
npm install
npm run dev
```
1. Open [localhost:3000](http://localhost:3000) in your browser. That's it!
200 changes: 200 additions & 0 deletions components/canvas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import * as React from "react";
import { useEffect, useRef, useCallback, useState } from "react";
import { ReactSketchCanvas } from "react-sketch-canvas";
import {
Undo as UndoIcon,
Trash as TrashIcon,
Camera as CameraIcon,
X as CloseIcon,
File as FileIcon,
ClipboardX as DiscardIcon,
} from "lucide-react";
import Webcam from "react-webcam";

export default function Canvas({
startingPaths,
onScribble,
scribbleExists,
setScribbleExists,
}) {
const canvasRef = useRef(null);
const webCamRef = useRef(null);
const inputFileRef = useRef(null);

const [showCamera, setShowCamera] = React.useState(false);
const [imageUrl, setImageUrl] = useState(null);

const capture = useCallback(() => {
const imageSrc = webCamRef.current.getScreenshot();
setImageUrl(imageSrc);
setShowCamera(false);
onScribble(imageSrc);
}, []);

const handleImageChange = (event) => {
const file = event.target.files[0];

if (file && file.type === "image/png") {
const reader = new FileReader();
reader.onloadend = () => {
setImageUrl(reader.result);
onScribble(reader.result);
};
reader.readAsDataURL(file);
} else {
setImageUrl(null);
}
};

useEffect(() => {
// Hack to work around Firfox bug in react-sketch-canvas
// https://github.com/vinothpandian/react-sketch-canvas/issues/54
if (showCamera || imageUrl !== null) return;
setShowCamera(false);
document
.querySelector("#react-sketch-canvas__stroke-group-0")
?.removeAttribute("mask");

loadStartingPaths();
}, [showCamera,imageUrl]);

async function loadStartingPaths() {
await canvasRef.current.loadPaths(startingPaths);
setScribbleExists(true);
onChange();
}

const onChange = async () => {
const paths = await canvasRef.current.exportPaths();
localStorage.setItem("paths", JSON.stringify(paths, null, 2));

if (!paths.length) return;

setScribbleExists(true);

const data = await canvasRef.current.exportImage("png");
console.log("data", data);
onScribble(data);
};

const undo = () => {
canvasRef.current.undo();
};

const reset = () => {
setScribbleExists(false);
canvasRef.current.resetCanvas();
};

return (
<div className="relative">
{scribbleExists || (
<div>
<div className="absolute grid w-full h-full p-3 place-items-center pointer-events-none text-xl">
<span className="opacity-40">Draw something here.</span>
</div>
</div>
)}
{!showCamera && imageUrl === null && (
<ReactSketchCanvas
ref={canvasRef}
className="w-full aspect-square border-none cursor-crosshair"
strokeWidth={4}
strokeColor="black"
onChange={onChange}
withTimestamp={true}
/>
)}
{showCamera && imageUrl === null && (
<div className="aspect-square border-none cursor-crosshair">
<Webcam
audio={false}
ref={webCamRef}
screenshotFormat="image/png"
videoConstraints={{
facingMode: "environment",
}}
/>
</div>
)}

{!showCamera && imageUrl !== null && (
<div className="aspect-square border-none cursor-crosshair">
<img src={imageUrl} alt="webcam" />
</div>
)}

<div className="animate-in fade-in duration-700 text-left">
{scribbleExists && !showCamera && imageUrl === null && (
<>
<button className="lil-button" onClick={undo}>
<UndoIcon className="icon" />
Undo
</button>
<button className="lil-button" onClick={reset}>
<TrashIcon className="icon" />
Clear
</button>
</>
)}
{!showCamera && imageUrl === null && (
<>
<button
className="lil-button"
onClick={() => {
setShowCamera(true);
}}
>
<CameraIcon className="icon" />
Take Photo
</button>
<button
className="lil-button"
onClick={() => {
inputFileRef.current.click();
}}
>
<FileIcon className="icon" type="file" />
Upload Sketch
<input
ref={inputFileRef}
type="file"
style={{ display: "none" }}
accept=".png"
onChange={handleImageChange}
/>
</button>
</>
)}

{showCamera && (
<>
<button className="lil-button" onClick={capture}>
<CameraIcon className="icon" />
Capture Photo
</button>
<button className="lil-button" onClick={() => setShowCamera(false)}>
<CloseIcon className="icon" />
Cancel
</button>
</>
)}

{imageUrl !== null && (
<>
<button
className="lil-button"
onClick={() => {
setImageUrl(null);
setShowCamera(false);
}}
>
<DiscardIcon className="icon" />
Discard Image
</button>
</>
)}
</div>
</div>
);
}
9 changes: 9 additions & 0 deletions components/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function Footer({ error }) {
if (!error) return null;

return (
<div className="mx-auto w-full">
{error && <p className="bold text-red-500 pb-5">{error}</p>}
</div>
);
}
23 changes: 23 additions & 0 deletions components/footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Link from "next/link";
import Image from "next/image";
// import "react-tooltip/dist/react-tooltip.css";

const linkStyles =
"inline-block relative w-12 h-12 mx-2 opacity-40 hover:opacity-100 transition-all duration-200";
const imageStyles =
"p-3 hover:p-1 transition-all duration-200 hover:saturate-100";

export default function Footer() {
return (
<footer className="mt-20">
<div className="">
<p className="text-center">
Scribble Scribble , An AI powered tool to generate images from
sketches.
</p>

<nav className="text-center mt-16"></nav>
</div>
</footer>
);
}
9 changes: 9 additions & 0 deletions components/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import PulseLoader from "react-spinners/PulseLoader";

export default function Loader() {
return (
<div>
<PulseLoader size={12} margin={4} className="opacity-40" />
</div>
);
}
Loading

0 comments on commit f685129

Please sign in to comment.