Skip to content

Commit

Permalink
Merge pull request #5 from DA-NIN-JA/main
Browse files Browse the repository at this point in the history
Chatbot and Minor Fixes
  • Loading branch information
DhairyaCodes authored Dec 16, 2023
2 parents 1abaaac + 3f7a48a commit f1b673b
Show file tree
Hide file tree
Showing 8 changed files with 498 additions and 191 deletions.
7 changes: 6 additions & 1 deletion lib/components/hexagon.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:yogzen/global/color.dart';
import 'package:yogzen/openAI/open_AI.dart';


class HexagonClipper extends CustomClipper<Path> {
Expand Down Expand Up @@ -47,8 +48,12 @@ class HexagonWidget extends StatelessWidget {
),
height: 68, // Set the height of your hexagon here
child: IconButton(
// onPressed: () async {
// List<String> l= await fetchYogaPose("TadaAsan");
// print(l);
// },
onPressed: () {
// Navigator.pushNamed(context, Camera.routeName);

},
icon: const Icon(
Icons.camera,
Expand Down
12 changes: 6 additions & 6 deletions lib/components/nav_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ class _NavBarState extends State<NavBar> {
],
),
),
Positioned(
child: HexagonWidget(),
top: -30,
left: width / 2 - 30,
right: width / 2 - 30,
)
// Positioned(
// child: HexagonWidget(),
// top: -30,
// left: width / 2 - 30,
// right: width / 2 - 30,
// )
],
);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:yogzen/components/nav_bar_screen.dart';
import 'package:yogzen/global/color.dart';
import 'package:yogzen/providers/user_provider.dart';
import 'package:yogzen/screens/auth/auth_screen.dart';
import 'package:yogzen/screens/chatbot.dart/chatbot.dart';
import 'package:yogzen/screens/welcome/welcome_screen.dart';
import 'package:yogzen/services/auth_services.dart';

Expand Down Expand Up @@ -66,6 +67,7 @@ class _MyAppState extends State<MyApp> {
routes: {
AuthScreen.routeName: (context) => AuthScreen(),
NavScreen.routeName: (context) => NavScreen(),
ChatScreen.routeName: (context) => ChatScreen(),
// YogGuru.routeName: (context) => YogGuru(),
// Camera.routeName: (context) => Camera(),
// SpecificNeeds.routeName: (context) => SpecificNeeds(),
Expand Down
81 changes: 81 additions & 0 deletions lib/openAI/open_AI.dart
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');
}
}
124 changes: 124 additions & 0 deletions lib/screens/chatbot.dart/chatbot.dart
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,
),
),
],
),
),
);
}
}
40 changes: 40 additions & 0 deletions lib/screens/chatbot.dart/components/chatBubble.dart
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),
),
),
],
),
);
}
}
Loading

0 comments on commit f1b673b

Please sign in to comment.