-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
196 lines (173 loc) · 4.86 KB
/
App.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { StatusBar } from 'expo-status-bar';
import { useState } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
} from 'react-native';
import { MaterialIcons } from '@expo/vector-icons';
import { GiftedChat } from 'react-native-gifted-chat';
// Importing Keys
import {
OPENAI_CHAT,
OPENAI_IMAGE,
PAWAN_CHAT,
PAWAN_IMAGE,
} from './keys.json';
export default function App() {
const OPENAI_key = OPENAI_CHAT.api_key;
const pawanOpenAI_key = PAWAN_CHAT.api_key;
// Chat GPT
const OPENAI_url = OPENAI_CHAT.api_url;
const pawanOpenAI_url = PAWAN_CHAT.api_url;
// Dall E
const OPENAI_DALLE_URL = OPENAI_IMAGE.api_url;
const pawanOpenAI_DALLE_URL = PAWAN_IMAGE.api_url;
const [inputMessage, setInputMessage] = useState('');
const [outputMessage, setOutputMessage] = useState(
'Results to be shown here!'
);
const [messages, setMessages] = useState([]);
const handleButtonClick = () => {
console.log(inputMessage);
const message = {
_id: Math.random().toString(36).substring(7),
text: inputMessage,
createdAt: new Date(),
user: { _id: 1, name: 'Sam Jain' },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
fetch(pawanOpenAI_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${pawanOpenAI_key}`,
},
body: JSON.stringify({
messages: [{ role: 'user', content: inputMessage }],
model: 'gpt-3.5-turbo',
}),
})
.then((response) => response.json())
.then((data) => {
// console.log(data.choices[0].message.content);
setOutputMessage(data.choices[0].message.content.trim());
const message = {
_id: Math.random().toString(36).substring(7),
text: data.choices[0].message.content.trim(),
createdAt: new Date(),
user: { _id: 2, name: 'Open AI' },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
});
};
const generateImages = () => {
console.log(inputMessage);
const message = {
_id: Math.random().toString(36).substring(7),
text: inputMessage,
createdAt: new Date(),
user: { _id: 1, name: 'Sam Jain' },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
fetch(pawanOpenAI_DALLE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${pawanOpenAI_key}`,
},
body: JSON.stringify({
prompt: inputMessage,
n: 1,
size: '1024x1024',
}),
})
.then((response) => response.json())
.then((data) => {
// console.log(data.data[0].url);
setOutputMessage(data.data[0].url);
const message = {
_id: Math.random().toString(36).substring(7),
text: "Image",
image: data.data[0].url,
createdAt: new Date(),
user: { _id: 2, name: 'Open AI' },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
});
};
return (
<View style={{ flex: 1 }}>
<View style={{ flex: 1, justifyContent: 'center' }}>
<GiftedChat
messages={messages}
renderInputToolbar={() => {}}
user={{ _id: 1 }}
minInputToolbarHeight={0}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<View
style={{
flex: 1,
marginLeft: 10,
marginBottom: 20,
backgroundColor: 'white',
borderRadius: 10,
borderColor: 'grey',
borderWidth: 1,
height: 60,
marginLeft: 10,
marginRight: 10,
justifyContent: 'center',
paddingLeft: 14,
paddingRight: 14,
}}>
<TextInput
placeholder="Enter your Question"
onChangeText={setInputMessage}
/>
</View>
<TouchableOpacity onPress={generateImages}>
<View
style={{
backgroundColor: 'green',
padding: 5,
marginRight: 10,
marginBottom: 20,
borderRadius: 9999,
width: 60,
height: 60,
justifyContent: 'center',
}}>
<MaterialIcons
name="send"
size={30}
color={'white'}
style={{ marginLeft: 10 }}
/>
</View>
</TouchableOpacity>
</View>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});