-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
204 lines (185 loc) · 5.56 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
197
198
199
200
201
import { StatusBar } from "expo-status-bar";
import { useState } from "react";
import React from "react"; // Add this import for React
import {
ImageBackground,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import { MaterialIcons } from "@expo/vector-icons";
import { GiftedChat } from "react-native-gifted-chat";
import * as Speech from 'expo-speech'
export default function App() {
const [messages, setMessages] = useState([]);
const [inputMessage, setInputMessage] = useState("");
const [outputMessage, setOutputMessage] = useState("Results to be shown here");
const generateText = () => {
console.log(inputMessage);
const message = {
_id: Math.random().toString(36).substring(7),
text: inputMessage,
createdAt: new Date(),
user: { _id: 1, name: "1" },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization":
"Bearer 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", // Replace with your OpenAI API key
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: inputMessage,
},
],
}),
})
.then((response) => response.json())
.then((data) => {
console.log(data.choices[0].message.content);
setInputMessage("")
setOutputMessage(data.choices[0].message.content.trim());
const message = {
_id: Math.random().toString(36).substring(7),
text: data.choices[0].message.content,
createdAt: new Date(),
user: { _id: 2, name: "Uma" },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
)
options = {};
Speech.speak(data.choices[0].message.content, options)
})
.catch((error) => {
console.error("Error fetching text:", error);
});
};
const generateImages = () => {
console.log(inputMessage);
const message = {
_id: Math.random().toString(36).substring(7),
text: inputMessage,
createdAt: new Date(),
user: { _id: 1, name: "1" },
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
fetch("https://api.openai.com/v1/images/generations", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization":
"Bearer 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",
},
body: JSON.stringify({
prompt: inputMessage,
n: 1,
size: "1024x1024",
}),
})
.then((response) => response.json())
.then((data) => {
console.log(data.data[0].url);
setInputMessage("");
setOutputMessage(data.data[0].url);
const message = {
_id: Math.random().toString(36).substring(7),
text: "Image Generated✅",
createdAt: new Date(),
user: { _id: 2, name: "Uma" },
image: data.data[0].url,
};
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, [message])
);
Speech.VoiceQuality.Enhanced = "Enhanced"
Speech.speak("Image Generated")
})
.catch((error) => {
console.error("Error fetching image:", error);
});
};
const handleButtonClick = () => {
console.log(inputMessage);
if (inputMessage.toLowerCase().startsWith("generate image")) {
generateImages();
} else {
generateText();
}
};
const handleTextInput = (text) => {
setInputMessage(text);
};
return (
<ImageBackground source={require('./assets/bg.png')} resizeMode="cover"
style={{ flex: 1, width: "100%", height: "100%" }} >
<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: 10,
borderRadius: 10,
borderColor: "grey",
borderWidth: 1,
height: 60,
marginLeft: 10,
marginRight: 10,
justifyContent: "center",
paddingLeft: 10,
paddingRight: 10,
}}
>
<TextInput
placeholder="ask a Question or type generate image... "
onChangeText={handleTextInput}
value={inputMessage}
/>
</View>
<TouchableOpacity onPress={handleButtonClick}>
<View
style={{
backgroundColor: "green",
padding: 5,
marginRight: 10,
marginBottom: 10,
borderRadius: 9999,
width: 60,
height: 60,
justifyContent: "center",
}}
>
<MaterialIcons
name="send"
size={35}
color="white"
style={{ marginLeft: 10 }}
/>
</View>
</TouchableOpacity>
</View>
<StatusBar style="auto" />
</View>
</ImageBackground>
);
}