FabioSantos commited on
Commit
568c189
·
verified ·
1 Parent(s): ccb58bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -3,28 +3,27 @@ import streamlit as st
3
  import pandas as pd
4
  import json
5
  from datetime import datetime
 
6
  import firebase_admin
7
  from firebase_admin import credentials, firestore
8
- import os
9
 
10
- from groq import Groq
 
 
 
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
- # 1. Inicializar o Firebase Admin SDK
21
- cred = credentials.Certificate("playbot-f0897-firebase-adminsdk-uay8b-0c87a1f1d0.json") # Substitua pelo caminho correto
22
- firebase_admin.initialize_app(cred)
23
-
24
- # Conectar ao Firestore
25
- db = firestore.client()
26
 
27
- # 2. Função para converter imagem em texto
28
  def image_to_text(client, model, base64_image, prompt):
29
  chat_completion = client.chat.completions.create(
30
  messages=[
@@ -45,13 +44,13 @@ def image_to_text(client, model, base64_image, prompt):
45
  )
46
  return chat_completion.choices[0].message.content
47
 
48
- # 3. Função para gerar uma brincadeira
49
  def game_generation(client, image_description, age, support_level, stimulus_type):
50
  chat_completion = client.chat.completions.create(
51
  messages=[
52
  {
53
  "role": "system",
54
- "content": f"Suggest a safe game for a child of {age} years old, with support level {support_level} and focus on {stimulus_type}. The game should use the object described in the image and should not use objects that are dangerous for children...",
55
  },
56
  {
57
  "role": "user",
@@ -62,21 +61,16 @@ def game_generation(client, image_description, age, support_level, stimulus_type
62
  )
63
  return chat_completion.choices[0].message.content
64
 
65
- # 4. Função para salvar os dados no Firestore
66
- def save_to_firestore(idade, nivel_suporte, tipo_estimulo):
67
- doc_ref = db.collection('usuarios').add({
68
- "Idade": idade,
69
- "Nível de Suporte": nivel_suporte,
70
- "Tipo de Estimulo": tipo_estimulo,
71
- "datetime": datetime.now()
72
- })
73
  st.success("Dados registrados no Firebase Firestore com sucesso.")
74
 
75
- # 5. Função principal do app Streamlit
76
  def main():
77
  st.image("autism.png", width=200)
78
  st.title("PlayBot - Gerador de Brincadeiras", anchor="center")
79
- st.write("Conheça o PlayBot, um assistente inteligente que gera brincadeiras...")
80
 
81
  # Entrada de dados do usuário
82
  idade = st.number_input("Idade da criança:", min_value=1, max_value=16, step=1)
@@ -98,8 +92,16 @@ def main():
98
  game_description = game_generation(client, image_description, idade, nivel_suporte, tipo_estimulo)
99
  st.write(game_description)
100
 
101
- # Salvando dados no Firestore
102
- save_to_firestore(idade, nivel_suporte, tipo_estimulo)
 
 
 
 
 
 
 
 
103
 
104
  # Executar o app
105
  if __name__ == "__main__":
 
3
  import pandas as pd
4
  import json
5
  from datetime import datetime
6
+ from pathlib import Path
7
  import firebase_admin
8
  from firebase_admin import credentials, firestore
 
9
 
10
+ # Inicializar o Firebase, se ainda não estiver inicializado
11
+ if not firebase_admin._apps:
12
+ cred = credentials.Certificate("playbot-f0897-firebase-adminsdk-uay8b-0c87a1f1d0.json") # Substitua pelo caminho correto
13
+ firebase_admin.initialize_app(cred)
14
 
15
+ # Cliente Firestore
16
+ db = firestore.client()
 
 
17
 
18
  llava_model = 'llava-v1.5-7b-4096-preview'
19
  llama31_model = 'llama-3.1-70b-versatile'
20
 
21
+ # 2. Função para codificação de imagem
22
+ def encode_image(image_path):
23
+ with open(image_path, "rb") as image_file:
24
+ return base64.b64encode(image_file.read()).decode('utf-8')
 
 
25
 
26
+ # 3. Função para converter imagem em texto
27
  def image_to_text(client, model, base64_image, prompt):
28
  chat_completion = client.chat.completions.create(
29
  messages=[
 
44
  )
45
  return chat_completion.choices[0].message.content
46
 
47
+ # 4. Função para gerar uma brincadeira
48
  def game_generation(client, image_description, age, support_level, stimulus_type):
49
  chat_completion = client.chat.completions.create(
50
  messages=[
51
  {
52
  "role": "system",
53
+ "content": f"Suggest a safe game for a child of {age} years old, with support level {support_level} and focus on {stimulus_type}. The game should use the object described in the image and should not use objects that are dangerous for children, for example, scissors, knives, toothpicks, electrical wires, any sharp object cannot be used. If the object is not dangerous, describe the game in the form of a step-by-step recipe. Note: Write in Portuguese.",
54
  },
55
  {
56
  "role": "user",
 
61
  )
62
  return chat_completion.choices[0].message.content
63
 
64
+ # Função para salvar dados no Firebase Firestore
65
+ def save_to_firestore(data):
66
+ db.collection('usuarios').add(data)
 
 
 
 
 
67
  st.success("Dados registrados no Firebase Firestore com sucesso.")
68
 
69
+ # Função principal do app Streamlit
70
  def main():
71
  st.image("autism.png", width=200)
72
  st.title("PlayBot - Gerador de Brincadeiras", anchor="center")
73
+ st.write("Conheça o PlayBot, um assistente inteligente que gera brincadeiras divertidas e educativas para crianças com TEA...")
74
 
75
  # Entrada de dados do usuário
76
  idade = st.number_input("Idade da criança:", min_value=1, max_value=16, step=1)
 
92
  game_description = game_generation(client, image_description, idade, nivel_suporte, tipo_estimulo)
93
  st.write(game_description)
94
 
95
+ # Salvando dados no Firebase Firestore
96
+ data = {
97
+ "Idade": idade,
98
+ "Nível de Suporte": nivel_suporte,
99
+ "Tipo de Estimulo": tipo_estimulo,
100
+ "Descrição da Brincadeira": game_description,
101
+ "Timestamp": datetime.now().isoformat()
102
+ }
103
+
104
+ save_to_firestore(data)
105
 
106
  # Executar o app
107
  if __name__ == "__main__":