Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,96 @@
|
|
| 1 |
-
# 1. Imports and API setup
|
| 2 |
-
from groq import Groq
|
| 3 |
-
import base64
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import pandas as pd
|
| 6 |
-
|
| 7 |
-
client = Groq(
|
| 8 |
-
api_key="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",
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
llava_model = 'llava-v1.5-7b-4096-preview'
|
| 12 |
-
llama31_model = 'llama-3.1-70b-versatile'
|
| 13 |
-
|
| 14 |
-
# 2. Image encoding
|
| 15 |
-
def encode_image(image_path):
|
| 16 |
-
with open(image_path, "rb") as image_file:
|
| 17 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 18 |
-
|
| 19 |
-
# 3. Image to text function
|
| 20 |
-
def image_to_text(client, model, base64_image, prompt):
|
| 21 |
-
chat_completion = client.chat.completions.create(
|
| 22 |
-
messages=[
|
| 23 |
-
{
|
| 24 |
-
"role": "user",
|
| 25 |
-
"content": [
|
| 26 |
-
{"type": "text", "text": prompt},
|
| 27 |
-
{
|
| 28 |
-
"type": "image_url",
|
| 29 |
-
"image_url": {
|
| 30 |
-
"url": f"data:image/jpeg;base64,{base64_image}",
|
| 31 |
-
},
|
| 32 |
-
},
|
| 33 |
-
],
|
| 34 |
-
}
|
| 35 |
-
],
|
| 36 |
-
model=model
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
return chat_completion.choices[0].message.content
|
| 40 |
-
|
| 41 |
-
# 4. Short story generation function
|
| 42 |
-
def game_generation(client, image_description, age, support_level, stimulus_type):
|
| 43 |
-
chat_completion = client.chat.completions.create(
|
| 44 |
-
messages=[
|
| 45 |
-
{
|
| 46 |
-
"role": "system",
|
| 47 |
-
"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.",
|
| 48 |
-
},
|
| 49 |
-
{
|
| 50 |
-
"role": "user",
|
| 51 |
-
"content": image_description,
|
| 52 |
-
}
|
| 53 |
-
],
|
| 54 |
-
model=llama31_model
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
return chat_completion.choices[0].message.content
|
| 58 |
-
|
| 59 |
-
# 5. Streamlit app
|
| 60 |
-
def main():
|
| 61 |
-
st.image("autism.png", width=200)
|
| 62 |
-
st.title("PlayBot - Gerador de Brincadeiras", anchor="center")
|
| 63 |
-
st.write("Conheça o PlayBot, um assistente inteligente que gera brincadeiras divertidas e educativas para crianças com TEA, utilizando objetos disponíveis em sua casa. Basta tirar uma foto dos itens e o PlayBot sugere atividades interativas e adaptadas às necessidades sensoriais, promovendo aprendizado e diversão de forma simples e prática!")
|
| 64 |
-
|
| 65 |
-
idade = st.number_input("Idade da criança:", min_value=1, max_value=
|
| 66 |
-
nivel_suporte = st.selectbox("Nível de Suporte", ["Nível 1", "Nível 2", "Nível 3"])
|
| 67 |
-
tipo_estimulo = st.selectbox("Tipo de Estimulo", ["Sensorial", "Coordenação motora", "Raciocinio", "Fala"])
|
| 68 |
-
|
| 69 |
-
uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
|
| 70 |
-
if uploaded_file is not None:
|
| 71 |
-
# To read file as bytes:
|
| 72 |
-
bytes_data = uploaded_file.read()
|
| 73 |
-
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 74 |
-
prompt = '''
|
| 75 |
-
Describe this image in detail, including the appearance of the object(s).
|
| 76 |
-
'''
|
| 77 |
-
image_description = image_to_text(client, llava_model, base64_image, prompt)
|
| 78 |
-
|
| 79 |
-
#st.write("\n--- Image Description ---")
|
| 80 |
-
#st.write(image_description)
|
| 81 |
-
|
| 82 |
-
st.write("\n--- Brincadeira ---")
|
| 83 |
-
game_description = game_generation(client, image_description, idade, nivel_suporte, tipo_estimulo)
|
| 84 |
-
st.write(game_description)
|
| 85 |
-
|
| 86 |
-
# Save data to CSV
|
| 87 |
-
data = {
|
| 88 |
-
"Idade": idade,
|
| 89 |
-
"Nível de Suporte": nivel_suporte,
|
| 90 |
-
"Tipo de Estimulo": tipo_estimulo
|
| 91 |
-
}
|
| 92 |
-
df = pd.DataFrame([data])
|
| 93 |
-
df.to_csv("game_data.csv", mode='a', header=False, index=False)
|
| 94 |
-
|
| 95 |
-
if __name__ == "__main__":
|
| 96 |
main()
|
|
|
|
| 1 |
+
# 1. Imports and API setup
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import base64
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
client = Groq(
|
| 8 |
+
api_key="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",
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
llava_model = 'llava-v1.5-7b-4096-preview'
|
| 12 |
+
llama31_model = 'llama-3.1-70b-versatile'
|
| 13 |
+
|
| 14 |
+
# 2. Image encoding
|
| 15 |
+
def encode_image(image_path):
|
| 16 |
+
with open(image_path, "rb") as image_file:
|
| 17 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 18 |
+
|
| 19 |
+
# 3. Image to text function
|
| 20 |
+
def image_to_text(client, model, base64_image, prompt):
|
| 21 |
+
chat_completion = client.chat.completions.create(
|
| 22 |
+
messages=[
|
| 23 |
+
{
|
| 24 |
+
"role": "user",
|
| 25 |
+
"content": [
|
| 26 |
+
{"type": "text", "text": prompt},
|
| 27 |
+
{
|
| 28 |
+
"type": "image_url",
|
| 29 |
+
"image_url": {
|
| 30 |
+
"url": f"data:image/jpeg;base64,{base64_image}",
|
| 31 |
+
},
|
| 32 |
+
},
|
| 33 |
+
],
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
model=model
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
return chat_completion.choices[0].message.content
|
| 40 |
+
|
| 41 |
+
# 4. Short story generation function
|
| 42 |
+
def game_generation(client, image_description, age, support_level, stimulus_type):
|
| 43 |
+
chat_completion = client.chat.completions.create(
|
| 44 |
+
messages=[
|
| 45 |
+
{
|
| 46 |
+
"role": "system",
|
| 47 |
+
"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.",
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"role": "user",
|
| 51 |
+
"content": image_description,
|
| 52 |
+
}
|
| 53 |
+
],
|
| 54 |
+
model=llama31_model
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
return chat_completion.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
# 5. Streamlit app
|
| 60 |
+
def main():
|
| 61 |
+
st.image("autism.png", width=200)
|
| 62 |
+
st.title("PlayBot - Gerador de Brincadeiras", anchor="center")
|
| 63 |
+
st.write("Conheça o PlayBot, um assistente inteligente que gera brincadeiras divertidas e educativas para crianças com TEA, utilizando objetos disponíveis em sua casa. Basta tirar uma foto dos itens e o PlayBot sugere atividades interativas e adaptadas às necessidades sensoriais, promovendo aprendizado e diversão de forma simples e prática!")
|
| 64 |
+
|
| 65 |
+
idade = st.number_input("Idade da criança:", min_value=1, max_value=16, step=1)
|
| 66 |
+
nivel_suporte = st.selectbox("Nível de Suporte", ["Nível 1", "Nível 2", "Nível 3"])
|
| 67 |
+
tipo_estimulo = st.selectbox("Tipo de Estimulo", ["Sensorial", "Coordenação motora", "Raciocinio", "Fala"])
|
| 68 |
+
|
| 69 |
+
uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
|
| 70 |
+
if uploaded_file is not None:
|
| 71 |
+
# To read file as bytes:
|
| 72 |
+
bytes_data = uploaded_file.read()
|
| 73 |
+
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 74 |
+
prompt = '''
|
| 75 |
+
Describe this image in detail, including the appearance of the object(s).
|
| 76 |
+
'''
|
| 77 |
+
image_description = image_to_text(client, llava_model, base64_image, prompt)
|
| 78 |
+
|
| 79 |
+
#st.write("\n--- Image Description ---")
|
| 80 |
+
#st.write(image_description)
|
| 81 |
+
|
| 82 |
+
st.write("\n--- Brincadeira ---")
|
| 83 |
+
game_description = game_generation(client, image_description, idade, nivel_suporte, tipo_estimulo)
|
| 84 |
+
st.write(game_description)
|
| 85 |
+
|
| 86 |
+
# Save data to CSV
|
| 87 |
+
data = {
|
| 88 |
+
"Idade": idade,
|
| 89 |
+
"Nível de Suporte": nivel_suporte,
|
| 90 |
+
"Tipo de Estimulo": tipo_estimulo
|
| 91 |
+
}
|
| 92 |
+
df = pd.DataFrame([data])
|
| 93 |
+
df.to_csv("game_data.csv", mode='a', header=False, index=False)
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
main()
|