Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# 1. Imports and API setup
|
| 2 |
from groq import Groq
|
| 3 |
import base64
|
| 4 |
import streamlit as st
|
|
@@ -6,22 +5,24 @@ import pandas as pd
|
|
| 6 |
import json
|
| 7 |
from datetime import datetime
|
| 8 |
from pathlib import Path
|
| 9 |
-
|
| 10 |
import os
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
client = Groq(
|
| 13 |
-
api_key=os.getenv("GROQ_API")
|
| 14 |
)
|
| 15 |
|
| 16 |
llava_model = 'llava-v1.5-7b-4096-preview'
|
| 17 |
llama31_model = 'llama-3.1-70b-versatile'
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
def encode_image(image_path):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
# 3.
|
| 25 |
def image_to_text(client, model, base64_image, prompt):
|
| 26 |
chat_completion = client.chat.completions.create(
|
| 27 |
messages=[
|
|
@@ -40,10 +41,9 @@ def image_to_text(client, model, base64_image, prompt):
|
|
| 40 |
],
|
| 41 |
model=model
|
| 42 |
)
|
| 43 |
-
|
| 44 |
return chat_completion.choices[0].message.content
|
| 45 |
|
| 46 |
-
# 4.
|
| 47 |
def game_generation(client, image_description, age, support_level, stimulus_type):
|
| 48 |
chat_completion = client.chat.completions.create(
|
| 49 |
messages=[
|
|
@@ -58,84 +58,50 @@ def game_generation(client, image_description, age, support_level, stimulus_type
|
|
| 58 |
],
|
| 59 |
model=llama31_model
|
| 60 |
)
|
| 61 |
-
|
| 62 |
return chat_completion.choices[0].message.content
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
JSON_DATASET_PATH = Path("dados.json")
|
| 66 |
-
|
| 67 |
-
def save_json(idade: str, nivel_suporte: str, tipo_estimulo: str) -> None:
|
| 68 |
-
with JSON_DATASET_PATH.open("a") as f:
|
| 69 |
-
# Dump dos dados em formato JSON, seguido de uma nova linha
|
| 70 |
-
json.dump({
|
| 71 |
-
"Idade": idade,
|
| 72 |
-
"NivelDeSuporte": nivel_suporte,
|
| 73 |
-
"TipoDeEstimulo": tipo_estimulo,
|
| 74 |
-
"datetime": datetime.now().isoformat()
|
| 75 |
-
}, f)
|
| 76 |
-
f.write("\n") # Para garantir que cada registro fique em uma linha nova)
|
| 77 |
-
st.write(tipo_estimulo)
|
| 78 |
-
|
| 79 |
-
# 5. Streamlit app
|
| 80 |
def main():
|
| 81 |
st.image("autism.png", width=200)
|
| 82 |
-
st.title("PlayBot - Gerador de Brincadeiras",
|
| 83 |
-
st.write("Conheça o PlayBot, um assistente inteligente que gera brincadeiras divertidas e educativas para crianças com TEA
|
| 84 |
|
|
|
|
| 85 |
idade = st.number_input("Idade da criança:", min_value=1, max_value=16, step=1)
|
| 86 |
nivel_suporte = st.selectbox("Nível de Suporte", ["Nível 1", "Nível 2", "Nível 3"])
|
| 87 |
tipo_estimulo = st.selectbox("Tipo de Estimulo", ["Sensorial", "Coordenação motora", "Raciocinio", "Fala"])
|
| 88 |
|
|
|
|
| 89 |
uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
|
| 90 |
if uploaded_file is not None:
|
| 91 |
-
#
|
| 92 |
bytes_data = uploaded_file.read()
|
| 93 |
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 94 |
-
prompt =
|
| 95 |
-
|
| 96 |
-
|
| 97 |
image_description = image_to_text(client, llava_model, base64_image, prompt)
|
| 98 |
|
| 99 |
-
#st.write("\n--- Image Description ---")
|
| 100 |
-
#st.write(image_description)
|
| 101 |
-
|
| 102 |
st.write("\n--- Brincadeira ---")
|
| 103 |
game_description = game_generation(client, image_description, idade, nivel_suporte, tipo_estimulo)
|
| 104 |
st.write(game_description)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
data = {
|
| 108 |
"Idade": idade,
|
| 109 |
"Nível de Suporte": nivel_suporte,
|
| 110 |
-
"Tipo de Estimulo": tipo_estimulo
|
| 111 |
}
|
| 112 |
df = pd.DataFrame([data])
|
| 113 |
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
import pandas as pd
|
| 117 |
-
from sqlalchemy import create_engine
|
| 118 |
-
|
| 119 |
-
# Dicionário com os dados
|
| 120 |
-
data = {
|
| 121 |
-
"Idade": idade,
|
| 122 |
-
"Nível de Suporte": nivel_suporte,
|
| 123 |
-
"Tipo de Estimulo": tipo_estimulo
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
# Criar um DataFrame a partir dos dados
|
| 127 |
-
df = pd.DataFrame([data])
|
| 128 |
-
|
| 129 |
-
# Criar uma conexão com o banco de dados SQLite (ou criar o arquivo se não existir)
|
| 130 |
-
engine = create_engine('sqlite:///dados_usuarios.db') # Isso cria o banco de dados dados_usuarios.db
|
| 131 |
-
|
| 132 |
-
# Escrever os dados no banco de dados, criando uma tabela 'usuarios'
|
| 133 |
df.to_sql('usuarios', con=engine, if_exists='append', index=False)
|
| 134 |
-
|
| 135 |
-
print("Dados registrados no banco de dados com sucesso.")
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
main()
|
|
|
|
|
|
|
| 1 |
from groq import Groq
|
| 2 |
import base64
|
| 3 |
import streamlit as st
|
|
|
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
from pathlib import Path
|
|
|
|
| 8 |
import os
|
| 9 |
+
import sqlite3
|
| 10 |
+
from sqlalchemy import create_engine
|
| 11 |
|
| 12 |
+
# Inicialização do cliente Groq
|
| 13 |
client = Groq(
|
| 14 |
+
api_key=os.getenv("GROQ_API"),
|
| 15 |
)
|
| 16 |
|
| 17 |
llava_model = 'llava-v1.5-7b-4096-preview'
|
| 18 |
llama31_model = 'llama-3.1-70b-versatile'
|
| 19 |
|
| 20 |
+
# 2. Codificação de Imagem
|
| 21 |
def encode_image(image_path):
|
| 22 |
+
with open(image_path, "rb") as image_file:
|
| 23 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 24 |
|
| 25 |
+
# 3. Função para converter imagem em texto
|
| 26 |
def image_to_text(client, model, base64_image, prompt):
|
| 27 |
chat_completion = client.chat.completions.create(
|
| 28 |
messages=[
|
|
|
|
| 41 |
],
|
| 42 |
model=model
|
| 43 |
)
|
|
|
|
| 44 |
return chat_completion.choices[0].message.content
|
| 45 |
|
| 46 |
+
# 4. Função para gerar uma brincadeira
|
| 47 |
def game_generation(client, image_description, age, support_level, stimulus_type):
|
| 48 |
chat_completion = client.chat.completions.create(
|
| 49 |
messages=[
|
|
|
|
| 58 |
],
|
| 59 |
model=llama31_model
|
| 60 |
)
|
|
|
|
| 61 |
return chat_completion.choices[0].message.content
|
| 62 |
|
| 63 |
+
# Função principal do app Streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def main():
|
| 65 |
st.image("autism.png", width=200)
|
| 66 |
+
st.title("PlayBot - Gerador de Brincadeiras", anchor="center")
|
| 67 |
+
st.write("Conheça o PlayBot, um assistente inteligente que gera brincadeiras divertidas e educativas para crianças com TEA...")
|
| 68 |
|
| 69 |
+
# Entrada de dados do usuário
|
| 70 |
idade = st.number_input("Idade da criança:", min_value=1, max_value=16, step=1)
|
| 71 |
nivel_suporte = st.selectbox("Nível de Suporte", ["Nível 1", "Nível 2", "Nível 3"])
|
| 72 |
tipo_estimulo = st.selectbox("Tipo de Estimulo", ["Sensorial", "Coordenação motora", "Raciocinio", "Fala"])
|
| 73 |
|
| 74 |
+
# Carregamento de imagem
|
| 75 |
uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
|
| 76 |
if uploaded_file is not None:
|
| 77 |
+
# Leitura do arquivo
|
| 78 |
bytes_data = uploaded_file.read()
|
| 79 |
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 80 |
+
prompt = "Describe this image in detail, including the appearance of the object(s)."
|
| 81 |
+
|
| 82 |
+
# Chamada para converter imagem em descrição de texto
|
| 83 |
image_description = image_to_text(client, llava_model, base64_image, prompt)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
st.write("\n--- Brincadeira ---")
|
| 86 |
game_description = game_generation(client, image_description, idade, nivel_suporte, tipo_estimulo)
|
| 87 |
st.write(game_description)
|
| 88 |
|
| 89 |
+
# Salvando dados no banco de dados
|
| 90 |
data = {
|
| 91 |
"Idade": idade,
|
| 92 |
"Nível de Suporte": nivel_suporte,
|
| 93 |
+
"Tipo de Estimulo": tipo_estimulo
|
| 94 |
}
|
| 95 |
df = pd.DataFrame([data])
|
| 96 |
|
| 97 |
+
# Conexão com o banco de dados SQLite
|
| 98 |
+
engine = create_engine('sqlite:///dados_usuarios.db')
|
| 99 |
|
| 100 |
+
# Inserindo os dados na tabela 'usuarios'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
df.to_sql('usuarios', con=engine, if_exists='append', index=False)
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
st.success("Dados registrados no banco de dados com sucesso.")
|
| 104 |
+
|
| 105 |
+
# Executar o app
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
main()
|