Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +86 -0
- images.jpg +0 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 analyzer_generation(client, image_description):
|
| 43 |
+
chat_completion = client.chat.completions.create(
|
| 44 |
+
messages=[
|
| 45 |
+
{
|
| 46 |
+
"role": "system",
|
| 47 |
+
"content": f"You are a food and nutrition expert, you analyze Food by Photo: The user takes a photo of a plate of food, and the app describes the ingredients, possible calories, and offers suggestions on how to make the meal healthier or more balanced. 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("images.jpg", width=200)
|
| 62 |
+
st.title("FoodBot - Análisador de Alimentos", anchor="center")
|
| 63 |
+
st.write("Conheça o FoodBot, um assistente inteligente que o usuário tira uma foto de um prato de comida, e o app descreve os ingredientes, possíveis calorias, e oferece sugestões de como tornar a refeição mais saudável ou equilibrada.")
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
|
| 67 |
+
if uploaded_file is not None:
|
| 68 |
+
# To read file as bytes:
|
| 69 |
+
bytes_data = uploaded_file.read()
|
| 70 |
+
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 71 |
+
prompt = '''
|
| 72 |
+
Describe this image in detail, including the appearance of the object(s). Note: Write in Portuguese.
|
| 73 |
+
'''
|
| 74 |
+
image_description = image_to_text(client, llava_model, base64_image, prompt)
|
| 75 |
+
|
| 76 |
+
st.write("\n--- Image Description ---")
|
| 77 |
+
st.write(image_description)
|
| 78 |
+
|
| 79 |
+
st.write("\n--- Análise do Alimento ---")
|
| 80 |
+
food_description = analyzer_generation(client, image_description)
|
| 81 |
+
st.write(food_description)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
main()
|
images.jpg
ADDED
|
requirements.txt
ADDED
|
Binary file (1.83 kB). View file
|
|
|