├── .gitignore
├── .metadata
├── LICENSE
├── README.md
├── analysis_options.yaml
├── demo.gif
├── firebase.json
├── firestore.indexes.json
├── firestore.rules
├── flow.png
├── functions
├── .eslintrc.js
├── .gitignore
├── package-lock.json
├── package.json
├── src
│ └── index.ts
├── tsconfig.dev.json
└── tsconfig.json
├── lib
├── app_state.dart
├── blocs
│ ├── app_cubit.dart
│ ├── app_events.dart
│ └── app_states.dart
├── main.dart
├── models
│ └── prediction_model.dart
├── repository
│ └── prediction_repo.dart
├── services
│ └── backend_service.dart
└── ui
│ ├── draw
│ ├── canvas.dart
│ ├── drawing_page.dart
│ ├── stroke.dart
│ └── stroke_options.dart
│ ├── home
│ └── home_page.dart
│ └── result
│ └── result_page.dart
├── pubspec.lock
├── pubspec.yaml
├── sample_01.png
├── sample_02.png
├── sample_03.png
├── storage.rules
└── web
├── favicon.png
├── icons
├── Icon-192.png
├── Icon-512.png
├── Icon-maskable-192.png
└── Icon-maskable-512.png
├── index.html
└── manifest.json
/.gitignore:
--------------------------------------------------------------------------------
1 | # Miscellaneous
2 | *.class
3 | *.log
4 | *.pyc
5 | *.swp
6 | .DS_Store
7 | .atom/
8 | .buildlog/
9 | .history
10 | .svn/
11 | migrate_working_dir/
12 |
13 | # IntelliJ related
14 | *.iml
15 | *.ipr
16 | *.iws
17 | .idea/
18 |
19 | # The .vscode folder contains launch configuration and tasks you configure in
20 | # VS Code which you may wish to be included in version control, so this line
21 | # is commented out by default.
22 | #.vscode/
23 |
24 | # Flutter/Dart/Pub related
25 | **/doc/api/
26 | **/ios/Flutter/.last_build_id
27 | .dart_tool/
28 | .flutter-plugins
29 | .flutter-plugins-dependencies
30 | .packages
31 | .pub-cache/
32 | .pub/
33 | /build/
34 |
35 | # Symbolication related
36 | app.*.symbols
37 |
38 | # Obfuscation related
39 | app.*.map.json
40 |
41 | # Android Studio will place build artifacts here
42 | /android/app/debug
43 | /android/app/profile
44 | /android/app/release
45 |
46 | /resources
47 | /lib/firebase_options.dart
48 | .firebaserc
49 | .firebase/
50 |
--------------------------------------------------------------------------------
/.metadata:
--------------------------------------------------------------------------------
1 | # This file tracks properties of this Flutter project.
2 | # Used by Flutter tool to assess capabilities and perform upgrades etc.
3 | #
4 | # This file should be version controlled.
5 |
6 | version:
7 | revision: 9944297138845a94256f1cf37beb88ff9a8e811a
8 | channel: stable
9 |
10 | project_type: app
11 |
12 | # Tracks metadata for the flutter migrate command
13 | migration:
14 | platforms:
15 | - platform: root
16 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
17 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
18 | - platform: android
19 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
20 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
21 | - platform: ios
22 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
23 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
24 | - platform: linux
25 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
26 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
27 | - platform: macos
28 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
29 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
30 | - platform: web
31 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
32 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
33 | - platform: windows
34 | create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
35 | base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
36 |
37 | # User provided section
38 |
39 | # List of Local paths (relative to this file) that should be
40 | # ignored by the migrate tool.
41 | #
42 | # Files that are not part of the templates will be ignored by default.
43 | unmanaged_files:
44 | - 'lib/main.dart'
45 | - 'ios/Runner.xcodeproj/project.pbxproj'
46 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Flutter Scribble Diffusion
2 |
3 | Turn your scribbles into detailed images with AI.
4 |
5 | Demo: [flutter-scribble.web.app](https://flutter-scribble.web.app)
6 |
7 | ---
8 |
9 |
10 | ## Sample Generations
11 |
12 | [](https://flutter-scribble.web.app/#/share/mezhk5rvdzbtrg3lriiufkr7sy)
13 | [link](https://flutter-scribble.web.app/#/share/mezhk5rvdzbtrg3lriiufkr7sy)
14 |
15 | [](https://flutter-scribble.web.app/#/share/whf7xnpqcnb5bj6udndqde7sbq)
16 | [link](https://flutter-scribble.web.app/#/share/whf7xnpqcnb5bj6udndqde7sbq)
17 |
18 | [](https://flutter-scribble.web.app/#/share/4e425d3xdbftpgry6c4ta3asqe)
19 | [link](https://flutter-scribble.web.app/#/share/4e425d3xdbftpgry6c4ta3asqe)
20 |
21 | ## Powered by
22 |
23 | 🚀 [Replicate](https://replicate.com/), a platform for running machine learning models in the cloud.
24 |
25 | 🖍️ [ControlNet](https://replicate.com/jagilley/controlnet-scribble/), an open-source machine learning model that generates images from text and scribbles.
26 |
27 | 🐦 [Flutter](https://flutter.dev/), an open source framework by Google for building beautiful, natively compiled, multi-platform applications from a single codebase.
28 |
29 | 🔥 [Firebase](https://firebase.google.com/), an app development platform that helps you build and grow apps and games users love.
30 |
31 | 🔥 ⚡ [Cloud Functions for Firebase](https://firebase.google.com/docs/functions), for talking to the Replicate API.
32 |
33 | 🔥 📝 [Cloud Firestore](https://firebase.google.com/products/firestore), for storing predictions from Replicate.
34 |
35 | 🔥 ☁️ [Cloud Storage for Firebase](https://firebase.google.com/products/storage), for saving generated images from Replicate.
36 |
37 | 🔥 🔐 [Firebase Authentication](https://firebase.google.com/docs/auth), for handling anonymous authentication.
38 |
39 | 🔥 🕸️ [Firebase Hosting](https://firebase.google.com/docs/hosting), for hosting the Flutter web app.
40 |
41 | 🖌️ [Perfect Freehand Dart](https://github.com/steveruizok/perfect-freehand-dart), for scribbles in Flutter.
42 |
43 | ---
44 |
45 | 
46 |
47 | ---
48 |
49 | Inspired by [🖍️ Scribble Diffusion](https://github.com/replicate/scribble-diffusion)
50 |
--------------------------------------------------------------------------------
/analysis_options.yaml:
--------------------------------------------------------------------------------
1 | # This file configures the analyzer, which statically analyzes Dart code to
2 | # check for errors, warnings, and lints.
3 | #
4 | # The issues identified by the analyzer are surfaced in the UI of Dart-enabled
5 | # IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
6 | # invoked from the command line by running `flutter analyze`.
7 |
8 | # The following line activates a set of recommended lints for Flutter apps,
9 | # packages, and plugins designed to encourage good coding practices.
10 | include: package:flutter_lints/flutter.yaml
11 |
12 | linter:
13 | # The lint rules applied to this project can be customized in the
14 | # section below to disable rules from the `package:flutter_lints/flutter.yaml`
15 | # included above or to enable additional rules. A list of all available lints
16 | # and their documentation is published at
17 | # https://dart-lang.github.io/linter/lints/index.html.
18 | #
19 | # Instead of disabling a lint rule for the entire project in the
20 | # section below, it can also be suppressed for a single line of code
21 | # or a specific dart file by using the `// ignore: name_of_lint` and
22 | # `// ignore_for_file: name_of_lint` syntax on the line or in the file
23 | # producing the lint.
24 | rules:
25 | # avoid_print: false # Uncomment to disable the `avoid_print` rule
26 | # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
27 |
28 | # Additional information about this file can be found at
29 | # https://dart.dev/guides/language/analysis-options
30 |
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lahirumaramba/flutter_scribble/13f96c24238453eb78d08c8f9ecf6dd207c017b9/demo.gif
--------------------------------------------------------------------------------
/firebase.json:
--------------------------------------------------------------------------------
1 | {
2 | "firestore": {
3 | "rules": "firestore.rules",
4 | "indexes": "firestore.indexes.json"
5 | },
6 | "functions": [
7 | {
8 | "source": "functions",
9 | "codebase": "default",
10 | "ignore": [
11 | "node_modules",
12 | ".git",
13 | "firebase-debug.log",
14 | "firebase-debug.*.log"
15 | ],
16 | "predeploy": [
17 | "npm --prefix \"$RESOURCE_DIR\" run lint",
18 | "npm --prefix \"$RESOURCE_DIR\" run build"
19 | ]
20 | }
21 | ],
22 | "emulators": {
23 | "functions": {
24 | "port": 5001
25 | },
26 | "firestore": {
27 | "port": 8080
28 | },
29 | "storage": {
30 | "port": 9199
31 | },
32 | "ui": {
33 | "enabled": true
34 | },
35 | "singleProjectMode": true
36 | },
37 | "storage": {
38 | "rules": "storage.rules"
39 | },
40 | "hosting": {
41 | "public": "build/web",
42 | "ignore": [
43 | "firebase.json",
44 | "**/.*",
45 | "**/node_modules/**"
46 | ],
47 | "rewrites": [
48 | {
49 | "source": "**",
50 | "destination": "/index.html"
51 | }
52 | ]
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/firestore.indexes.json:
--------------------------------------------------------------------------------
1 | {
2 | "indexes": [],
3 | "fieldOverrides": []
4 | }
5 |
--------------------------------------------------------------------------------
/firestore.rules:
--------------------------------------------------------------------------------
1 | rules_version = '2';
2 | service cloud.firestore {
3 | match /databases/{database}/documents {
4 | match /{document=**} {
5 | allow read, write: if false;
6 | }
7 | match /results/{id} {
8 | allow read: if true;
9 | }
10 | match /showcase/{id} {
11 | allow read: if true;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/flow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lahirumaramba/flutter_scribble/13f96c24238453eb78d08c8f9ecf6dd207c017b9/flow.png
--------------------------------------------------------------------------------
/functions/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | env: {
4 | es6: true,
5 | node: true,
6 | },
7 | extends: [
8 | 'eslint:recommended',
9 | 'plugin:import/errors',
10 | 'plugin:import/warnings',
11 | 'plugin:import/typescript',
12 | 'google',
13 | 'plugin:@typescript-eslint/recommended',
14 | ],
15 | parser: '@typescript-eslint/parser',
16 | parserOptions: {
17 | project: ['tsconfig.json', 'tsconfig.dev.json'],
18 | sourceType: 'module',
19 | },
20 | ignorePatterns: [
21 | '/lib/**/*', // Ignore built files.
22 | ],
23 | plugins: ['@typescript-eslint', 'import'],
24 | rules: {
25 | quotes: ['error', 'single'],
26 | 'import/no-unresolved': 0,
27 | indent: ['error', 2],
28 | 'object-curly-spacing': ['error', 'always'],
29 | 'quote-props': ['error', 'as-needed'],
30 | 'require-jsdoc': [
31 | 'error',
32 | {
33 | require: {
34 | FunctionDeclaration: false,
35 | MethodDefinition: false,
36 | ClassDeclaration: false,
37 | ArrowFunctionExpression: false,
38 | FunctionExpression: false,
39 | },
40 | },
41 | ],
42 | },
43 | };
44 |
--------------------------------------------------------------------------------
/functions/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled JavaScript files
2 | lib/**/*.js
3 | lib/**/*.js.map
4 |
5 | # TypeScript v1 declaration files
6 | typings/
7 |
8 | # Node.js dependency directory
9 | node_modules/
10 |
--------------------------------------------------------------------------------
/functions/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "functions",
3 | "scripts": {
4 | "lint": "eslint --ext .js,.ts .",
5 | "build": "tsc",
6 | "build:watch": "tsc --watch",
7 | "serve": "npm run build && firebase emulators:start --only functions",
8 | "shell": "npm run build && firebase functions:shell",
9 | "start": "npm run shell",
10 | "deploy": "firebase deploy --only functions",
11 | "logs": "firebase functions:log"
12 | },
13 | "engines": {
14 | "node": "18"
15 | },
16 | "main": "lib/index.js",
17 | "dependencies": {
18 | "firebase-admin": "^11.5.0",
19 | "firebase-functions": "^4.2.0"
20 | },
21 | "devDependencies": {
22 | "@typescript-eslint/eslint-plugin": "^5.12.0",
23 | "@typescript-eslint/parser": "^5.12.0",
24 | "eslint": "^8.9.0",
25 | "eslint-config-google": "^0.14.0",
26 | "eslint-plugin-import": "^2.25.4",
27 | "firebase-functions-test": "^3.0.0",
28 | "typescript": "^4.9.0"
29 | },
30 | "private": true
31 | }
32 |
--------------------------------------------------------------------------------
/functions/src/index.ts:
--------------------------------------------------------------------------------
1 | import { onRequest } from 'firebase-functions/v2/https';
2 | import * as functions from 'firebase-functions';
3 | import { getStorage } from 'firebase-admin/storage';
4 | import { initializeApp } from 'firebase-admin/app';
5 | import { getFirestore, FieldValue } from 'firebase-admin/firestore';
6 | import { randomUUID } from 'crypto';
7 |
8 | initializeApp();
9 |
10 | exports.replicatehook = onRequest(
11 | { timeoutSeconds: 20, cors: true },
12 | async (req, res) => {
13 | if (req.method !== 'POST') {
14 | res.status(403).send('Forbidden!');
15 | return;
16 | }
17 |
18 | const prediction = req.body;
19 | if (
20 | prediction?.id === null ||
21 | typeof prediction?.id === 'undefined' ||
22 | prediction?.id === ''
23 | ) {
24 | res.status(400).send('Bad Request!');
25 | return;
26 | }
27 |
28 | try {
29 | await getFirestore()
30 | .collection('predictions')
31 | .doc(prediction.id)
32 | .update(prediction);
33 | } catch (err) {
34 | res.status(500).send(err);
35 | return;
36 | }
37 |
38 | const status = prediction.status;
39 | if (status in ['succeeded', 'failed'] === false) {
40 | if (status === 'succeeded') {
41 | const outputImageUrl = prediction?.output[1];
42 | console.log('outputImageUrl', outputImageUrl);
43 | try {
44 | await processOutputImage(outputImageUrl, prediction.id);
45 | } catch (err) {
46 | console.log(err);
47 | res.status(200).send('Ok');
48 | return;
49 | }
50 | }
51 | // if (status === 'failed') cleanup
52 | }
53 |
54 | res.status(200).send('Ok');
55 | }
56 | );
57 |
58 | async function processOutputImage(url: string, id: string): Promise {
59 | const response = await fetch(url);
60 | const imageData = Buffer.from(await response.arrayBuffer());
61 | const imageUrl = await uploadImageBuffer(imageData);
62 |
63 | await getFirestore()
64 | .collection('results')
65 | .doc(id)
66 | .update({ output: imageUrl });
67 | }
68 |
69 | exports.createPrediction = functions
70 | .runWith({
71 | secrets: ['REPLICATE_API_TOKEN', 'REPLICATE_WEB_HOOK'],
72 | timeoutSeconds: 20,
73 | })
74 | .https.onCall(async (data: any, context: any) => {
75 | const imageData = data.image;
76 | const prompt = data.prompt;
77 | const userId = data.userId;
78 |
79 | const apiKey = process.env.REPLICATE_API_TOKEN ?? '';
80 | const webhook = process.env.REPLICATE_WEB_HOOK ?? '';
81 |
82 | let prediction;
83 |
84 | try {
85 | const imageUrl = await uploadImage(imageData);
86 | prediction = await predict(prompt, imageUrl, apiKey, webhook);
87 |
88 | await getFirestore()
89 | .collection('predictions')
90 | .doc(prediction.id)
91 | .set(prediction);
92 | await getFirestore()
93 | .collection('results')
94 | .doc(prediction.id)
95 | .set({
96 | input: imageUrl,
97 | output: null,
98 | prompt,
99 | user: userId,
100 | timeStamp: FieldValue.serverTimestamp(),
101 | });
102 | } catch (error) {
103 | return { error };
104 | }
105 |
106 | return { data: prediction.id };
107 | });
108 |
109 | /**
110 | * Create a replicate prediction
111 | * @param {string} prompt Text prompt
112 | * @param {string} imageUrl Input image url
113 | * @param {string} apiKey Replicate apikey
114 | * @param {string} webhook Webhook for prediction updates
115 | * @return {Promise} Prediction JSON data
116 | */
117 | async function predict(
118 | prompt: string,
119 | imageUrl: string,
120 | apiKey: string,
121 | webhook: string
122 | ): Promise {
123 | const response = await fetch('https://api.replicate.com/v1/predictions', {
124 | method: 'POST',
125 | headers: {
126 | Authorization: `Token ${apiKey}`,
127 | 'Content-Type': 'application/json',
128 | },
129 | body: JSON.stringify({
130 | version:
131 | '435061a1b5a4c1e26740464bf786efdfa9cb3a3ac488595a2de23e143fdb0117',
132 | input: { prompt, image: imageUrl },
133 | webhook,
134 | webhook_events_filter: ['start', 'completed'],
135 | }),
136 | });
137 |
138 | if (response.status !== 201) {
139 | const error = await response.json();
140 | throw Error(`${error.detail}`);
141 | }
142 |
143 | return response.json();
144 | }
145 |
146 | /**
147 | * Upload 64bitEncoded Image to Storage
148 | * @param {string} imageBytes64Str Image data
149 | * @return {Promise} Image URL
150 | */
151 | async function uploadImage(imageBytes64Str: string): Promise {
152 | const imageBuffer = Buffer.from(imageBytes64Str, 'base64');
153 | const imageByteArray = new Uint8Array(imageBuffer);
154 | return await uploadImageBuffer(Buffer.from(imageByteArray));
155 | }
156 |
157 | async function uploadImageBuffer(buffer: Buffer): Promise {
158 | const bucket = getStorage().bucket();
159 | const fileName = randomUUID();
160 | const file = bucket.file(`images/${fileName}.png`);
161 | const options = { resumable: false, metadata: { contentType: 'image/png' } };
162 |
163 | // options may not be necessary
164 | try {
165 | await file.save(buffer, options);
166 | const urls = await file.getSignedUrl({
167 | action: 'read',
168 | expires: '03-09-2500',
169 | });
170 | const url = urls[0];
171 | return url;
172 | } catch (err) {
173 | throw Error(`Unable to upload image ${err}`);
174 | }
175 | }
176 |
--------------------------------------------------------------------------------
/functions/tsconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "include": [
3 | ".eslintrc.js"
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/functions/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "noImplicitReturns": true,
5 | "noUnusedLocals": true,
6 | "outDir": "lib",
7 | "sourceMap": true,
8 | "strict": true,
9 | "target": "es2017"
10 | },
11 | "compileOnSave": true,
12 | "include": [
13 | "src"
14 | ]
15 | }
16 |
--------------------------------------------------------------------------------
/lib/app_state.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | import 'package:cloud_firestore/cloud_firestore.dart';
4 |
5 | class AppState {
6 | AppState() {
7 | StreamController _localStreamController =
8 | StreamController.broadcast();
9 | }
10 |
11 | void _listenForEntries() {
12 | FirebaseFirestore.instance
13 | .collection('Entries')
14 | .snapshots()
15 | .listen((event) {
16 | final entries = event.docs.map((doc) {
17 | final data = doc.data();
18 | /*return Entry(
19 | date: data['date'] as String,
20 | text: data['text'] as String,
21 | title: data['title'] as String,
22 | );*/
23 | }).toList();
24 |
25 | //_entriesStreamController.add(entries);
26 | });
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/lib/blocs/app_cubit.dart:
--------------------------------------------------------------------------------
1 | import 'dart:async';
2 |
3 | import 'package:flutter_bloc/flutter_bloc.dart';
4 | import 'package:flutter_scribble/blocs/app_states.dart';
5 | import 'package:flutter_scribble/models/prediction_model.dart';
6 | import 'package:flutter_scribble/repository/prediction_repo.dart';
7 | import 'package:flutter_scribble/services/backend_service.dart';
8 | import 'package:get_it/get_it.dart';
9 |
10 | class AppCubit extends Cubit {
11 | final PredictionRepository _predcitionRepository = GetIt.instance();
12 | final BackendService _backendService = GetIt.instance();
13 | StreamSubscription? _predictionSubscription;
14 |
15 | AppCubit() : super(InitialState());
16 |
17 | void init() {
18 | emit(InitialState());
19 | _backendService.logEvent('screen_view', {'screen_name': 'create_page'});
20 | }
21 |
22 | Future listenToPrediction(String id) async {
23 | // Subscribe to listen for changes in the prediction state
24 | _predictionSubscription?.cancel();
25 | _predictionSubscription = _predcitionRepository
26 | .onPredictionUpdated(id)
27 | .listen(_predictionChanged);
28 | }
29 |
30 | Future createPrediction(String imageData, String prompt) async {
31 | final pid = await _backendService.createPrediction(imageData, prompt);
32 | pid == null ? emit(PredictionStopped()) : emit(PredictionStarted(pid));
33 | _backendService.logEvent('create_prediction', {'prompt': prompt});
34 | }
35 |
36 | void startPrediction(String prompt) {
37 | emit(PredictionTriggered(prompt));
38 | }
39 |
40 | void _predictionChanged(PredictionModel data) {
41 | emit(PredictionUpdated(data));
42 | if (data.outputImageUrl.isNotEmpty) {
43 | emit(PredictionCompleted());
44 | }
45 | }
46 |
47 | @override
48 | Future close() {
49 | _predictionSubscription?.cancel();
50 | return super.close();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/lib/blocs/app_events.dart:
--------------------------------------------------------------------------------
1 | import 'package:equatable/equatable.dart';
2 |
3 | abstract class PredictionEvent extends Equatable {
4 | const PredictionEvent();
5 |
6 | @override
7 | List