-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from DA-NIN-JA/main
Chatbot and Minor Fixes
- Loading branch information
Showing
8 changed files
with
498 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import 'dart:convert'; | ||
import 'dart:math'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import '../global/baseurl.dart'; | ||
|
||
Random random = Random(); | ||
|
||
// List<Map<String, String>> l = []; | ||
|
||
final List<String> yogaPhrases = [ | ||
"I'm really passionate about yoga, so I'd love to stick to that topic if you don't mind.", | ||
"Yoga is a topic I'm excited about. Let's focus on that for now.", | ||
"I'm here to chat about yoga! Any yoga-related questions are welcome.", | ||
"Yoga is my main interest, so I'd prefer to keep our conversation centered around it.", | ||
"Let's make our discussion all about yoga. Feel free to ask anything related to it.", | ||
"Yoga is my expertise, and I'm happy to share my knowledge on that subject.", | ||
"For today, I'm dedicating our conversation to yoga. Ask me anything about it!", | ||
"I'm in a yoga mindset right now, so let's explore yoga-related topics together.", | ||
"I'm here to dive deep into yoga discussions. Other topics might not be my forte.", | ||
"I'm ready for a yoga-focused conversation. Bring on the yoga questions!", | ||
]; | ||
|
||
Future<String> fetchYogaPose(String question) async { | ||
|
||
final String apiUrl = 'https://api.openai.com/v1/chat/completions'; | ||
|
||
final Map<String, String> headers = { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer $apiKey', | ||
}; | ||
// final String question = "Tell me something that is good for mental and physical health."; | ||
final String prompt = 'Is the following topic related to yoga? Answer in yes or no and then generate the answer in 50 words only if the question is related to yoga.$question'; | ||
|
||
// l.add({"role": "user", "content": prompt}); | ||
|
||
final Map<String, dynamic> data = { | ||
'messages' : [{"role": "user", "content": prompt}], | ||
'model' : "gpt-3.5-turbo", | ||
'max_tokens': 256, | ||
'temperature' : 1 | ||
}; | ||
|
||
final response = await http.post( | ||
Uri.parse(apiUrl), | ||
headers: headers, | ||
body: json.encode(data), | ||
); | ||
// print(response.statusCode); | ||
// print(response.body); | ||
|
||
Map<String, dynamic> jsonResponse = json.decode(response.body); | ||
print(jsonResponse); | ||
|
||
String content = jsonResponse["choices"][0]["message"]["content"]; | ||
|
||
if(content.startsWith("Yes")){ | ||
content = content.substring(4); | ||
content = content.trimLeft(); | ||
if(content.isEmpty){ | ||
content = yogaPhrases[random.nextInt(10)]; | ||
} | ||
} | ||
else{ | ||
if(question.contains("Hi") || question.contains("hello") || question.contains("hi")|| question.contains("Hey")|| question.contains("Hello") || question.contains("hey")){ | ||
content = "Hi! " + yogaPhrases[random.nextInt(10)]; | ||
} | ||
else{ | ||
content = yogaPhrases[random.nextInt(10)]; | ||
} | ||
} | ||
|
||
print(content); | ||
// print(l); | ||
|
||
if (response.statusCode == 200) { | ||
return content; | ||
} else { | ||
throw Exception('Failed to load data'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:yogzen/global/color.dart'; | ||
import 'package:yogzen/openAI/open_AI.dart'; | ||
import 'package:yogzen/screens/chatbot.dart/components/chatBubble.dart'; | ||
|
||
class ChatScreen extends StatefulWidget { | ||
static const routeName = '/ChatScreen'; | ||
@override | ||
_ChatScreenState createState() => _ChatScreenState(); | ||
} | ||
|
||
class _ChatScreenState extends State<ChatScreen> { | ||
final TextEditingController _controller = TextEditingController(); | ||
List<ChatBubble> _chatBubbles = []; | ||
String curr = ""; | ||
|
||
void _handleSubmitted() { | ||
_controller.clear(); | ||
|
||
// User's message | ||
ChatBubble userBubble = ChatBubble( | ||
message: curr, | ||
isUser: true, | ||
); | ||
|
||
// Add user's message to the chat | ||
setState(() { | ||
_chatBubbles.add(userBubble); | ||
}); | ||
|
||
// Get response from the chatbot | ||
fetchYogaPose(curr).then((String response) { | ||
// Bot's response | ||
ChatBubble botBubble = ChatBubble( | ||
message: response, | ||
isUser: false, | ||
); | ||
|
||
// Add bot's response to the chat | ||
setState(() { | ||
_chatBubbles.add(botBubble); | ||
}); | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
backgroundColor: klightBlue, | ||
appBar: AppBar( | ||
title: Text('Aditya Nath Yogi'), | ||
backgroundColor: klightBlue, | ||
scrolledUnderElevation: 0, | ||
), | ||
body: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Column( | ||
children: <Widget>[ | ||
Expanded( | ||
child: ListView.builder( | ||
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16), | ||
reverse: true, | ||
itemCount: _chatBubbles.length, | ||
itemBuilder: (_, int index) => | ||
_chatBubbles[_chatBubbles.length - 1 - index], | ||
), | ||
), | ||
Container( | ||
margin: EdgeInsets.symmetric(horizontal: 8.0), | ||
padding: const EdgeInsets.symmetric( | ||
horizontal: 8, vertical: 16), | ||
child: TextField( | ||
controller: _controller, | ||
onSubmitted: (_) => _handleSubmitted, | ||
onChanged: (value) => setState(() { | ||
curr = value; | ||
}), | ||
decoration: InputDecoration( | ||
enabledBorder: OutlineInputBorder( | ||
borderRadius: BorderRadius.circular(16), | ||
borderSide: BorderSide( | ||
color: kdarkBlue, | ||
width: 1, | ||
), | ||
), | ||
focusedBorder: OutlineInputBorder( | ||
borderRadius: BorderRadius.circular(16), | ||
borderSide: BorderSide( | ||
color: kdarkBlue, | ||
width: 1, | ||
), | ||
), | ||
border: OutlineInputBorder( | ||
borderRadius: BorderRadius.circular(16), | ||
borderSide: BorderSide( | ||
color: kdarkBlue, | ||
width: 1, | ||
), | ||
), | ||
contentPadding: EdgeInsets.all(8), | ||
hintText: 'Ask something about Yoga.', | ||
hintStyle: TextStyle(color: kdarkBlue), | ||
suffixIcon: IconButton( | ||
onPressed: _handleSubmitted, | ||
icon: Icon( | ||
Icons.send, | ||
size: 28, | ||
), | ||
), | ||
suffixIconColor: _controller.text == "" | ||
? kdarkBlueMuted.withOpacity(0.5) | ||
: kdarkBlue, | ||
), | ||
// style: TextStyle(overflow: TextOverflow.visible), | ||
maxLines: 1, | ||
// expands: true, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:yogzen/global/color.dart'; | ||
|
||
class ChatBubble extends StatelessWidget { | ||
final String message; | ||
final bool isUser; | ||
|
||
const ChatBubble({required this.message, required this.isUser}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final width = MediaQuery.of(context).size.width; | ||
return Container( | ||
margin: const EdgeInsets.symmetric(vertical: 10.0), | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
mainAxisAlignment: | ||
isUser ? MainAxisAlignment.end : MainAxisAlignment.start, | ||
children: [ | ||
Container( | ||
// width: width / 2 + 64, | ||
constraints: BoxConstraints(maxWidth: width / 2 + 64), | ||
// height: , | ||
padding: const EdgeInsets.all(10.0), | ||
decoration: BoxDecoration( | ||
color: | ||
!isUser ? Colors.white : kdarkBlue, | ||
borderRadius: BorderRadius.circular(16), | ||
border: isUser ? Border() : Border.all(color: kdarkBlue, width: 2) | ||
), | ||
child: Text( | ||
message,textAlign: TextAlign.left, | ||
style: TextStyle(color : !isUser ? kdarkBlue : Colors.white, fontSize: 15), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.