Skip to content

Commit

Permalink
feat: minor chatbot fix
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
cmaloney111 authored Jun 17, 2024
2 parents 92a7440 + a31b388 commit fa48f68
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 130 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ jobs:
run: pip install -r api/requirements.txt pyinstaller==6.6.0

- name: Build flask exe
run: pyinstaller --name app --onefile --console api/app.py --hidden-import=tiktoken_ext.openai_public --hidden-import=tiktoken_ext

run: pyinstaller api/app.spec
- name: Install dependencies
run: npm install

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
run: pip install -r api/requirements.txt pyinstaller==6.6.0

- name: Build flask exe
run: pyinstaller --name app --onefile --console api/app.py --hidden-import=tiktoken_ext.openai_public --hidden-import=tiktoken_ext
run: pyinstaller app.spec

- name: Install dependencies
run: npm install
Expand Down
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
# LinguifAI

Projeto NLP 2 - 2023.2

## Tecnologias utilizadas:

O projeto utiliza das seguintes tecnologias e ferramentas:

### Front-End
* React
* React-Router
* Axios
* ElectronJs
* Tailwind

- React
- React-Router
- Axios
- ElectronJs
- Tailwind

### Back-End
* Python
* Flask

- Python
- Flask

## Como rodar a aplicação:

Primeiramente, instale as depencencias:
Primeiramente, instale as dependências:

Dependências do back-end:

```bash
pip install -r api/requirements.txt
```

Dependências do front-end:

```bash
yarn
Expand Down Expand Up @@ -48,6 +60,6 @@ npm run electron
- [x] Abrir CSV
- [x] Exibir Preview de N linhas do CSV
- [x] Escolher colunas de entrada
- [X] Selecionar classificador e enviar dados
- [x] Selecionar classificador e enviar dados
- [x] Exibir resultado do classificador
- [x] Exportar resultado em csv?
- [x] Exportar resultado em csv?
2 changes: 1 addition & 1 deletion api/Neural_Network2.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def create_and_train_rnn_model(df, name, epochs = 10, batch_size = 32, learning_

if len(valid_losses) > 2 and all(valid_loss >= loss for loss in valid_losses[-3:]):
print('Stopping early due to lack of improvement in validation loss.')
break
# break

train_losses.append(train_loss)
valid_losses.append(valid_loss)
Expand Down
78 changes: 22 additions & 56 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ def chat():
chat_history = data.get('history', [])
api_key = data.get('apikey')


if df is not None:

print(df.head(1))
documents = split_dataframe_into_documents(df)

embeddings = OpenAIEmbeddings(api_key=api_key)
Expand All @@ -74,27 +77,24 @@ def chat():

return jsonify(reply=bot_reply)
else:
print("No df")
return jsonify(reply="No data available."), 400

# messages = [{"role": "system", "content": "You are a helpful assistant."}]
# for msg in chat_history:
# messages.append({"role": "user" if msg['origin'] == 'user' else "assistant", "content": msg['text']})
# messages.append({"role": "user", "content": user_message})

# try:
# client = openai.OpenAI(api_key = api_key)
# response = client.chat.completions.create(
# model="gpt-3.5-turbo",
# messages=messages,
# max_tokens=200
# )
# bot_reply = response.choices[0].message.content.strip()

# return jsonify(reply=bot_reply)
# except Exception as e:
# print(f"Error: {e}")
# return jsonify(reply="Desculpe, ocorreu um erro ao processar sua mensagem."), 500
messages = [{"role": "system", "content": "You are a helpful assistant."}]
for msg in chat_history:
messages.append({"role": "user" if msg['origin'] == 'user' else "assistant", "content": msg['text']})
messages.append({"role": "user", "content": user_message})

try:
client = openai.OpenAI(api_key = api_key)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=200
)
bot_reply = response.choices[0].message.content.strip()

return jsonify(reply=bot_reply)
except Exception as e:
print(f"Error: {e}")
return jsonify(reply="Desculpe, ocorreu um erro ao processar sua mensagem."), 500


def shutdown_server():
Expand All @@ -116,7 +116,7 @@ def receive_file():
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
df = pd.read_csv(file)
df = pd.read_csv(file, on_bad_lines='skip')
return jsonify({'message': 'File uploaded successfully'}), 200


Expand Down Expand Up @@ -282,40 +282,6 @@ def apikey():
return jsonify(reply="Desculpe, ocorreu um erro ao processar sua mensagem."), 500


# @app.route('/chat', methods=['POST'])
# def chat():
# global df
# if df is not None:
# # run rag
# print(df.head(1))
# else:
# print("No df")
# data = request.get_json()
# user_message = data.get('message')
# chat_history = data.get('history', [])
# api_key = data.get('apikey')

# messages = [{"role": "system", "content": "You are a helpful assistant."}]
# for msg in chat_history:
# messages.append({"role": "user" if msg['origin'] == 'user' else "assistant", "content": msg['text']})
# messages.append({"role": "user", "content": user_message})

# try:
# client = openai.OpenAI(api_key = api_key)
# response = client.chat.completions.create(
# model="gpt-3.5-turbo", # ou a gente poderia ver com gpt 4 mas por enquanto coloquei 3.5
# messages=messages,
# max_tokens=200
# )
# bot_reply = response.choices[0].message.content.strip()

# return jsonify(reply=bot_reply)
# except Exception as e:
# print(f"Error: {e}")
# return jsonify(reply="Desculpe, ocorreu um erro ao processar sua mensagem."), 500



if __name__ == '__main__':
training_progress = {
'training_progress': 0,
Expand Down
5 changes: 3 additions & 2 deletions api/app.spec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# -*- mode: python ; coding: utf-8 -*-

entry_point = 'app.py'
Expand All @@ -9,7 +10,7 @@ a = Analysis(
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hiddenimports=['tiktoken_ext.openai_public', 'tiktoken_ext'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
Expand Down Expand Up @@ -48,4 +49,4 @@ coll = COLLECT(
upx=True,
upx_exclude=[],
name='app',
)
)
33 changes: 28 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Cameron",
"email": "[email protected]"
},
"version": "0.2.5",
"version": "0.2.8",
"main": "./public/electron.js",
"homepage": "./",
"private": true,
Expand All @@ -32,6 +32,8 @@
"react-papaparse": "^4.1.0",
"react-router-dom": "^6.15.0",
"react-scripts": "5.0.0",
"react-spinners": "^0.13.8",
"react-toastify": "^10.0.5",
"react-transition-group": "^4.4.5",
"serve": "^14.2.1",
"typescript": "^4.9.5"
Expand Down
11 changes: 6 additions & 5 deletions src/components/chatbot/chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ const ChatBot: React.FC = () => {
const errorMessage: Message = { text: "Desculpe, ocorreu um erro. Tente novamente.", origin: 'bot' };
setChatHistory(prevHistory => [...prevHistory, errorMessage]);
}

console.log(message)
setMessage("");
console.log(message)
};

const sendAPIKeyMessage = () => {
setChatHistory(prevHistory => [
...prevHistory,
{ text: "Olá! Eu sou o (LinguiTalk ou LinguaBot). Coloca a sua chave:", origin: 'bot' }
{ text: "Olá! Eu sou o LinguiTalk, um chatbot para lhe auxiliar na exploração dos seus dados! Primeiro, insira uma chave API válida do ChatGPT:", origin: 'bot' }
]);
};

const sendInitialMessage = () => {
setChatHistory(prevHistory => [
...prevHistory,
{ text: "Olá! Eu sou o (LinguiTalk ou LinguaBot). Como posso ajudar?", origin: 'bot' }
{ text: "Olá! Eu sou o LinguiTalk. Como posso ajudar?", origin: 'bot' }
]);
};

Expand Down Expand Up @@ -98,7 +99,7 @@ const ChatBot: React.FC = () => {
{isOpen && (
<div className="fixed bottom-20 right-4 bg-white border border-gray-300 shadow-lg rounded-lg w-80 flex flex-col max-h-[600px]">
<div className="bg-blue-500 text-white p-4 flex justify-between items-center">
<h2 className="text-lg">LinguiTalk ou LinguaBot</h2>
<h2 className="text-lg">LinguiTalk</h2>
<button
className="text-white hover:text-gray-200"
onClick={toggleChat}
Expand Down Expand Up @@ -130,7 +131,7 @@ const ChatBot: React.FC = () => {
value={message}
onChange={(e) => setMessage(e.target.value)}
onKeyPress={(e) => {
if (e.key === 'Enter') sendMessage();
if (e.key === 'Enter') sendMessage()
}}
/>
<button
Expand Down
Loading

0 comments on commit fa48f68

Please sign in to comment.