-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
125 lines (111 loc) · 2.89 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, { useEffect, useState, useRef } from 'react';
import type {PropsWithChildren} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
Button,
Alert,
Image
} from 'react-native';
import { LCMStableDiffusionPipeline } from '@venetanji/diffusers.js'
import { Skia, AlphaType, ColorType } from "@shopify/react-native-skia";
const App = () => {
const [pipe, setPipe] = useState(null);
const createSession = async () => {
// download model if the file does not exist
if (pipe === null) {
const apipe = await LCMStableDiffusionPipeline.fromPretrained('venetanji/ds8lcm')
setPipe(apipe)
} else {
console.log('Session already created')
}
};
const [imageUri, setImageUri] = useState({uri: 'https://reactnative.dev/img/tiny_logo.png'});
const generate = async () => {
if (pipe === null) {
console.log('Session not created')
return
}
//await createSession()
console.log("Generating image...")
const square = 384
const {width, height} = {width: square, height: square}
const progressCallback = (payload:any) => {
console.log(payload)
}
const images = await pipe.run({
prompt: "A photograph of an astronaut riding a horse on mars, highly detailed",
negativePrompt: "low res, bad quality",
width: width,
height: height,
numInferenceSteps: 5,
guidanceScale: 1.4,
sdV1: true,
//beta_schedule: "scaled_linear",
progressCallback: progressCallback
})
const pixels = new Uint8Array(images[0].data);
const imagedata = Skia.Data.fromBytes(pixels)
// console.log("Creating skia image")
const img = Skia.Image.MakeImage(
{
width: width,
height: height,
colorType: ColorType.RGBA_8888,
alphaType: AlphaType.Premul,
},
imagedata,
width * 4
);
if (!img) {
console.error('Failed to create image');
return;
}
setImageUri({uri: `data:image/png;base64,${img?.encodeToBase64()}`})
console.log('Image encoded and sent to Image component')
}
const GeneratedImage = () => {
useEffect(() => {
createSession()
return () => {
console.log('cleanup')
if (pipe !== null) {
pipe.release()
setPipe(null)
}
}
}, []);
return (
<Image
style={{width: 400, height: 400}}
source={imageUri}
/>
);
};
return (
<View style={styles.container}>
<Text>React native diffusion</Text>
<GeneratedImage></GeneratedImage>
<Button
title="Generate"
onPress={() => {
generate();
}}
></Button>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'space-around',
},
btn: { width: '90%' },
});
export default App;