Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,85 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from groq import Groq
|
| 5 |
-
import base64
|
| 6 |
-
from prompt import build_messages
|
| 7 |
-
from werkzeug.utils import secure_filename
|
| 8 |
-
from gerar_json import processar_imagem
|
| 9 |
-
|
| 10 |
-
# Carregar variáveis de ambiente
|
| 11 |
-
load_dotenv()
|
| 12 |
-
client = Groq(
|
| 13 |
-
api_key=os.getenv("GROQ_API_KEY"),
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
# Função para codificar a imagem
|
| 17 |
-
def encode_image(image_path):
|
| 18 |
-
with open(image_path, "rb") as image_file:
|
| 19 |
-
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 20 |
-
|
| 21 |
-
# Configuração do Streamlit
|
| 22 |
-
st.title("Análise de Imagens de Vestuário")
|
| 23 |
-
|
| 24 |
-
# Formulário para entrada do usuário
|
| 25 |
-
with st.form(key="form_analise"):
|
| 26 |
-
sexo = st.selectbox("Selecione o sexo:", ["Masculino", "Feminino"])
|
| 27 |
-
ocasiao = st.selectbox("Selecione a ocasião:", ["Formal", "Casual", "Esporte", "Festa", "Praia"])
|
| 28 |
-
imagem = st.file_uploader("Carregue a imagem:", type=["jpg", "jpeg", "png"])
|
| 29 |
-
submit_button = st.form_submit_button(label="Enviar")
|
| 30 |
-
|
| 31 |
-
# Lógica de processamento ao enviar o formulário
|
| 32 |
-
if submit_button:
|
| 33 |
-
if imagem is not None:
|
| 34 |
-
# Garantir que o nome do arquivo seja seguro
|
| 35 |
-
file_name = secure_filename(imagem.name)
|
| 36 |
-
|
| 37 |
-
# Criar uma pasta temporária para armazenar o arquivo
|
| 38 |
-
temp_dir = "temp_images"
|
| 39 |
-
os.makedirs(temp_dir, exist_ok=True)
|
| 40 |
-
file_path = os.path.join(temp_dir, file_name)
|
| 41 |
-
|
| 42 |
-
# Salvar a imagem no disco
|
| 43 |
-
with open(file_path, "wb") as f:
|
| 44 |
-
f.write(imagem.getbuffer())
|
| 45 |
-
|
| 46 |
-
# Codificar a imagem para base64
|
| 47 |
-
base64_image = encode_image(file_path)
|
| 48 |
-
|
| 49 |
-
# Processar a imagem
|
| 50 |
-
deteccao = processar_imagem(file_path)[2]
|
| 51 |
-
|
| 52 |
-
# Construir mensagens para os prompts
|
| 53 |
-
messages_for_description, messages_for_analysis = build_messages(deteccao, base64_image)
|
| 54 |
-
|
| 55 |
-
try:
|
| 56 |
-
# Primeira interação com o modelo
|
| 57 |
-
chat_completion = client.chat.completions.create(
|
| 58 |
-
messages=messages_for_description,
|
| 59 |
-
model="llama-3.2-90b-vision-preview",
|
| 60 |
-
)
|
| 61 |
-
clothing_description = chat_completion.choices[0].message.content
|
| 62 |
-
|
| 63 |
-
# Segunda interação com o modelo
|
| 64 |
-
analysis_response = client.chat.completions.create(
|
| 65 |
-
messages=messages_for_analysis(clothing_description, sexo, ocasiao),
|
| 66 |
-
model="llama-3.2-
|
| 67 |
-
)
|
| 68 |
-
resposta = analysis_response.choices[0].message.content
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
st.
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from groq import Groq
|
| 5 |
+
import base64
|
| 6 |
+
from prompt import build_messages
|
| 7 |
+
from werkzeug.utils import secure_filename
|
| 8 |
+
from gerar_json import processar_imagem
|
| 9 |
+
|
| 10 |
+
# Carregar variáveis de ambiente
|
| 11 |
+
load_dotenv()
|
| 12 |
+
client = Groq(
|
| 13 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Função para codificar a imagem
|
| 17 |
+
def encode_image(image_path):
|
| 18 |
+
with open(image_path, "rb") as image_file:
|
| 19 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 20 |
+
|
| 21 |
+
# Configuração do Streamlit
|
| 22 |
+
st.title("Análise de Imagens de Vestuário")
|
| 23 |
+
|
| 24 |
+
# Formulário para entrada do usuário
|
| 25 |
+
with st.form(key="form_analise"):
|
| 26 |
+
sexo = st.selectbox("Selecione o sexo:", ["Masculino", "Feminino"])
|
| 27 |
+
ocasiao = st.selectbox("Selecione a ocasião:", ["Formal", "Casual", "Esporte", "Festa", "Praia"])
|
| 28 |
+
imagem = st.file_uploader("Carregue a imagem:", type=["jpg", "jpeg", "png"])
|
| 29 |
+
submit_button = st.form_submit_button(label="Enviar")
|
| 30 |
+
|
| 31 |
+
# Lógica de processamento ao enviar o formulário
|
| 32 |
+
if submit_button:
|
| 33 |
+
if imagem is not None:
|
| 34 |
+
# Garantir que o nome do arquivo seja seguro
|
| 35 |
+
file_name = secure_filename(imagem.name)
|
| 36 |
+
|
| 37 |
+
# Criar uma pasta temporária para armazenar o arquivo
|
| 38 |
+
temp_dir = "temp_images"
|
| 39 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 40 |
+
file_path = os.path.join(temp_dir, file_name)
|
| 41 |
+
|
| 42 |
+
# Salvar a imagem no disco
|
| 43 |
+
with open(file_path, "wb") as f:
|
| 44 |
+
f.write(imagem.getbuffer())
|
| 45 |
+
|
| 46 |
+
# Codificar a imagem para base64
|
| 47 |
+
base64_image = encode_image(file_path)
|
| 48 |
+
|
| 49 |
+
# Processar a imagem
|
| 50 |
+
deteccao = processar_imagem(file_path)[2]
|
| 51 |
+
|
| 52 |
+
# Construir mensagens para os prompts
|
| 53 |
+
messages_for_description, messages_for_analysis = build_messages(deteccao, base64_image)
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Primeira interação com o modelo
|
| 57 |
+
chat_completion = client.chat.completions.create(
|
| 58 |
+
messages=messages_for_description,
|
| 59 |
+
model="llama-3.2-90b-vision-preview",
|
| 60 |
+
)
|
| 61 |
+
clothing_description = chat_completion.choices[0].message.content
|
| 62 |
+
|
| 63 |
+
# Segunda interação com o modelo
|
| 64 |
+
analysis_response = client.chat.completions.create(
|
| 65 |
+
messages=messages_for_analysis(clothing_description, sexo, ocasiao),
|
| 66 |
+
model="llama-3.2-11b-vision-preview",
|
| 67 |
+
)
|
| 68 |
+
resposta = analysis_response.choices[0].message.content
|
| 69 |
+
|
| 70 |
+
final_answer = client.chat.completions.create(
|
| 71 |
+
messages=format_answer(resposta),
|
| 72 |
+
model="llama3-70b-8192"
|
| 73 |
+
)
|
| 74 |
+
resposta_final = analysis_response.choices[0].message.content
|
| 75 |
+
|
| 76 |
+
# Exibir o resultado
|
| 77 |
+
st.success("Análise concluída com sucesso!")
|
| 78 |
+
st.write("### Resultado:")
|
| 79 |
+
st.write(resposta_final)
|
| 80 |
+
|
| 81 |
+
os.remove(file_path)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.error(f"Ocorreu um erro durante o processamento: {e}")
|
| 84 |
+
else:
|
| 85 |
+
st.warning("Por favor, carregue uma imagem para análise.")
|