Skip to content

Commit

Permalink
fine tuning ml models with chat output
Browse files Browse the repository at this point in the history
  • Loading branch information
tharoosha committed Oct 28, 2023
1 parent 40ebab9 commit a49ed6c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 71 deletions.
52 changes: 5 additions & 47 deletions backend/controllers/mlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,49 +118,7 @@ export async function speech_to_text(req, res) {
}
}

// /** GET: http://localhost:5001/api/youtube_videos */
// /**
// * @param : {
// "mood" : "Happiness",
// }
// */
// // Define the controller function to execute the Python script
// export async function video_predict(req, res) {
// try {
// const { mood } = req.body;

// // Spawn the Python script as a child process
// const pythonProcess = spawn("/usr/src/app/venv/bin/python3", ["../ml_models/recommanded_system/combinedScript.py",mood,]);

// let output = "";

// // Listen for data events from the Python script's stdout
// pythonProcess.stdout.on("data", (data) => {
// output += data.toString();
// });

// // Listen for the 'close' event to handle the completion of the Python script
// pythonProcess.on("close", (code) => {
// if (code === 0) {
// try {
// const result = JSON.parse(output);
// res.status(200).json(result);
// } catch (error) {
// res.status(500).json({ error: "Failed to parse JSON response" });
// }
// } else {
// res.status(500).json({ error: "Python script exited with an error" });
// }
// });

// // Handle any errors during the execution of the Python script
// pythonProcess.on("error", (error) => {
// res.status(500).json({ error: error.message });
// });
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// }


/** POST: http://localhost:5001/api/emotion_analyzer */
/**
Expand All @@ -170,10 +128,10 @@ export async function speech_to_text(req, res) {
*/

export async function emotion_analyzer(req, res) {
const { message } = req.body;
const { tweet } = req.body;
try {
console.log(message)
const process = spawn("python3", ["../backend/ml_models/emotion_detection/emotionScript.py", message,]);
console.log(tweet)
const process = spawn("python3", ["../backend/ml_models/emotion_detection/emotionScript.py", tweet,]);

let emotion = ""
process.stdout.on("data", (data) => {
Expand All @@ -192,7 +150,7 @@ export async function emotion_analyzer(req, res) {
// // # Extract the "emotion" field
// const emotion = jsonData.emotion;
const jsonData = JSON.parse(emotion);
const joy = jsonData.emotion;
// const joy = jsonData.emotion;
// console.log(joy);
res.status(200).send(jsonData);
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion backend/ml_models/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
config.py
config.py
.env
25 changes: 24 additions & 1 deletion backend/ml_models/Chatbot/ChatbotKB.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@
"# ! pip3 install langchain==0.0.218\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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\n"
]
}
],
"source": [
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n",
"\n",
"api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"print(api_key)"
]
},
{
"cell_type": "code",
"execution_count": 14,
Expand Down Expand Up @@ -286,7 +309,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
},
"orig_nbformat": 4
},
Expand Down
8 changes: 3 additions & 5 deletions backend/ml_models/Chatbot/chatbotkb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
from llama_index import ServiceContext
import backoff

# from dotenv import load_dotenv
from dotenv import load_dotenv

# load_dotenv()
# load
load_dotenv()

# os.environ['OPENAI_API_KEY'] = "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"
openai.api_key = "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"
openai.api_key = os.getenv("OPENAI_API_KEY")

def create_Index(path):
max_input = 4096
Expand Down
30 changes: 26 additions & 4 deletions backend/ml_models/emotion_detection/emotionDetection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3133,6 +3133,30 @@
"OpenAI API"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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\n"
]
}
],
"source": [
"# Set up your OpenAI API credentials\n",
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n",
"\n",
"api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"print(api_key)"
]
},
{
"cell_type": "code",
"execution_count": 59,
Expand All @@ -3141,9 +3165,7 @@
"source": [
"import openai\n",
"\n",
"# Set up your OpenAI API credentials\n",
"# openai.api_key = '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'\n",
"openai.api_key = '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'\n",
"\n",
"\n",
"\n",
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
Expand Down Expand Up @@ -3439,7 +3461,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
},
"orig_nbformat": 4
},
Expand Down
13 changes: 10 additions & 3 deletions backend/ml_models/emotion_detection/emotionScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import backoff
import sys
import json
from dotenv import load_dotenv
import os

def get_tweet(data):
texts = [x['text'] for x in data]
Expand Down Expand Up @@ -144,7 +146,12 @@ def makePrediction(tweet, model, tokenizer):

# Set up your OpenAI API credentials
# openai.api_key = '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'
openai.api_key = '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'
# Set up your OpenAI API credentials


load_dotenv()

openai.api_key = os.getenv("OPENAI_API_KEY")

def get_completion(prompt, model="gpt-3.5-turbo"):
# def get_completion(prompt, model="text-davinci-002"):
Expand Down Expand Up @@ -230,8 +237,8 @@ def answer(tweet):
# # getInstantEmotion(['I am feeling sad'], model, tokenizer)

# # print(getInstantEmotion(['he has broufgt flowers to see me '], model, tokenizer))
# answer(input)
answer('he has brought flowers to see me')
answer(input)
# answer('he has brought flowers to see me')

# # Call the main function with the input
# response = getInstantEmotion(list(input), model, tokenizer)
Expand Down
4 changes: 3 additions & 1 deletion backend/ml_models/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ sentence_transformers
# gradio==3.14.0
SpeechRecognition==3.8.1
# mtranslate==1.8
# gTTS==2.3.0
# gTTS==2.3.0

python-dotenv
Binary file modified backend/ml_models/temp_audio.wav
Binary file not shown.
32 changes: 23 additions & 9 deletions frontend/src/pages/AI_Assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ const AI_Assistant = () => {
const audioFile = new File([audioBlob], 'userVoice.wav', { type: 'audio/wav' });
formData.append('audio', audioFile);

console.log(formData.get('audio'));
const audio = formData.get('audio');
console.log(audio);

// Send the audio data to the Node.js backend
axios
.post('http://localhost:5001/api/voice-input', formData)
.then((response) => {
console.log(response.data.result);
const updatedChatLogWithVoice = [...chatLog, { user: 'User', message: response.data.result }];
let data = response.data.result
data = String(data)
const updatedChatLogWithVoice = [...chatLog, { user: 'User', message: data }];
setChatLog(updatedChatLogWithVoice);
// setMessage(response.data.result);

Expand All @@ -82,13 +86,22 @@ const AI_Assistant = () => {
axios
.post('http://localhost:5001/api/analyze', { message: voice_message })
.then((response) => {
const updatedChatLogWithAI = [...chatLog, { user: 'AI_Consultant', message: response.data.result }];
const updatedChatLogWithAI = [...chatLog, { user: 'User', message: voice_message }, { user: 'AI_Consultant', message: response.data.result }];
setChatLog(updatedChatLogWithAI);
setResponse(response.data.result);
})
.catch((error) => console.error(error));

console.log('chatLog');
axios
.post('http://localhost:5001/api/emotion_analyze', { message: voice_message })
.then((response) => {
setEmotion(response.data.emotion);
// console.log(response.data.emotion)
})
.catch((error) => console.error(error));
// console.log(chatLog);
console.log(emotion);

})
.catch((error) => console.error(error));
};
Expand All @@ -114,21 +127,22 @@ const AI_Assistant = () => {
const updatedChatLogWithAI = [...updatedChatLog, { user: 'AI_Consultant', message: response.data.result }];
setChatLog(updatedChatLogWithAI);
setResponse(response.data.result);
// console.log(response.data.result)
})
.catch((error) => console.error(error));

console.log(message)

axios
.post('http://localhost:5001/api/emotion_analyze', { message: message })
.then((response) => {
setEmotion(response.data);
// console.log(response.data)
setEmotion(response.data.emotion);
// console.log(response.data.emotion)
})
.catch((error) => console.error(error));
// Clear the input field after submitting
setMessage('');
console.log(chatLog);
console.log(emotion);
// console.log(chatLog);
// console.log(emotion);
};

return (
Expand Down

0 comments on commit a49ed6c

Please sign in to comment.